Webpack how to build production code and how to use it

前端 未结 8 924
梦谈多话
梦谈多话 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:45

    After observing number of viewers to this question I decided to conclude an answer from Vikramaditya and Sandeep.

    To build the production code the first thing you have to create is production configuration with optimization packages like,

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

    Then in the package.json file you can configure the build procedure with this production configuration

    "scripts": {
        "build": "NODE_ENV=production webpack --config ./webpack.production.config.js"
    },
    

    now you have to run the following command to initiate the build

    npm run build
    

    As per my production build configuration webpack will build the source to ./dist directory.

    Now your UI code will be available in ./dist/ directory. Configure your server to serve these files as static assets. Done!

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-29 15:47

    This will help you.

    plugins: [
        new webpack.DefinePlugin({
          'process.env': {
            // This has effect on the react lib size
            'NODE_ENV': JSON.stringify('production'),
          }
        }),
        new ExtractTextPlugin("bundle.css", {allChunks: false}),
        new webpack.optimize.AggressiveMergingPlugin(),
        new webpack.optimize.OccurrenceOrderPlugin(),
        new webpack.optimize.DedupePlugin(),
        new webpack.optimize.UglifyJsPlugin({
          mangle: true,
          compress: {
            warnings: false, // Suppress uglification warnings
            pure_getters: true,
            unsafe: true,
            unsafe_comps: true,
            screw_ie8: true
          },
          output: {
            comments: false,
          },
          exclude: [/\.min\.js$/gi] // skip pre-minified libs
        }),
        new webpack.IgnorePlugin(/^\.\/locale$/, [/moment$/]), //https://stackoverflow.com/questions/25384360/how-to-prevent-moment-js-from-loading-locales-with-webpack
        new CompressionPlugin({
          asset: "[path].gz[query]",
          algorithm: "gzip",
          test: /\.js$|\.css$|\.html$/,
          threshold: 10240,
          minRatio: 0
        })
      ],
    
    0 讨论(0)
  • 2020-11-29 15:52

    You can use argv npm module (install it by running npm install argv --save) for getting params in your webpack.config.js file and as for production you use -p flag "build": "webpack -p", you can add condition in webpack.config.js file like below

    plugins: [
        new webpack.DefinePlugin({
            'process.env':{
                'NODE_ENV': argv.p ? JSON.stringify('production') : JSON.stringify('development')
            }
        })
    ]
    

    And thats it.

    0 讨论(0)
  • 2020-11-29 15:54

    In addition to Gilson PJ answer:

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

    with

    "scripts": {
        "build": "NODE_ENV=production webpack -p --config ./webpack.production.config.js"
    },
    

    cause that the it tries to uglify your code twice. See https://webpack.github.io/docs/cli.html#production-shortcut-p for more information.

    You can fix this by removing the UglifyJsPlugin from plugins-array or add the OccurrenceOrderPlugin and remove the "-p"-flag. so one possible solution would be

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

    and

    "scripts": {
        "build": "NODE_ENV=production webpack --config ./webpack.production.config.js"
    },
    
    0 讨论(0)
  • 2020-11-29 15:55

    You can add the plugins as suggested by @Vikramaditya. Then to generate the production build. You have to run the the command

    webpack -p --config ./webpack.production.config.js
    

    The -p tells webpack to generate a production build. You have to change the build script in package.json to include the production flag.

    0 讨论(0)
提交回复
热议问题