是否可以缩小除一个以外的所有捆绑包?

Is it possible to minify all bundles except one?

我的 webpack 优化配置如下:

optimization: {
    runtimeChunk: 'single',
    minimize: false,
    splitChunks: {
        chunks: 'all',
        maxInitialRequests: Infinity,
        minSize: 0,
        cacheGroups: {
            config: {
                test: /[\/]app[\/]js[\/]config[\/]/,
                minSize: 0
            },
            vendors: {
                test: /[\/]node_modules[\/]/,
                name(module) {
                    // get the name. E.g. node_modules/packageName/not/this/part.js
                    // or node_modules/packageName
                    const packageName = module.context.match(/[\/]node_modules[\/](.*?)([\/]|$)/)[1];

                    // npm package names are URL-safe, but some servers don't like @ symbols
                    return `npm.${packageName.replace('@', '')}`;
                },
            }
        },
    },
},

这完美地工作并生成了我想要的所有包。 但是,我不想缩小配置包。不过,所有其他捆绑包都应该缩小。到目前为止,我只找到了一个全局设置,最小化:true/false。我有另一种更精细的设置方式吗?

查看有关 optimization.minimizer setting and chunkFilter 的文档 选项。您应该为 config 块设置一个名称。并在您的 optimization 设置中添加这样的内容:

minimizer: [
  new TerserPlugin({
    chunkFilter: (chunk) => {
      if (chunk.name === 'CONFIG CHUNK NAME') {
        return false;
      }

      return true;
    },
  }),
],

请注意,您需要安装插件:const TerserPlugin = require('terser-webpack-plugin');