I have two entries: page1.js, page2.js.
I don't want to extract shared codes between two entries. I only want to extract node_modules used in page2.js.
How do I achieve this in webpack 4? Thanks.
I have two entries: page1.js, page2.js.
I don't want to extract shared codes between two entries. I only want to extract node_modules used in page2.js.
How do I achieve this in webpack 4? Thanks.
In Webpack 4, you will have to have two webpack.config.js, one for each entry, i.e. you will have to build them separately.
In Webpack 5, you can use chunks() function of SplitChunksPlugin, see the docs:
Alternatively, you may provide a function for more control. The return value will indicate whether to include each chunk.
module.exports = {
//...
optimization: {
splitChunks: {
chunks(chunk) {
// exclude `my-excluded-chunk`
return chunk.name !== 'my-excluded-chunk';
},
},
},
};
From the official docs, having multiple entries will create separate dependency graphs for each entry.
const config = {
entry: {
pageOne: './src/page1.js',
pageTwo: './src/page2.js',
},
output: {
path: path.resolve(__dirname, 'dist')
}
};
Reference: https://webpack.js.org/concepts/entry-points/#multi-page-application
You can use the following config:
splitChunks {
vendor: {
name: 'vendor',
chunks: 'all',
test: /node_modules/
}
}