Here\'s my webpack.config.js
var webpack = require(\"webpack\");
module.exports = {
entry: \"./entry.js\",
devtool: \"source-map\",
outp
webpack.config.js:
const webpack = require("webpack");
module.exports = {
entry: {
"bundle": "./entry.js",
"bundle.min": "./entry.js",
},
devtool: "source-map",
output: {
path: "./dist",
filename: "[name].js"
},
plugins: [
new webpack.optimize.UglifyJsPlugin({
include: /\.min\.js$/,
minimize: true
})
]
};
Since Webpack 4, webpack.optimize.UglifyJsPlugin
has been deprecated and its use results in error:
webpack.optimize.UglifyJsPlugin has been removed, please use config.optimization.minimize instead
As the manual explains, the plugin can be replaced with minimize
option. Custom configuration can be provided to the plugin by specifying UglifyJsPlugin
instance:
const webpack = require("webpack");
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
module.exports = {
// ...
optimization: {
minimize: true,
minimizer: [new UglifyJsPlugin({
include: /\.min\.js$/
})]
}
};
This does the job for a simple setup. A more effective solution is to use Gulp together with Webpack and do the same thing in one pass.
You can format your webpack.config.js like this:
var debug = process.env.NODE_ENV !== "production";
var webpack = require('webpack');
module.exports = {
context: __dirname,
devtool: debug ? "inline-sourcemap" : null,
entry: "./entry.js",
output: {
path: __dirname + "/dist",
filename: "library.min.js"
},
plugins: debug ? [] : [
new webpack.optimize.DedupePlugin(),
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.optimize.UglifyJsPlugin({ mangle: false, sourcemap: false }),
],
};'
And then to build it unminified run (while in the project's main directory):
$ webpack
To build it minified run:
$ NODE_ENV=production webpack
Notes:
Make sure that for the unminified version you change the output file name to library.js
and for the minified library.min.js
so they do not overwrite each other.
In my opinion it's a lot easier just to use the UglifyJS tool directly:
npm install --save-dev uglify-js
./dst/bundle.js
file.Add a build
command to your package.json
:
"scripts": {
"build": "webpack && uglifyjs ./dst/bundle.js -c -m -o ./dst/bundle.min.js --source-map ./dst/bundle.min.js.map"
}
npm run build
command.No need to install uglify-js globally, just install it locally for the project.
I found a new solution for this problem.
This uses an array of configuration to enable webpack to build the minified and non-minified version in parallel. This make build faster. No need to run the webpack twice. No need extra plugins. Just webpack.
webpack.config.js
const devConfig = {
mode: 'development',
entry: { bundle: './src/entry.js' },
output: { filename: '[name].js' },
module: { ... },
resolve: { ... },
plugins: { ... }
};
const prodConfig = {
...devConfig,
mode: 'production',
output: { filename: '[name].min.js' }
};
module.exports = (env) => {
switch (env) {
case 'production':
return [devConfig, prodConfig];
default:
return devConfig;
}
};
Running webpack
will only build the non-minified version.
Running webpack --env=production
will build the minified and non-minified version at the same time.
To add another answer, the flag -p
(short for --optimize-minimize
) will enable the UglifyJS with default arguments.
You won't get a minified and raw bundle out of a single run or generate differently named bundles so the -p
flag may not meet your use case.
Conversely the -d
option is short for --debug
--devtool sourcemap
--output-pathinfo
My webpack.config.js omits devtool
, debug
, pathinfo
, and the minmize plugin in favor of these two flags.
You can run webpack twice with different arguments:
$ webpack --minimize
then check command line arguments in webpack.config.js
:
var path = require('path'),
webpack = require('webpack'),
minimize = process.argv.indexOf('--minimize') !== -1,
plugins = [];
if (minimize) {
plugins.push(new webpack.optimize.UglifyJsPlugin());
}
...
example webpack.config.js