Skip to content

Commit

Permalink
feat: abstract minify and use value for all modes (#3965)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
manniL authored and Atinux committed Sep 30, 2018
1 parent bc90716 commit d69b4b8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 26 deletions.
16 changes: 13 additions & 3 deletions lib/builder/generator.js
Expand Up @@ -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) {
Expand Down Expand Up @@ -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}`
Expand Down
4 changes: 2 additions & 2 deletions lib/builder/webpack/client.js
Expand Up @@ -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
})
)
Expand All @@ -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'
}),
Expand Down
36 changes: 15 additions & 21 deletions lib/common/nuxt.config.js
Expand Up @@ -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: {},
Expand All @@ -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: {
Expand Down

0 comments on commit d69b4b8

Please sign in to comment.