[Webpack 2] Optimize React size and performance with Webpack production plugins

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Webpack 2] Optimize React size and performance with Webpack production plugins相关的知识,希望对你有一定的参考价值。

You can fine tune several webpack plugins to make your bundle as small as it can be for your specific application. However there are a few things you can do for pretty much every application to make it smaller and run faster. In this lesson we’ll combine several webpack plugins to optimize things for a React application (this is also applicable for non-React applications as well).

 

First we need to modify the prod scripts from:

"build:prod": "webpack --env.prod -p",

to:

"build:prod": "webpack --env.prod",

 

Help methods:

    plugins: removeEmpty([
      // doesn‘t save anything in this small app. [email protected] mostly takes care of this
      ifProd(new webpack.optimize.DedupePlugin()),
      // saves a couple of kBs
      ifProd(new webpack.LoaderOptionsPlugin({  //Loader plugin only works in webpack 2
        minimize: true,
        debug: false,
        quiet: true,
      })),
      // saves 65 kB with Uglify!! Saves 38 kB without
      ifProd(new webpack.DefinePlugin({
        process.env: {
          NODE_ENV: "production",
        },
      })),
      // saves 711 kB!!
      ifProd(new webpack.optimize.UglifyJsPlugin({
        compress: {
          screw_ie8: true, // eslint-disable-line
          warnings: false,
        },
      })),
    ])

 

以上是关于[Webpack 2] Optimize React size and performance with Webpack production plugins的主要内容,如果未能解决你的问题,请参考以下文章

TypeError: webpack.optimize.UglifyJsPlugin 不是构造函数

错误:webpack.optimize.CommonsChunkPlugin 已被移除,请改用 config.optimization.splitChunks

webpack之插件optimize css assets webpack plugin

webpack5以上版本 使用optimize-css-assets-webpack-plugin压缩问题

webpack--webpack.optimize.CommonsChunkPlugin has been removed, please use config.o

迁移到webpack4:从webpack.optimize.CommonsChunkPlugin到config.optimization.splitChunk,以及有个搜出来的中文解决办法是错的