feat(new_split_chunks): support reuseExistingChunk
#3000
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Related issue (if exists)
Related to #2991, but I need to add a test case for it in another PR to make sure solving the issue.
Summary
With
reuseExistingChunk: true
, Rspack would try to reuse chunks generated in CodeSplitting.The test case
reuse-existing-chunk-simple
shows that thesplittedFoo
chunk is the same asfoo_js
chunk(generated in CodeSplitting), so we could just reuse thefoo_js
chunk instead of creating a newsplittedFoo
chunk.🤖 Generated by Copilot at 5eff55d
This pull request adds a new option
reuseExistingChunk
to the split chunks plugin, which allows reusing existing chunks with the same modules instead of creating new ones. This option is enabled by default to improve performance and reduce bundle size. The pull request modifies therspack_plugin_split_chunks_new
crate and therspack
package to implement and expose the option. It also enables thelet_chains
feature in therspack_plugin_split_chunks_new
crate.Walkthrough
🤖 Generated by Copilot at 5eff55d
reuseExistingChunk
to the cache group configuration, which controls whether to reuse an existing chunk if it contains the same modules as the current chunk (link, link, link)reuseExistingChunk
option to theCacheGroup
andModuleGroup
structs, which represent the cache group and the module group in the plugin logic (link, link, link)let_chains
feature in therspack_plugin_split_chunks_new
crate, which allows usinglet
expressions in the condition of anif
orwhile
statement (link)std::borrow::Cow
to create a borrowed or owned reference to the chunks that the module group is related to (link)get_corresponding_chunk
method in theplugin.rs
file, which finds or creates a new chunk for a module group, to check if there is a reusable chunk and reuse it if thereuseExistingChunk
option is true (link, link)find_the_best_reusable_chunk
in theplugin.rs
file, which searches for the best existing chunk that can be reused for a module group based on the chunk name and the modules (link)reuseExistingChunk
option totrue
when converting aSplitChunksCacheGroup
to aRawSplitChunksCacheGroup
(link)