diff --git a/lib/builder/generator.js b/lib/builder/generator.js index c04ef183b5..407ca32794 100644 --- a/lib/builder/generator.js +++ b/lib/builder/generator.js @@ -4,7 +4,7 @@ import htmlMinifier from 'html-minifier' import Chalk from 'chalk' import fsExtra from 'fs-extra' import consola from 'consola' -import { isUrl, promisifyRoute, waitFor, flatRoutes } from '../common/utils' +import { flatRoutes, isUrl, promisifyRoute, waitFor } from '../common/utils' export default class Generator { constructor(nuxt, builder) { @@ -223,9 +223,19 @@ export default class Generator { 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 { - html = htmlMinifier.minify(html, this.options.generate.minify) + html = htmlMinifier.minify(html, minificationOptions) } catch (err) /* istanbul ignore next */ { const minifyErr = new Error( `HTML minification failed. Make sure the route generates valid HTML. Failed HTML:\n ${html}` diff --git a/lib/builder/webpack/client.js b/lib/builder/webpack/client.js index 775862ff36..f337374bfa 100644 --- a/lib/builder/webpack/client.js +++ b/lib/builder/webpack/client.js @@ -63,7 +63,7 @@ export default class WebpackClientConfig extends WebpackBaseConfig { new HTMLPlugin({ filename: '../server/index.ssr.html', template: this.options.appTemplatePath, - minify: true, + minify: this.options.build.html.minify, inject: false // Resources will be injected using bundleRenderer }) ) @@ -73,7 +73,7 @@ export default class WebpackClientConfig extends WebpackBaseConfig { new HTMLPlugin({ filename: '../server/index.spa.html', template: this.options.appTemplatePath, - minify: true, + minify: this.options.build.html.minify, inject: true, chunksSortMode: 'dependency' }), diff --git a/lib/common/nuxt.config.js b/lib/common/nuxt.config.js index cefbb2051d..8eafd7f9ff 100644 --- a/lib/common/nuxt.config.js +++ b/lib/common/nuxt.config.js @@ -116,6 +116,20 @@ export default { stage: 2 } }, + html: { + minify: { + collapseBooleanAttributes: true, + decodeEntities: true, + minifyCSS: true, + minifyJS: true, + processConditionalComments: true, + removeEmptyAttributes: true, + removeRedundantAttributes: true, + trimCustomFragments: true, + useShortDoctype: true + } + }, + templates: [], watch: [], devMiddleware: {}, @@ -140,27 +154,7 @@ export default { concurrency: 500, interval: 0, subFolders: true, - 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 - } + fallback: '200.html' }, env: {}, head: {