Skip to content

Commit

Permalink
feat(builder): allow to customize or disable minimizer plugins (#4018)
Browse files Browse the repository at this point in the history
* feat(builder): allow to customize or disable minimizer plugins

* feat: support optimization.minimize

https://webpack.js.org/configuration/optimization/#optimization-minimize

* fix typos
  • Loading branch information
pi0 authored and Atinux committed Sep 30, 2018
1 parent 1bf6385 commit 8f06a18
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 18 deletions.
41 changes: 23 additions & 18 deletions lib/builder/webpack/client.js
Expand Up @@ -99,31 +99,36 @@ export default class WebpackClientConfig extends WebpackBaseConfig {
extendConfig() {
const config = super.extendConfig(...arguments)

if (!this.options.dev && !config.optimization.minimizer) {
// Add minimizer plugins
if (config.optimization.minimize && config.optimization.minimizer === undefined) {
config.optimization.minimizer = []

// https://github.com/webpack-contrib/terser-webpack-plugin
const terserJsPlugin = new TerserWebpackPlugin({
parallel: true,
cache: this.options.build.cache,
sourceMap: config.devtool && /source-?map/.test(config.devtool),
extractComments: {
filename: 'LICENSES'
},
terserOptions: {
output: {
comments: /^\**!|@preserve|@license|@cc_on/
}
}
})
config.optimization.minimizer.push(terserJsPlugin)
if (this.options.build.terser) {
config.optimization.minimizer.push(
new TerserWebpackPlugin(Object.assign({
parallel: true,
cache: this.options.build.cache,
sourceMap: config.devtool && /source-?map/.test(config.devtool),
extractComments: {
filename: 'LICENSES'
},
terserOptions: {
output: {
comments: /^\**!|@preserve|@license|@cc_on/
}
}
}, this.options.build.terser))
)
}

// https://github.com/NMFR/optimize-css-assets-webpack-plugin
// https://github.com/webpack-contrib/mini-css-extract-plugin#minimizing-for-production
// TODO: Remove OptimizeCSSAssetsPlugin when upgrading to webpack 5
if (this.options.build.extractCSS) {
const optimizeCSSPlugin = new OptimizeCSSAssetsPlugin({})
config.optimization.minimizer.push(optimizeCSSPlugin)
if (this.options.build.optimizeCSS) {
config.optimization.minimizer.push(
new OptimizeCSSAssetsPlugin(Object.assign({}, this.options.build.optimizeCSS))
)
}
}

Expand Down
4 changes: 4 additions & 0 deletions lib/common/nuxt.config.js
Expand Up @@ -87,8 +87,12 @@ export default {
},
styleResources: {},
plugins: [],
terser: {},
optimizeCSS: undefined,
optimization: {
runtimeChunk: 'single',
minimize: undefined,
minimizer: undefined,
splitChunks: {
chunks: 'all',
automaticNameDelimiter: '.',
Expand Down
10 changes: 10 additions & 0 deletions lib/common/options.js
Expand Up @@ -239,6 +239,16 @@ Options.from = function (_options) {
options.build.extractCSS = false
}

// Enable minimize for production builds
if (options.build.optimization.minimize === undefined) {
options.build.optimization.minimize = !options.dev
}

// Enable optimizeCSS only when extractCSS is enabled
if (options.build.optimizeCSS === undefined) {
options.build.optimizeCSS = options.build.extractCSS ? {} : false
}

const loaders = options.build.loaders
const vueLoader = loaders.vue
if (vueLoader.productionMode === undefined) {
Expand Down

0 comments on commit 8f06a18

Please sign in to comment.