Repository navigation
Description
Background
Runtime helper demand can become known later than runtime placement. For example, order wrapping can introduce a dependency on __esm after the initial tree-shaking and chunk placement decisions.
In the existing pipeline, runtime merging can happen before order wrapping. New helper demand can then require creating a runtime chunk or extracting an already-merged runtime, updating chunk dependencies, and checking runtime merging again. Conversely, removing an unused runtime later can invalidate an execution order calculated earlier.
The problem extends beyond retaining a few additional helpers: changes to runtime placement affect which chunks load each other, requiring dependent analysis and optimization results to be reconsidered.
Goal
Separate two decisions:
- Where generated code can reference runtime helpers: establish a separate runtime chunk during chunk construction.
- How the runtime appears in the final output: decide whether to remove it, keep it separate, or merge it into a user chunk after all helper demand is known.
The goal is to reduce repeated analysis and optimization caused by late runtime demand overturning earlier placement decisions.
Required runtime behavior
The final output must satisfy two different requirements, depending on runtime side effects:
- With side effects: runtime initialization must run first on every execution path through the generated output and complete before any user code executes. This guarantee applies even to paths that never reference a runtime symbol; it is not limited to direct helper consumers.
- Without side effects: runtime values must be initialized and available when used. The runtime does not need to execute before unrelated user code or load eagerly just because it exists.
These requirements apply whether the runtime remains a separate chunk or is merged into a user chunk. Import removal and merging must preserve the applicable requirement.
Side effects are determined from the plugin-transformed runtime and its retained dependencies. The unmodified runtime is assumed to be side-effect-free, but a plugin can make runtime initialization observable even when no helper is referenced.
Ensuring runtime executes first
For runtime with side effects, we cover both cases:
- Separate runtime chunk (default): For a synchronous runtime with no module dependencies, place its import before all other static dependencies in every other chunk. ESM evaluation then runs runtime first.
- Merged runtime: Check the resulting execution order from every entry. Accept the merge only if runtime initialization completes before any user code executes. Otherwise, adjust and recheck the candidate, or keep runtime separate. Reject a merge that requires repeating order wrapping.
Proposed pipeline
1. Reserve a separate runtime chunk
Construct a runtime chunk even when no runtime statement is included yet. Every other chunk initially imports it first, including chunks created later in the pipeline.
This is the starting arrangement for analysis and optimization, not a requirement to emit all of these imports in the final output.
2. Run chunk optimizations and order wrapping with runtime present
All relevant analyses use the load graph containing the runtime chunk and its initial imports. Ordinary chunk optimizations keep the runtime separate; runtime merging is handled in its own later stage.
When generated code needs another helper, retain that helper and its runtime-local dependencies and add the corresponding symbol references. Because the consuming chunk already imports runtime, this runtime-local demand does not introduce a new chunk-loading edge or require reconsidering user-code inclusion.
3. Complete helper demand, then optimize runtime
Runtime optimization runs after order wrapping and every other step that can introduce helper demand, including entry generation and output-format glue.
Under the proposed removal rule:
- If a chunk references no runtime symbols and runtime has no side effects, remove that chunk's initial runtime import.
- If no other chunk references runtime symbols and runtime has no side effects, remove the runtime chunk and its initial imports entirely.
- If runtime has side effects, absence of symbol references does not justify removing its imports or the runtime itself. Preserve the execution-before-user-code guarantee on every path.
Then consider merging any retained runtime into a suitable user chunk, using the remaining imports, including bare imports retained for side effects. If no safe merge is available, keep runtime separate.
4. Validate changes and finalize output
Import removal and runtime merging are checked against the same shared load graph, which is updated after each accepted change.
A merge must preserve user-code loading and execution, as well as the applicable runtime requirement: execution first on every path for an effectful runtime, or initialized values before use for a side-effect-free runtime. Putting runtime code first in a host chunk's body is insufficient if that host loads user code before its body runs.
Reject a merge that would require repeating order wrapping. Compute final output ordering, names, and hashes after runtime optimization. Rendering introduces no new runtime demand.
Expected benefit and remaining work
Runtime participates in analysis in a separate chunk that generated code can reference, while its final output form is decided only after demand is complete. Adding runtime-local helpers changes retained runtime code and symbol bindings; final removal and merging happen before final output metadata is calculated.
Load-graph updates are still necessary. The intended saving is avoiding repeated placement decisions, analysis, and optimization caused by discovering runtime demand after runtime was already removed or merged. Actual performance gains require implementation and measurement.
This proposal defines the required behavior. Proving the removal rule and enforcing the execution guarantee for plugin-modified runtime code remain design work, including initialization that reads mutable state, plugin-added dependencies outside runtime, and asynchronous initialization.
Metadata
Metadata
Assignees
Labels
Type
Fields
Priority
Activity
hyfdev commentedon Sep 8, 2026
I had brief disucssion with @IWANABETHATGUY, he roughtly feels ok with what this issue proposed.