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.
- Still these files are heavy(same as development version)
- 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]
}
};
You can add the plugins as suggested by @Vikramaditya. Then to generate the production build. You have to run the the command
The
-p
tells webpack to generate a production build. You have to change the build script in package.json to include the production flag.Use these plugins to optimize your production build:
Server side dynamic gzip compression is not recommended for serving static client-side files because of heavy CPU usage.
In addition to Gilson PJ answer:
with
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
and
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,
Then in the package.json file you can configure the build procedure with this production configuration
now you have to run the following command to initiate the 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!This will help you.
If you have a lot of duplicate code in your webpack.dev.config and your webpack.prod.config, you could use a boolean
isProd
to activate certain features only in certain situations and only have a single webpack.config.js file.By the way: The DedupePlugin plugin was removed from Webpack. You should remove it from your configuration.
UPDATE:
In addition to my previous answer:
If you want to hide your code for release, try enclosejs.com. It allows you to:
You can install it with
npm install -g enclose