Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack how to build production code and how to use it

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.

  1. Still these files are heavy(same as development version)
  2. How to use these files? Currently I am using webpack-dev-server to run the application.

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]   } }; 
like image 428
Gilson PJ Avatar asked Jan 28 '16 06:01

Gilson PJ


People also ask

Is webpack used in production?

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.

What does webpack production mode do?

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 .


2 Answers

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.

like image 151
sandeep Avatar answered Sep 21 '22 20:09

sandeep


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!

like image 37
Gilson PJ Avatar answered Sep 19 '22 20:09

Gilson PJ