Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.5k views
in Technique[技术] by (71.8m points)

如何在开发环境实现 webpack 4 production mode 的压缩效果?

需求和背景

commonjs 写的云函数代码,用 webpack 打包,然后在开发环境(mode=development|none)时打包出来的代码超出运行环境的限制了,因为希望在开发环境也能进行压缩。

尝试和结果

看到 webpack mode production 里列出的配置,于是搬到开发环境,最终的配置大概如下:

{
    mode: "none",
    target: 'node',
    optimization: {
      minimize: true,
      sideEffects: false,
      usedExports: true,
      concatenateModules: true,
      namedModules: false,
      namedChunks: false,
      occurrenceOrder: true,
      checkWasmTypes: true,
      nodeEnv: 'production',
      flagIncludedChunks: true,
      splitChunks: {
        hidePathInfo: true,
        minSize: 30000,
        maxAsyncRequests: 5,
        maxInitialRequests: 3,
      },
      noEmitOnErrors: true,
    },
    plugins: [new TerserPlugin({
      terserOptions: {
        ecma: 5,
        compress: {
          passes: 4,
          arrows: true,
          dead_code: true,
          drop_debugger: true,
          evaluate: true,
          side_effects: true,
          module: true,
        },
        mangle: true,
        nameCache: {},
      }
    })]
}

运行构建后,输出的包大小是 3.8M, 而只要把 mode 改成 production,输出包的大小就变成了 1.4M,看起来上面这些配置并不能起到 mode=production 的作用。

问题

请问 mode=production 还做了些什么工作?如何在开发环境实现 mode=production 的压缩效果?

相似问题:https://stackoverflow.com/que...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...