Skip to content
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 to define minimum chunk size limit #4705

Merged
merged 18 commits into from Nov 12, 2022
Merged

Allow to define minimum chunk size limit #4705

merged 18 commits into from Nov 12, 2022

Conversation

lukastaegert
Copy link
Member

@lukastaegert lukastaegert commented Nov 5, 2022

This PR contains:

  • bugfix
  • feature
  • refactor
  • documentation
  • other

Are tests included?

  • yes (bugfixes and features will not be merged without tests)
  • no

Breaking Changes?

  • yes (breaking changes will not be merged unless absolutely necessary)
  • no

List any relevant issue numbers:

Description

This defines a new option output.experimentalMinChunkSize: number which defines a target lower limit for chunk sizes.

This option is experimental, i.e. it can have breaking changes on minor releases, because I want to be able to release it soon, but I am not yet 100% sure about the final implementation. In the end, it could also become a plugin interface + corresponding plugin, but that would be quite a bit more work. The current implementation already gives an idea what information is needed and what the algorithm could look like.

If a positive integer number is specified for this option, rollup will add an additional phase to the chunk generation where it tries to find a suitable merge target for each chunk that does not have side effects when executed and is below the specified limit.

The chunk size determination is rather inaccurate at the moment, though. It takes tree-shaking into account, but otherwise it looks at the size of the generated code before any plugins like minifiers could do their work.

A suitable merge target is a chunk that is loaded under "similar" conditions based on the entry points that import that chunk. So basically it creates a metric between the chunks and tries to select the closest one. If there are several closest ones, it gives precedence to other chunks that are below the size limit. Chained merges are also possible, i.e. A is merged into B is merged into C etc.

The implementation should be fully functional and sufficiently tested. Still, real world testing would be very welcome. Not that this may not work with Vite 3, i.e. you may need to work against a Rollup 3 branch vitejs/vite#9870 .

Things to look for:

  • Does everything work when the option is used?
  • You could also set the value to Infinity to get the absolute minimal number of chunks 😉
  • Is there any performance impact?
    • For vanilla Rollup, using the --perf option will print special timings for this option in a section optimize chunks

@lukastaegert lukastaegert changed the title Allow to define minimal chunk size Allow to define minimum chunk size limit Nov 5, 2022
@github-actions
Copy link

github-actions bot commented Nov 5, 2022

Thank you for your contribution! ❤️

You can try out this pull request locally by installing Rollup via

npm install rollup/rollup#min-chunk-size

or load it into the REPL:
https://rollupjs.org/repl/?pr=4705

@codecov
Copy link

codecov bot commented Nov 5, 2022

Codecov Report

Merging #4705 (3aad8fd) into master (842ff0d) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master    #4705   +/-   ##
=======================================
  Coverage   99.02%   99.03%           
=======================================
  Files         215      216    +1     
  Lines        7599     7663   +64     
  Branches     2104     2116   +12     
=======================================
+ Hits         7525     7589   +64     
  Misses         24       24           
  Partials       50       50           
Impacted Files Coverage Δ
src/Module.ts 100.00% <ø> (ø)
src/utils/options/mergeOptions.ts 100.00% <ø> (ø)
src/Bundle.ts 100.00% <100.00%> (ø)
src/finalisers/es.ts 100.00% <100.00%> (ø)
src/utils/chunkAssignment.ts 100.00% <100.00%> (ø)
src/utils/iterators.ts 100.00% <100.00%> (ø)
src/utils/options/normalizeOutputOptions.ts 100.00% <100.00%> (ø)
src/utils/resolveIdViaPlugins.ts 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@lukastaegert
Copy link
Member Author

This PR has been released as part of rollup@3.3.0-0. Note that this is a pre-release, so to test it, you need to install Rollup via npm install rollup@3.3.0-0 or npm install rollup@beta. It will likely become part of a regular release later.

@lukastaegert lukastaegert merged commit ff286e5 into master Nov 12, 2022
@lukastaegert lukastaegert deleted the min-chunk-size branch November 12, 2022 05:19
@rollup-bot
Copy link
Collaborator

This PR has been released as part of rollup@3.3.0. You can test it via npm install rollup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Can code splitting be done according to chunk size?
2 participants