mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-23 14:15:13 +00:00
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:
parent
1bf6385d48
commit
8f06a187db
@ -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))
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -87,8 +87,12 @@ export default {
|
||||
},
|
||||
styleResources: {},
|
||||
plugins: [],
|
||||
terser: {},
|
||||
optimizeCSS: undefined,
|
||||
optimization: {
|
||||
runtimeChunk: 'single',
|
||||
minimize: undefined,
|
||||
minimizer: undefined,
|
||||
splitChunks: {
|
||||
chunks: 'all',
|
||||
automaticNameDelimiter: '.',
|
||||
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user