Imagine I have a rich application, which uses a lot of the 3rd party modules including lodash, moment, axios, react.
If I bundle everything in one bundle at the end the size would be higher then 1MB.
I want webpack to pack my libraries in the way, that it stores:
- lodash separately
- moment separately
- all the other node_modules are stored under the vendor bundle
- the code of the application is stored in the separate file
I tried to play with the CommonsChunkPlugin in different ways, but never received the result I want.
I've prepared the example repository to illustrate my issue: https://github.com/PavelPolyakov/webpack-react-bundles
Webpack entry part (conf/webpack.base.config.js):
entry: {
lodash: 'lodash',
moment: 'moment',
app: [
'react-hot-loader/patch',
'webpack-dev-server/client?http://localhost:3001',
'webpack/hot/only-dev-server',
'./app/index.js']
}
Here is production config (conf/webpack.production.config.js), trying to separate modules:
plugins: [
new webpack.DefinePlugin({
'process.env': {
'NODE_ENV': JSON.stringify('production')
}
}),
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor',
minChunks: function (module) {
// this assumes your vendor imports exist in the node_modules directory
return module.context &&
module.context.indexOf('node_modules') !== -1;
}
}),
new webpack.optimize.UglifyJsPlugin({
minimize: true,
compress: {
warnings: true
}
})]
This case, the moment and lodash would still appear in the vendor bundle. I want to have them in the two separate bundles.
Any thoughts appreciated.