
Providing the mode configuration option tells webpack to use its built-in optimizations accordingly.
string
Just provide the mode option in the config:
module.exports = {
mode: 'production'
};
or pass it as a CLI argument:
webpack --mode=production
If not set, webpack sets production as the default value for mode. The supported values for mode are:
development
process.env.NODE_ENV with value development. Enables NamedChunksPlugin and NamedModulesPlugin.
production
process.env.NODE_ENV with value production. Enables FlagDependencyUsagePlugin, FlagIncludedChunksPlugin, ModuleConcatenationPlugin, NoEmitOnErrorsPlugin, OccurrenceOrderPlugin, SideEffectsFlagPlugin and UglifyJsPlugin.
none
Please remember that settingNODE_ENVdoesn't automatically setmode.
// webpack.development.config.js
module.exports = {
+ mode: 'development'
- plugins: [
- new webpack.NamedModulesPlugin(),
- new webpack.DefinePlugin({ "process.env.NODE_ENV": JSON.stringify("development") }),
- ]
}
// webpack.production.config.js
module.exports = {
+ mode: 'production',
- plugins: [
- new UglifyJsPlugin(/* ... */),
- new webpack.DefinePlugin({ "process.env.NODE_ENV": JSON.stringify("production") }),
- new webpack.optimize.ModuleConcatenationPlugin(),
- new webpack.NoEmitOnErrorsPlugin()
- ]
}
// webpack.custom.config.js
module.exports = {
+ mode: 'none',
- plugins: [
- ]
}
If you want to change the behavior according the mode variable inside the webpack.config.js you have to export a function instead of an object:
var config = {
entry: './app.js'
//...
};
module.exports = (env, argv) => {
if (argv.mode === 'development') {
config.devtool = 'source-map';
}
if (argv.mode === 'production') {
//...
}
return config;
};