I am using WebPack CommonsChunkPlugin to extract duplicate code and reduce JavaScript code size. I have two html pages and two entries for them. I also added a ReactJs entry. So far in webpack.config.js we have had:
var path = require("path"); var webpack = require('webpack'); var BundleTracker = require('webpack-bundle-tracker'); var BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin; module.exports = { context: __dirname, entry: { react: ["react", "react-dom"], home: './assets/js/home.jsx', about: './assets/js/about.jsx', }, output: { path: path.resolve('./assets/bundles/'), filename: "[name].js", }, plugins: [ new BundleTracker({filename: './webpack-stats.json'}), new webpack.optimize.CommonsChunkPlugin({ name: 'react', minChunks: Infinity }), new BundleAnalyzerPlugin(), ], module: { rules: [ { test: /\.jsx?$/, exclude: /node_modules/, loader: 'babel-loader', options: { plugins: [["lodash", { "id": ["semantic-ui-react"] }]], presets: ["es2015", "react"] } }, ], }, resolve: { modules: ['node_modules', 'bower_components'], extensions: ['*', '.js', '.jsx'] }, };
This configuration result using webpack-bundle-analyzer :

As you can see, there are several duplicates of the code, some in the red area and some others in the green area. I want to extract these js codes from home and about bundles in a separate package. To extract the red zone code, namely lodash , I added these lines to the webpack configuration:
new webpack.optimize.CommonsChunkPlugin({ name: 'lodash', minChunks: function(module, count) { return module.context.indexOf('node_modules/lodash') >= 0; } }),
But it does not work as expected, and the lodash library code is still in both the home and bundles, and webpack creates a package called lodash, which is almost empty and does not contain the js library.
Any idea on how to fix it? How about highlighting green codes?