-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow specific entrypoints in splitChunks.chunks
option
#6717
Comments
It's seems like can filter entry points by pass test option. function filterByEntryPoint(entry){
return function(module, chunks){
for (let i=0;i<chunks.length;i++) {
const chunk = chunks[i];
if (chunk.groupsIterable) {
for (const group of chunk.groupsIterable) {
if (group.getParents()[0] && group.getParents()[0].name === entry) {
return true;
}
}
}
}
return false;
}
} then put the function to test option. splitChunks:{
cacheGroups: {
default: false,
vendors: false,
desktop: {
name: "desktop-common",
minChunks: 3,
chunks: "async",
test: filterByEntryPoint("index")
},
mobile: {
name: "m-common",
minChunks: 3,
chunks: "async",
test: filterByEntryPoint("indexMobile")
}
}
}, |
Nice idea but this doesn't have the same effect as the chunks option. Passing a selector function to chunks makes sense so send a PR. |
Fixed by #6791 |
I so wish the documentation would reflect this change. It's a super important feature. |
is it released already? webpack config throw an error if i use anything other than |
Works for us ( optimization: {
removeAvailableModules: false,
removeEmptyChunks: false,
splitChunks: {
cacheGroups: {
// In dev mode, we want all vendor (node_modules) to go into a chunk,
// so building main.js is faster.
vendors: {
test: /[\\/]node_modules[\\/]/,
name: "vendors",
// Exclude pre-main dependencies going into vendors, as doing so
// will result in webpack only loading pre-main once vendors loaded.
// But pre-main is the one loading vendors.
// Currently undocument feature: https://github.com/webpack/webpack/pull/6791
chunks: chunk => chunk.name !== "pre-main.min"
}
}
}
}, |
Thanks @Izhaki, exactly what I needed! |
@wanglam I think it's better.
|
That post of mine with 4.8.1 is from 2 years ago. |
@Izhaki Still, can you confirm the version? |
I'm pretty sure that the post from two years ago is correct - we were using 4.8.1 then. You can find it here: https://www.npmjs.com/package/webpack/v/4.8.1 Currently we're running 4.40.1 with this:
|
@Izhaki what is the function content of isVendorsModule? |
I really can't remember and no longer have access to the code. As I have been using NextJS for a while now, I haven't done DIY webpack chunk optimisations for years now... |
I realise this ticket is already closed, however since it's so easy to end up reading it by finally inputing certain keywords in google, and since the ticket is quite helpful in understanding how to approach "bespoke" code splitting, I'll just leave here how I ended up configuring the SplitChunksPlugin.js in my particular use case, for the next googler to have an easier time. Use case: Multi-page web app. Multiple entrypoints. Webapp divided into a "site" and an "admin". A desire to split out vendor js code (node_modules) + common webapp code out of entrypoints. A need to get the smallest possible vendor and common "chunk" on the "site" homepage, to satisfy google PageSpeed. Webpack v4.
Comments:
I hope the above saves someone's time when configuring the split plugin beyond the defaults -- which do work on their own quite well, when one has complete control over the js setup. |
Do you want to request a feature or report a bug?
A feature
What is the current behavior?
It would be handy to be able to specify particular entry points here.
If this is a feature request, what is motivation or use case for changing the behavior?
In Storybook, we have two entry points: "manager" mostly contains our library code, and "preview" contains the user code, and is loaded in an iframe. So, for purposes of long-term caching it only makes sense to split vendor chunk in "preview" entry point. Plus, there's currently no easy way to use
HtmlWebpackPlugin
with multiple entry points, each of which has split chunks, see jantimon/html-webpack-plugin#882The text was updated successfully, but these errors were encountered: