universal-webpack icon indicating copy to clipboard operation
universal-webpack copied to clipboard

Add option to skip dynamic chunks

Open neocoder opened this issue 5 years ago • 12 comments

This PR adds an option to filter non-initial chunks that are loaded dynamically.

neocoder avatar Nov 04 '19 10:11 neocoder

Hi. I can see README.md got replaced entirely. Can you reduce it to a small diff?

catamphetamine avatar Nov 04 '19 16:11 catamphetamine

Sure. It's fixed now. I think VSCode replaced line endings the first time.

neocoder avatar Nov 05 '19 08:11 neocoder

@neocoder So, if expressed in a more specific way, what would the effect of the effect skipDynamicChunks option be? "This PR adds an option to filter non-initial chunks that are loaded dynamically" is not descriptive enough.

catamphetamine avatar Nov 05 '19 10:11 catamphetamine

@catamphetamine Yeah, maybe it was not descriptive enough. Maybe you can help with a better description? Basically, when you use Webpack's dynamic import() statements it generates two types of chunks: initial chunks ( that must be loaded with the page ) and dynamic chunks ( that will be loaded by Webpack dynamically ). Before the PR, filename_info() function collected all JS chunks ( including dynamic ones ) that later ended up in the initial HTML page. This broke the idea of loading part of the code dynamically when it's actually needed. So skipDynamicChunks option filters the dynamic chunks from the initial page HTML. I wasn't sure if the fix could possibly break some other code, so I decided to make it as an option to leave current behavior intact.

neocoder avatar Nov 05 '19 11:11 neocoder

@neocoder So, supposed you have a webpack-chunks.json file with both "initial" and "dynamic" chunk filenames. How do they end up included on a page though? They're just filenames, as far as I see, not the actual chunks content. Unless you include them manually via <script/> I guess they won't be included. Or provide a proof of otherwise.

catamphetamine avatar Nov 05 '19 11:11 catamphetamine

@catamphetamine yes, they are included manually into the page with the tags but there's no info on whether the chunk is initial or dynamic in the JSON file. You can't differentiate them in the current code. The other option to fix the issue might be to include info on the type of the chunk into the JSON file and I think it's just a matter of opinion on whether dynamic chunks must be included in the JSON or not. If the purpose of JSON is to specify chunks that must be included in the page then, I guess not. It's up to you to decide. If you like the other solution better, I can make another PR. I don't want to leave my fork, which I don't have time to maintain, in production on this project :)

neocoder avatar Nov 05 '19 11:11 neocoder

@neocoder

I think it's just a matter of opinion on whether dynamic chunks must be included in the JSON or not. If the purpose of JSON is to specify chunks that must be included in the page then, I guess not.

Your point's valid: "dynamic" chunks shouldn't be part of the list. But how exactly do they end up included in your project? This library doesn't include anything, it just provides the list of files.

catamphetamine avatar Nov 05 '19 11:11 catamphetamine

@catamphetamine In your original code, there was an iteration on the json.assetsByChunkName and this list includes both initial and dynamic chunks. It's the json.chunks object which includes the info on whether the chunk is initial or dynamic. Not sure why Webpack authors decided to keep it this way.

neocoder avatar Nov 05 '19 12:11 neocoder

@neocoder Can you be more specific? What code exactly does iterate json.assetsByChunkName? Where did you get it?

catamphetamine avatar Nov 05 '19 12:11 catamphetamine

@catamphetamine I don't want to be rude, but did you see the actual code in the PR? I'm not quite following what else is missing in my description. There's an issue and there's a solution.

neocoder avatar Nov 05 '19 12:11 neocoder

@neocoder There's a possibility that your application may be doing something incorrectly, so to rule out such possibility first an examination is required.

catamphetamine avatar Nov 05 '19 13:11 catamphetamine

@catamphetamine Unfortunately, I cannot share the code with you but I can assure you that it does everything like in the docs. It calls the chunks() function which is passed to the main server function and then creates a script and link tags for assets returned by that function.

neocoder avatar Nov 05 '19 13:11 neocoder