mirror of https://github.com/nuxt/nuxt.git
feat: abstract minify and use value for all modes (#3965)
* feat: abstract minify and use value for all modes * change name to htmlMinify * only override htmlMinify with legacy value in generate mode * Use default options for htmlMinify and add override info to warning * Remove redundant minification settings * Override minification settings in generator * Cleanup and add TODO for nuxt 3 * Improve warning * set default minify option to "true" * make tests pass again * remove the culprit * replace htmlMinify with html.minify
This commit is contained in:
parent
bc9071663f
commit
d69b4b8b12
|
@ -4,7 +4,7 @@ import htmlMinifier from 'html-minifier'
|
||||||
import Chalk from 'chalk'
|
import Chalk from 'chalk'
|
||||||
import fsExtra from 'fs-extra'
|
import fsExtra from 'fs-extra'
|
||||||
import consola from 'consola'
|
import consola from 'consola'
|
||||||
import { isUrl, promisifyRoute, waitFor, flatRoutes } from '../common/utils'
|
import { flatRoutes, isUrl, promisifyRoute, waitFor } from '../common/utils'
|
||||||
|
|
||||||
export default class Generator {
|
export default class Generator {
|
||||||
constructor(nuxt, builder) {
|
constructor(nuxt, builder) {
|
||||||
|
@ -223,9 +223,19 @@ export default class Generator {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.options.generate.minify) {
|
let minificationOptions = this.options.build.html.minify
|
||||||
|
|
||||||
|
// Legacy: Override minification options with generate.minify if present
|
||||||
|
// TODO: Remove in Nuxt version 3
|
||||||
|
if (typeof this.options.generate.minify !== 'undefined') {
|
||||||
|
minificationOptions = this.options.generate.minify
|
||||||
|
consola.warn('generate.minify has been deprecated and will be removed in the next major version.' +
|
||||||
|
' Use build.html.minify instead!')
|
||||||
|
}
|
||||||
|
|
||||||
|
if (minificationOptions) {
|
||||||
try {
|
try {
|
||||||
html = htmlMinifier.minify(html, this.options.generate.minify)
|
html = htmlMinifier.minify(html, minificationOptions)
|
||||||
} catch (err) /* istanbul ignore next */ {
|
} catch (err) /* istanbul ignore next */ {
|
||||||
const minifyErr = new Error(
|
const minifyErr = new Error(
|
||||||
`HTML minification failed. Make sure the route generates valid HTML. Failed HTML:\n ${html}`
|
`HTML minification failed. Make sure the route generates valid HTML. Failed HTML:\n ${html}`
|
||||||
|
|
|
@ -63,7 +63,7 @@ export default class WebpackClientConfig extends WebpackBaseConfig {
|
||||||
new HTMLPlugin({
|
new HTMLPlugin({
|
||||||
filename: '../server/index.ssr.html',
|
filename: '../server/index.ssr.html',
|
||||||
template: this.options.appTemplatePath,
|
template: this.options.appTemplatePath,
|
||||||
minify: true,
|
minify: this.options.build.html.minify,
|
||||||
inject: false // Resources will be injected using bundleRenderer
|
inject: false // Resources will be injected using bundleRenderer
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
@ -73,7 +73,7 @@ export default class WebpackClientConfig extends WebpackBaseConfig {
|
||||||
new HTMLPlugin({
|
new HTMLPlugin({
|
||||||
filename: '../server/index.spa.html',
|
filename: '../server/index.spa.html',
|
||||||
template: this.options.appTemplatePath,
|
template: this.options.appTemplatePath,
|
||||||
minify: true,
|
minify: this.options.build.html.minify,
|
||||||
inject: true,
|
inject: true,
|
||||||
chunksSortMode: 'dependency'
|
chunksSortMode: 'dependency'
|
||||||
}),
|
}),
|
||||||
|
|
|
@ -116,6 +116,20 @@ export default {
|
||||||
stage: 2
|
stage: 2
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
html: {
|
||||||
|
minify: {
|
||||||
|
collapseBooleanAttributes: true,
|
||||||
|
decodeEntities: true,
|
||||||
|
minifyCSS: true,
|
||||||
|
minifyJS: true,
|
||||||
|
processConditionalComments: true,
|
||||||
|
removeEmptyAttributes: true,
|
||||||
|
removeRedundantAttributes: true,
|
||||||
|
trimCustomFragments: true,
|
||||||
|
useShortDoctype: true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
templates: [],
|
templates: [],
|
||||||
watch: [],
|
watch: [],
|
||||||
devMiddleware: {},
|
devMiddleware: {},
|
||||||
|
@ -140,27 +154,7 @@ export default {
|
||||||
concurrency: 500,
|
concurrency: 500,
|
||||||
interval: 0,
|
interval: 0,
|
||||||
subFolders: true,
|
subFolders: true,
|
||||||
fallback: '200.html',
|
fallback: '200.html'
|
||||||
minify: {
|
|
||||||
collapseBooleanAttributes: true,
|
|
||||||
collapseWhitespace: false,
|
|
||||||
decodeEntities: true,
|
|
||||||
minifyCSS: true,
|
|
||||||
minifyJS: true,
|
|
||||||
processConditionalComments: true,
|
|
||||||
removeAttributeQuotes: false,
|
|
||||||
removeComments: false,
|
|
||||||
removeEmptyAttributes: true,
|
|
||||||
removeOptionalTags: false,
|
|
||||||
removeRedundantAttributes: true,
|
|
||||||
removeScriptTypeAttributes: false,
|
|
||||||
removeStyleLinkTypeAttributes: false,
|
|
||||||
removeTagWhitespace: false,
|
|
||||||
sortAttributes: true,
|
|
||||||
sortClassName: false,
|
|
||||||
trimCustomFragments: true,
|
|
||||||
useShortDoctype: true
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
env: {},
|
env: {},
|
||||||
head: {
|
head: {
|
||||||
|
|
Loading…
Reference in New Issue