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
CommonsChunkPlugin
is not there. –
Consumption You can use the following config:
splitChunks {
vendor: {
name: 'vendor',
chunks: 'all',
test: /node_modules/
}
}
node_modules
from both pages. –
Egis © 2022 - 2024 — McMap. All rights reserved.
preload.js
contain? Can you create a reproducable minimal example on git..? – Commonlypreload.js
should bepage1.js
,index.js
should bepage2.js
. Sorry about the confusing names. Whatpage1.js
contains shouldn't affect anything here. – Zephaniahlodash
in your sample repo. With the current config it extractslodash
which resides undernode_modules
into a speparate output file namedpage1_vendor.build
. This file contains the shared deplodash
. And you want to extractlodash
for page1 into a separate file (current config), but for page2 you wantlodash
to be part ofpage2
? Is this what you want? – CommonlysplitChunks
section. – Commonly