you can define node env in your script. based on that you switch on the plugins
ex in development you don't need to minify the code. but in production it should be minified. and you don't need HOT LOADER in production and vice versa. so it's better to define the node ENV variables and based on it do the switch.
this is how you define it.
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify('production')
})
based on this env you can have more/less plugins to act on your codebase
this is my common webpackconfig based on env.
below is my reactjs config condition check. you can apply simler concept to Native
const env=process.env.NODE_ENV;
const isProd = (env.toLowerCase().trim()=== 'production' || env.toLowerCase().trim() === 'staging')? true: false;
if (isProd) {
//config.plugins will have all common plugin for both dev and prod
Array.prototype.push.apply(config.plugins, [
new webpack.optimize.UglifyJsPlugin({ sourceMap: true }),
new webpack.optimize.OccurrenceOrderPlugin(),
new webpack.optimize.AggressiveMergingPlugin(),
new webpack.optimize.CommonsChunkPlugin({
name: 'vendors',
minChunks(module) {
const { context } = module;
if (typeof context !== 'string') {
return false;
}
return context.indexOf('node_modules') !== -1;
},
}),
new webpack.optimize.CommonsChunkPlugin({
name: 'common',
minChunks(module, count) {
return count >= 2;
},
}),
new CompressionPlugin({
asset: '[path].gz[query]',
algorithm: 'gzip',
test: /\.(js|html)$/,
threshold: 10240,
minRatio: 0.8,
}),
new webpack.LoaderOptionsPlugin({
minimize: true,
debug: false,
noInfo: true,
options: {
context: './',
},
}),
new HtmlWebpackPlugin({
title: 'React App',
filename: 'index.html',
template: './src/index.ejs',
favicon: './src/favicon.ico',
minify: {
removeComments: true,
collapseWhitespace: true,
removeRedundantAttributes: true,
useShortDoctype: true,
removeEmptyAttributes: true,
removeStyleLinkTypeAttributes: true,
keepClosingSlash: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true,
},
inject: true,
}),
]);
} else {
config.entry.splice(1, 0, 'react-hot-loader/patch');
Array.prototype.push.apply(config.plugins, [
new webpack.HotModuleReplacementPlugin(),
new webpack.NamedModulesPlugin(),
new HtmlWebpackPlugin({
title: 'React App',
filename: 'index.html',
template: './src/index.ejs',
inject: true,
favicon: './src/favicon.ico',
}),
new webpack.LoaderOptionsPlugin({
minimize: false,
debug: true,
noInfo: false,
options: {
context: './',
},
}),
]);
}