-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Guides - Code splitting seems to miss a basic example #1333
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
Comments
A small comment here, this approach may end up hurting you in certain cases. e.g. if you use some node modules only in a very specific corner case of your application, this will bundle it in your common bundle and you will end up shipping more code than really necessary. Specifying minChunks will just add the most commonly used node modules in this bundle.
|
@prateekbh yeah, I was hoping to find a way to say "put stuff in |
This looks great. Would you like to convert this issue into a PR? |
Sure! @TheLarkInn said he had some nits, but I'm happy to start on a PR if the rough structure is ok. My plan would be to replace "Code Splitting - Libraries" with something structured like above, and rename the article to something like "Code Splitting - JS". |
Only part I would mention is that: When you set In addition many will see
This is the equivalent of:
Which will do the exact same as mentioned but your bootstrap bundle will be called applesauce.[hash].js I feel like these are important to clarify but not sure how to make it flow. |
I'll try and call out the difference between name & names, but I'm not really sure I understand the difference. For instance, I thought: new webpack.optimize.CommonsChunkPlugin({
names: ['common', 'moment']
}) …would behave the same as… new webpack.optimize.CommonsChunkPlugin({
name: 'common'
}),
new webpack.optimize.CommonsChunkPlugin({
name: 'moment'
}) …but the latter throws an error: ERROR in CommonsChunkPlugin: While running in normal mode it's not allowed to use a non-entry chunk (moment). |
Then I'll have to verify this in the source (thankfully we have fully documented that plugin src for reasons like this, where what we currently have documented may vary). Here is what our documentation for this plugin states for each property:
|
@skipjack is actually in the process of rewriting this part of the guides. I'm sure he will take a look at this as you put in a lot of effort and we're trying to make the guides more accessible to everyone, specifically beginners. Thanks for all the hard work! |
Ah cool! I'll hold off on the PR then. Happy to help review the new doc as someone who hit the learning curve pretty hard on this. |
Thank you Jake, we'll make sure we tag you it PR already lands before discussed here. |
@jakearchibald I'll ping you as soon as I have a PR up so we can discuss further. It would be great to have your help on this and other guides as well. |
I'm an unknown third party here but I was reading this and trying to understand it until @TheLarkInn said the following... This seems like a pretty bad way of making an interface. Why not have an explicit parameter to tell whether you're splitting bootstrap or not? Currently it will just cause confusion and weird hard-to-find bugs. If some sort of parameter is not possible then maybe separate bootstrapChunkPlugin? |
FWIW, I wrote this: Covers some of the same ground. |
@twhid great thanks. The PR I'll open shortly actually removes most of the caching-related discussion from the code splitting docs and keeps it focused on the various approaches to split code. All the caching discussion should go in the caching guide instead (which does need some updating, see #652). We're at the point with this site where we've covered most of the missing documentation and now we have to start refining what we have by making the voice/examples a bit more consistent/reader-friendly and replacing any duplicated documentation with well-placed links (e.g. #1258). |
could you guys define whats the unit for minsize? bytes? so, 1000 = 1KB? |
When reading https://webpack.js.org/guides/code-splitting-libraries/ I was trying to figure out how to create a new file based on the common parts of two entry points.
I guess this is technically what the "vendor" example is doing, but as a first-time reader I didn't realise this, because this is in a page specific to "libraries", whereas I was wanting to avoid duplication in code I'd written.
I don't know if this would help people other than me, but I would have understood the guide better if it was structured something like this:
(the following is just showing the order I think things should be introduced, not final copy)
Splitting out common code
If two of your entry points import/require the same modules, you'll end up with that code duplicated in each output file.
CommonsChunkPlugin
is a plugin that can extract common modules:This will generate:
page1.ef0942b9ff95bd25c024.js
- the stuff unique to page1.page2.9de799314948aa4d7686.js
- the stuff unique to page2.common.a7b0b08b0c83bdccebbd.js
- the stuff used in both page1 and page2.Note: The filename of the chunk will inherit the filename of the output.
[name].[chunkhash].js
in this case.page1
won't automatically loadcommon
, you must include both on your page, andcommon
must execute first:Splitting out webpack's bootstrap
Webpack's bootstap code for dealing with importing modules is usually included with every output. However,
CommonsChunkPlugin
will see this duplication and move it into one file,common
in this case.However, since the bootstrap contains details about the other modules, such as their names & hashes, updating the code unique to
page1
will also updatecommon
, sincepage1
's hash will change. Adding apage3
will also updatecommon
since it will be adding details of this new module. This means the user is having to download a freshcommon
even though only a small part of the bootstrap has changed.To solve this, we can extract the bootstrap into its own file:
This will generate:
page1.ef0942b9ff95bd25c024.js
- the stuff unique to page1.page2.9de799314948aa4d7686.js
- the stuff unique to page2.common.a7b0b08b0c83bdccebbd.js
- the stuff used in both page1 and page2.manifest.1302ca8eccf18a208f11.js
- webpack's bootstrap and module manifest.Instances of
CommonsChunkPlugin
are handled in order. Our first instance extracts all the common code tocommon
. The second instance doesn't find any common code (we've already extracted it), but it will generate a file containing the bootstrap, and remove it from the others.Now
manifest
must execute first:Splitting out rarely-updated modules
We've removed all of the duplication, but you may find that a small part of one of your bundles is updating frequently. It's best to extract that, so the rest of the bundle, which perhaps updates infrequently, can cache for longer.
To do this, create an additional entry point for the parts that frequently update (or the parts that infrequently update – the important thing is they're separate):
CommonsChunkPlugin
sees thatfequentlyupdates
is the name of an entry point, so it removes its code from the other modules.Note: I expected two instances of
CommonsChunkPlugin
to work. One forcommon
, then one forfequentlyupdates
, but it throws an error. I don't know if this is a bug with webpack or a bug in my mental model.Splitting out a library
Library modules are typically rarely updated as part of your build. So let's say instead of
fequentlyupdates
, you wanted to extractmoment
, the date formatting library:Here we've replaced the path to the
fequentlyupdates
script with the module name.Splitting out everything in
node_modules
Assuming you update dependencies less-frequently than your application code, you may want to split everything within
node_modules
:The text was updated successfully, but these errors were encountered: