Skip to content

New Chunking Design: chunkModulesOrder is done after main chunking process(after order wrapping) #16

Open

Description

@hyfdev

What is chunkModulesOrder?

chunkModulesOrder controls how module code is arranged inside each emitted chunk. The default, exec-order, follows the execution order derived from the module graph; module-id sorts by module ID wherever doing so preserves runtime behavior. Module-ID ordering can improve gzip compression, especially for libraries containing many similar modules, such as icon libraries.

For example, suppose entry.js imports b.js before a.js, and both are independent leaf modules containing only the constant declarations below. Their code can be arranged differently without changing the result:

// exec-order: b.js → a.js → entry.js
const b = 'b';      // b.js
const a = 'a';      // a.js
console.log(a, b);  // entry.js
// module-id: a.js → b.js → entry.js
const a = 'a';      // a.js
const b = 'b';      // b.js
console.log(a, b);  // entry.js

Why sort after order wrapping in the new architecture?

In the new chunking architecture proposed in #10, final module ordering should happen after order wrapping and once chunk membership is settled.

Before wrapping, a module’s position determines when its top-level code runs, so initialization and side effects can restrict its placement. Wrapping moves that work into initializer calls: the order of those calls controls execution, giving the module definitions more freedom to move.

This gives us three reasons to sort afterwards:

  • Reorder more modules. Modules whose side effects previously required a particular position may become movable once their execution is deferred into initializer calls.
  • Make better use of module-id ordering. Sorting after wrapping can use this additional freedom to bring more similar code together, creating more opportunities to improve gzip compression.
  • Sort using the final constraints. The final wrapping result tells the sorter which code still executes eagerly and which definitions must be ready before a call. Sorting preserves those requirements while using the freedom that remains.

Sorting before wrapping is valid, but sorting only at that point leaves the freedom introduced by wrapping unused. Final sorting should use the wrapping result to take advantage of it.

Activity

  1. changed the title [-]Design: `chunkModulesOrder` is done after main chunking process(after order wrapping)[/-] [+]New Chunking Design: `chunkModulesOrder` is done after main chunking process(after order wrapping)[/+] on Sep 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions