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. How to reduce the size of code in production build? I found a sample webpack configurtion file from internet and I configured for my application and I run npm run build
and its started building and it generated some files in ./dist/
directory.
package.json file
{ "name": "MyAPP", "version": "0.1.0", "description": "", "main": "src/server/server.js", "repository": { "type": "git", "url": "" }, "keywords": [ ], "author": "Iam", "license": "MIT", "homepage": "http://example.com", "scripts": { "test": "", "start": "babel-node src/server/bin/server", "build": "rimraf dist && NODE_ENV=production webpack --config ./webpack.production.config.js --progress --profile --colors" }, "dependencies": { "scripts" : "", ... }, "devDependencies": { "scripts" : "", ... } }
webpack.config.js
var path = require('path'); var webpack = require('webpack'); var HtmlWebpackPlugin = require('html-webpack-plugin'); var public_dir = "src/frontend"; var ModernizrWebpackPlugin = require('modernizr-webpack-plugin'); module.exports = { devtool: 'eval-source-map', entry: [ 'webpack-hot-middleware/client?reload=true', path.join(__dirname, public_dir , 'main.js') ], output: { path: path.join(__dirname, '/dist/'), filename: '[name].js', publicPath: '/' }, plugins: [ plugins ], module: { loaders: [loaders] } };
webpack.production.config.js
var path = require('path'); var webpack = require('webpack'); var HtmlWebpackPlugin = require('html-webpack-plugin'); var public_dir = "src/frontend"; var ModernizrWebpackPlugin = require('modernizr-webpack-plugin'); console.log(path.join(__dirname, 'src/frontend' , 'index.html')); module.exports = { devtool: 'eval-source-map', entry: [ 'webpack-hot-middleware/client?reload=true', path.join(__dirname, 'src/frontend' , 'main.js') ], output: { path: path.join(__dirname, '/dist/'), filename: '[name].js', publicPath: '/' }, plugins: [plugins], resolve: { root: [path.resolve('./src/frontend/utils'), path.resolve('./src/frontend')], extensions: ['', '.js', '.css'] }, module: { loaders: [loaders] } };
Webpack v4+ will minify your code by default in production mode . Note that while the TerserPlugin is a great place to start for minification and being used by default, there are other options out there: ClosureWebpackPlugin.
In production mode, Webpack does everything to use the bundle for production. It includes minification of the bundle file and other optimizations. Default value of mode is production .
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With