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() { extendConfig() {
const config = super.extendConfig(...arguments) 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 = [] config.optimization.minimizer = []
// https://github.com/webpack-contrib/terser-webpack-plugin // https://github.com/webpack-contrib/terser-webpack-plugin
const terserJsPlugin = new TerserWebpackPlugin({ if (this.options.build.terser) {
parallel: true, config.optimization.minimizer.push(
cache: this.options.build.cache, new TerserWebpackPlugin(Object.assign({
sourceMap: config.devtool && /source-?map/.test(config.devtool), parallel: true,
extractComments: { cache: this.options.build.cache,
filename: 'LICENSES' sourceMap: config.devtool && /source-?map/.test(config.devtool),
}, extractComments: {
terserOptions: { filename: 'LICENSES'
output: { },
comments: /^\**!|@preserve|@license|@cc_on/ terserOptions: {
} output: {
} comments: /^\**!|@preserve|@license|@cc_on/
}) }
config.optimization.minimizer.push(terserJsPlugin) }
}, this.options.build.terser))
)
}
// https://github.com/NMFR/optimize-css-assets-webpack-plugin // https://github.com/NMFR/optimize-css-assets-webpack-plugin
// https://github.com/webpack-contrib/mini-css-extract-plugin#minimizing-for-production // https://github.com/webpack-contrib/mini-css-extract-plugin#minimizing-for-production
// TODO: Remove OptimizeCSSAssetsPlugin when upgrading to webpack 5 // TODO: Remove OptimizeCSSAssetsPlugin when upgrading to webpack 5
if (this.options.build.extractCSS) { if (this.options.build.optimizeCSS) {
const optimizeCSSPlugin = new OptimizeCSSAssetsPlugin({}) config.optimization.minimizer.push(
config.optimization.minimizer.push(optimizeCSSPlugin) new OptimizeCSSAssetsPlugin(Object.assign({}, this.options.build.optimizeCSS))
)
} }
} }

View File

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

View File

@ -239,6 +239,16 @@ Options.from = function (_options) {
options.build.extractCSS = false 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 loaders = options.build.loaders
const vueLoader = loaders.vue const vueLoader = loaders.vue
if (vueLoader.productionMode === undefined) { if (vueLoader.productionMode === undefined) {