Webpack how to build production code and how to use it Webpack how to build production code and how to use it reactjs reactjs

Webpack how to build production code and how to use it


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

NODE_ENV=production webpack --config ./webpack.production.config.js

If using babel, you will also need to prefix BABEL_ENV=node to the above command.


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!


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.