Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack 4 Code splitting generating separate vendor files for each route

I am trying webpack 4 (4.0.1) with code splitting. I am using dynamic loading to load React components.The react components, in turn, are importing components from internal npm modules. For instance, In my app I have the following routes.

<Route exact path="/" component={Home} />
<Route path="/about" component={About} />
<Route path="/topics" component={Topics} />

In each Home, About and Topics components, I am importing a component from my internal npm module( let us say int-home, int-about etc ).

export { App as default } from 'int-about';

Now with this setup, webpack is spitting out extra vendor bundles corresponding to each dynamic importBundle Analyser Details

What could be possibly wrong with my webpack config? How can I make sure that single vendor bundle is churned out in my build? Below is the webpack config for my main app.

const path = require('path');
const webpack = require('webpack');
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;

module.exports = {
    entry :'./src/index.js',
    output : {
        filename: '[name].js',
        chunkFilename: '[name].js',
        path: path.resolve(__dirname, 'dist'),
        publicPath:'dist/',
        library : '',
        libraryTarget:'umd'
    },
    resolve:{ 
        extensions: ['.', '.js', '.jsx']
    },
    mode : process.env.NODE_ENV == 'production' ? 'production' : 'development',
    module : {
        rules : [
            { 
                test: /\.css$/, 
                use: ExtractTextPlugin.extract({
                    fallback: 'style-loader',
                    use: {
                        loader:'css-loader',
                        options:{
                            minimize : true,
                            sourceMap: true,
                            modules:true,
                            localIdentName: '--[hash:base64:8]'
                        }
                    }
                })
            },
            { test: /\.jsx?$/, use: 'babel-loader' }     
        ]
    },
    optimization:{
        splitChunks:{
            cacheGroups:{
                vendor: {
                    chunks: 'initial',
                    test: path.resolve(__dirname, 'node_modules'),
                    name: 'vendors',
                    enforce: true,
                },
            }, 
        }
    },
    plugins:[
        new ExtractTextPlugin({
            filename:"[name].css",
            allChunks:true
        }),
        new webpack.EnvironmentPlugin({
            NODE_ENV: process.env.NODE_ENV,
        }),
        new BundleAnalyzerPlugin({
            analyzerMode : 'static'
        })
    ]
}
like image 484
pratyush jha Avatar asked Mar 05 '18 10:03

pratyush jha


People also ask

Can webpack split code into separate files?

Code splitting is one of the most compelling features of webpack. This feature allows you to split your code into various bundles which can then be loaded on demand or in parallel.

How does code splitting work webpack?

It does so by scanning your JavaScript code from an entry point (usually the index. js file) and then following the import statements that are written in that entry point. Webpack will repeat the process until it has all the files and modules needed by your web application.

What are 4 core concept of webpack?

There are four basic concepts in webpack: entry , output , modules and plug-ins . These configurations are added in webpack.

Does code splitting reduce bundle size?

Code Splitting is an efficient way to reduce your bundle size: it speeds up the loading of your application and reduces the payload size of your application. Bundling is great, but as your app grows, your bundle will grow too.


1 Answers

Taken from this gist:

https://gist.github.com/sokra/1522d586b8e5c0f5072d7565c2bee693#configurate-cache-groups

splitChunks: {
    cacheGroups: {
        commons: {
            test: /[\\/]node_modules[\\/]/,
            name: "vendors",
            chunks: "all"
        }
    }
}
like image 157
Sudarshan Gaikaiwari Avatar answered Sep 28 '22 02:09

Sudarshan Gaikaiwari