feat(builder): allow to customize or disable minimizer plugins (#4018)

* feat(builder): allow to customize or disable minimizer plugins

* feat: support optimization.minimize

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

* fix typos
This commit is contained in:
Pooya Parsa 2018-09-30 19:39:06 +03:30 committed by Sébastien Chopin
parent 1bf6385d48
commit 8f06a187db
3 changed files with 37 additions and 18 deletions

View File

@ -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))
)
}
}

View File

@ -87,8 +87,12 @@ export default {
},
styleResources: {},
plugins: [],
terser: {},
optimizeCSS: undefined,
optimization: {
runtimeChunk: 'single',
minimize: undefined,
minimizer: undefined,
splitChunks: {
chunks: 'all',
automaticNameDelimiter: '.',

View File

@ -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) {