Webpack how to build production code and how to use it

前端 未结 8 1090
梦谈多话
梦谈多话 2020-11-29 15:06

I am very new to webpack, I found that in production build we can able to reduce the size of overall code. Currently webpack builds around 8MB files and main.js around 5MB.

8条回答
  •  北荒
    北荒 (楼主)
    2020-11-29 15:47

    Use these plugins to optimize your production build:

      new webpack.optimize.CommonsChunkPlugin('common'),
      new webpack.optimize.DedupePlugin(),
      new webpack.optimize.UglifyJsPlugin(),
      new webpack.optimize.AggressiveMergingPlugin()
    

    I recently came to know about compression-webpack-plugin which gzips your output bundle to reduce its size. Add this as well in the above listed plugins list to further optimize your production code.

    new CompressionPlugin({
          asset: "[path].gz[query]",
          algorithm: "gzip",
          test: /\.js$|\.css$|\.html$/,
          threshold: 10240,
          minRatio: 0.8
    })
    

    Server side dynamic gzip compression is not recommended for serving static client-side files because of heavy CPU usage.

提交回复
热议问题