Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack duplicates packages in bundle

Here is the output from bundle-analyzer:

enter image description here

As you can see packages as react-dom, jquery and mobx are both in shell bundle and vendor bundle. Is it possible to put them only in vendor bundle?

UPDATE Here is config file:

entry: {
    shell: './src/shell.ts',
    vendor: [
      'jquery',
      'react',
      'react-dom',
      'react-router',
      'mobx',
      'mobx-react',
      'toastr',
      'styled-components',
    ],
  },
  output: {
    path: __dirname + '/dist',
    filename: '[name]bundle.js?[hash:8]',
    publicPath: '/',
  },
  devtool: PROD ? false : 'source-map',
  resolve: {
    extensions: ['.webpack.js', '.web.js', '.ts', '.tsx', '.js', '.json'],
    modules: ['node_modules'],
  },
  optimization: {
    minimize: !!PROD,
  },
  plugins: [
    new CleanWebpackPlugin(['dist']),
    new webpack.DefinePlugin({
      'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
    }),
    new ExtractTextPlugin('[name].css?[hash:8]', {
      allChunks: true,
      disable: !PROD,
    }),
    new HtmlWebpackPlugin({
      chunksSortMode: 'dependency',
      template: './src/index.tpl.html',
    }),
    new BundleAnalyzerPlugin(),
  ],
  
like image 874
stahlhammer Avatar asked Apr 22 '26 18:04

stahlhammer


1 Answers

You can split all your vendor code comming from the node_modules folder into a single vender bundle file using splitChunks optimization webpack setting.

Firstly, remove vendor form your entry.

Then, add the below code to your config file:

optimization: {
    splitChunks: {
        cacheGroups: {
            vendors: {
                test: /[\\/]node_modules[\\/]/ ,
                name: 'vendor' ,
                chunks: 'all' ,
                enforce: true ,
            }
        }
    }
}
like image 100
dhruw lalan Avatar answered Apr 25 '26 11:04

dhruw lalan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!