Skip to content

fix(html): keep entry script side effects when another importer loads it first - #24135

Merged
sapphi-red merged 1 commit into
mainfrom
fix/html-entry-script-side-effects
Nov 19, 2026
Merged

sapphi-red merged 1 commit into
mainfrom
fix/html-entry-script-side-effects

Conversation

@h-a-n-a

@h-a-n-a h-a-n-a commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

vite:build-html marks each <script type="module" src> entry as having side effects, so that treeshake.moduleSideEffects: false does not drop it. When this.getModuleInfo(id) returns null, it passes the value through this.load({ ...resolved, moduleSideEffects: true }) instead.

In Rolldown, another importer can already be loading the same module while getModuleInfo still returns null. this.load then waits for that load and ignores the moduleSideEffects option, so the entry script can be dropped. Rollup does not have this gap, because a module is visible to getModuleInfo as soon as it is created.

For example:

  • index.html has <script type="module" src="/main.js">.
  • other.js also imports /main.js, and its import is resolved just before the html transform calls getModuleInfo.
  • build.rolldownOptions.treeshake.moduleSideEffects is false.

The output can miss the top-level side effects of main.js.

This PR also sets the value through the module info after this.load. That is how this code worked before the switch to Rolldown (#18411 used the setter; 47a4d50 changed it to the this.load option).

I did not add a test. The bug needs two module loads to interleave at one point, so a normal test would be flaky. I checked it with a local script that forces that order and runs 200 builds:

Vite Rolldown Builds that dropped main.js
main 1.3.2 8 / 200
this PR 1.3.2 0 / 200
this PR rolldown main (57d68ef) 0 / 200

AI disclosure: I used Claude Code to find the cause and to run the timing script.

🤖 Generated with Claude Code

@h-a-n-a
h-a-n-a marked this pull request as ready for review November 18, 2026 10:57
@h-a-n-a
h-a-n-a requested a review from sapphi-red November 18, 2026 10:57
@sapphi-red sapphi-red added feat: html p2-edge-case Bug, but has workaround or limited in scope (priority) feat: build labels Nov 19, 2026

@sapphi-red sapphi-red left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sapphi-red
sapphi-red merged commit ef295d5 into main Nov 19, 2026
44 of 47 checks passed
@sapphi-red
sapphi-red deleted the fix/html-entry-script-side-effects branch November 19, 2026 02:23
h-a-n-a added a commit to rolldown/rolldown that referenced this pull request Nov 23, 2026
…ns (#11792)

This PR makes Rolldown follow Rollup's rules for the `meta` and
`moduleSideEffects` that plugins set on a module. The rules are in
[`internal-docs/module-meta-and-side-effects/design.md`](https://github.com/rolldown/rolldown/blob/align-module-options-with-rollup/internal-docs/module-meta-and-side-effects/design.md),
so please start the review there. The persistent scan cache in #11671
depends on them ([rolldown/cache
0003](rolldown/cache#16)).

## The rules

1. **Creation.** The first resolution of an id that reaches the module
loader creates the module and sets its first `meta` (a top-level copy)
and `moduleSideEffects`. Later resolutions, `this.resolve`, and
`this.load` options for an existing module do not change it.
2. **Hook results.** Each object result of the module's own `load` and
`transform` replaces `moduleSideEffects` when it is not `null` or
`undefined`, and merges `meta` at the top level.
3. **Writes.** Any hook can change the values through
`this.getModuleInfo(id)`. Writing `null` to `moduleSideEffects` clears
it. Writes up to the end of `buildEnd` reach tree shaking. Rolldown
ignores a `moduleSideEffects` write after `buildEnd`, so a later read
and the next build do not see it, and warns about the first one in a
build (`MODULE_SIDE_EFFECTS_WRITE_AFTER_BUILD_END`).
4. **Reading `moduleSideEffects`.** A read returns the decided value, or
else a default value. The order of the defaults is in the design doc.
From the time the module is created, a read never returns `null`.
5. **Lifetime.** A full build starts every module fresh. A partial scan
(HMR, incremental rebuild, lazy compile) starts each changed module
again from the resolution that created it.

```
a.js: resolveId('./x.css') → meta { from: 'a.js' }   first → creates x.css
b.js: resolveId('./x.css') → meta { from: 'b.js' }   x.css exists → not used (before: merged)
```

## What changes for plugins

| Case | Before | After |
| --- | --- | --- |
| A second importer resolves the same id | Merged into the module |
Ignored |
| `this.resolve` | Changed the target module and returned its live
`meta` | Returns only its own resolution: its `meta`, and its hook value
or the default `moduleSideEffects` |
| `this.load` options for an existing module | Merged | Ignored |
| `meta` that a hook returns | Kept as the plugin's object | Kept as a
top-level copy |
| `getModuleInfo(id).moduleSideEffects` before the module is parsed, or
after a write of `null` | `null` | The default value |
| A write in `buildEnd`, or to an external | Changed only JS, or skipped
| Reaches tree shaking |
| A `moduleSideEffects` write after `buildEnd` | No warning | Ignored,
with one warning per build |
| HMR and incremental rebuild | A changed module could lose its values |
Starts again from its creating resolution |
| Lazy compilation | A lazy virtual module could get an empty chunk |
Resolves the original import again |

`moduleSideEffects` now lives only in Rust. `deferSyncScanData` and
`invalidate` are removed.

The default values for an external module and for `treeshake: false`
come from #11422, which is on main.

## Differences from Rollup

- The importer whose module task ends first creates the module (Rollup:
the resolution that ends first).
- When the option and `package.json` both give a value, the option wins.
In Rollup, `@rollup/plugin-node-resolve` returns the `package.json`
value as a hook value, so it wins.
- An external Node.js built-in, such as `node:fs`, reads `false`
(Rollup: `true`).
- A read can return the `package.json` `sideEffects` value.
- Writing `null` clears the value (Rollup stores `null` and treats it as
`false`).
- Writes after `buildEnd` are ignored, and Rolldown warns about the
first one. In Rollup, a later read shows the written value.

## Impact

- Vite needs vitejs/vite#24135 in the same release or before. Without
it, the entry script of `<script type="module" src>` can be dropped when
another importer creates it first. This can already happen today, and
happens more often with this PR.
- `@vitejs/plugin-vue` 6.0.10, `@vitejs/plugin-react` 6.0.2 and several
Vite playgrounds give the same output.

## Performance

Release builds, 10 runs each, M4 Pro (noise about 2%). The output was
byte-identical.

| Build | Change |
| --- | --- |
| Plain builds of 6 benchmark apps | None |
| Vite with `@vitejs/plugin-react`, 5,000 modules | None |
| Stress case, 10,000 modules | 3–5% slower (+45–55 ms of 1.1 s) |

The stress case is not a usual build. Every relative import is resolved
by a JS `resolveId` that returns `meta`, and `moduleSideEffects` of
every module is read in `moduleParsed`, `renderChunk` and a
`codeSplitting.groups` `name` function. The cost comes from:

1. One call into JS per module that a JS `resolveId` creates, so JS can
set the first `meta` (`TaskContext::create_js_module_options`): about 2
µs per module.
2. A call into Rust per read of `moduleSideEffects` in the chunking
context: about 0.6 µs per read.

Possible fixes, not in this PR, are listed in
`internal-docs/cache/implementation.md`.

## Where to look

- JS: `createModuleOptions`, `saveResolutionOptions`,
`resetModuleOptions` in
`packages/rolldown/src/plugin/plugin-context-data.ts`.
- Rust:
`crates/rolldown_common/src/types/module_side_effects_overrides.rs`,
`declared_side_effects_without_hook` in
`crates/rolldown/src/ecmascript/ecma_module_view_factory.rs`,
`try_spawn_new_task` in
`crates/rolldown/src/module_loader/module_loader.rs`.
- Machinery: the JS side cache section of
`internal-docs/cache/implementation.md`.

## Tests

New fixtures under
`packages/rolldown/tests/fixtures/plugin/{context,resolve-id,transform}`.
Rollup's `empty-module-no-treeshake` and
`module-side-effects@resolve-id-external` tests now pass.

## Known gaps

- The synchronous Rust chunking getter cannot call a JS
`treeshake.moduleSideEffects` function.
- A JS `treeshake.moduleSideEffects` function can run two times for one
module; the first result is kept.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Signed-off-by: andrew <andywangsy@gmail.com>
Co-authored-by: 翠 <green@sapphi.red>
phbaer pushed a commit to phbaer/dtvp that referenced this pull request Nov 27, 2026
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [vite](https://vite.dev) ([source](https://github.com/vitejs/vite/tree/HEAD/packages/vite)) | devDependencies | minor | [`8.3.8` → `8.4.0`](https://renovatebot.com/diffs/npm/vite/8.3.8/8.4.0) |

---

### Release Notes

<details>
<summary>vitejs/vite (vite)</summary>

### [`v8.4.0`](https://github.com/vitejs/vite/blob/HEAD/packages/vite/CHANGELOG.md#840-2026-11-26)

[Compare Source](vitejs/vite@v8.3.8...v8.4.0)

##### Features

- add plugin name to hook errors ([#&#8203;24107](vitejs/vite#24107)) ([40475e5](vitejs/vite@40475e5))
- add plugin name to module hook errors ([#&#8203;24151](vitejs/vite#24151)) ([e1e1ac8](vitejs/vite@e1e1ac8))
- add `environment.addSafeModule` ([#&#8203;23974](vitejs/vite#23974)) ([e0fe98a](vitejs/vite@e0fe98a))
- add `server.fs.strict: 'extended'` ([#&#8203;23972](vitejs/vite#23972)) ([e4babd7](vitejs/vite@e4babd7))
- **build:** include worker chunks in manifest ([#&#8203;23699](vitejs/vite#23699)) ([bc6e03c](vitejs/vite@bc6e03c))
- **build:** support retrying failed CSS preloads ([#&#8203;23710](vitejs/vite#23710)) ([2155e12](vitejs/vite@2155e12))
- **cli:** omit `-0` suffix from the first profile file name ([#&#8203;23490](vitejs/vite#23490)) ([c64ba40](vitejs/vite@c64ba40))
- **css:** skip emitting empty CSS files ([#&#8203;24059](vitejs/vite#24059)) ([c5be55f](vitejs/vite@c5be55f))
- **css:** support subpath imports in CSS `url()` ([#&#8203;23869](vitejs/vite#23869)) ([c110663](vitejs/vite@c110663))
- **environment:** expose build manifest on build environment ([#&#8203;23292](vitejs/vite#23292)) ([4db9844](vitejs/vite@4db9844))
- migrate to `dev.sourcemap.css` from `css.devSourcemap` ([#&#8203;23765](vitejs/vite#23765)) ([76cb51b](vitejs/vite@76cb51b))
- **module-runner:** align stacktrace with v8 ([#&#8203;23690](vitejs/vite#23690)) ([bd32fea](vitejs/vite@bd32fea))
- **preview:** serve wasm compressed ([#&#8203;24051](vitejs/vite#24051)) ([39d63b8](vitejs/vite@39d63b8))
- register more references to safe modules ([#&#8203;23168](vitejs/vite#23168)) ([e258ce1](vitejs/vite@e258ce1))
- register references in HTML to safe modules ([#&#8203;23169](vitejs/vite#23169)) ([ef30399](vitejs/vite@ef30399))
- separate `safeModulePaths` per environment ([#&#8203;23973](vitejs/vite#23973)) ([7480c54](vitejs/vite@7480c54))
- show running process for the same project ([#&#8203;23624](vitejs/vite#23624)) ([3e1a86e](vitejs/vite@3e1a86e))
- stabilize `--configLoader` and deprecate `--configLoader=runner` ([#&#8203;23714](vitejs/vite#23714)) ([e719122](vitejs/vite@e719122))
- support bundled dev mode in ssr ([#&#8203;21626](vitejs/vite#21626)) ([5ff255e](vitejs/vite@5ff255e))
- **wasm:** support source phase imports ([#&#8203;22675](vitejs/vite#22675)) ([97bb00f](vitejs/vite@97bb00f))

##### Bug Fixes

- close server on failed start (fix [#&#8203;23965](vitejs/vite#23965)) ([#&#8203;24020](vitejs/vite#24020)) ([c732677](vitejs/vite@c732677))
- **css:** dedupe CSS of inlined common chunks when `cssCodeSplit: false` ([#&#8203;24168](vitejs/vite#24168)) ([1b34d86](vitejs/vite@1b34d86))
- **deps:** update all non-major dependencies ([#&#8203;24190](vitejs/vite#24190)) ([87d8a5a](vitejs/vite@87d8a5a))
- **dev:** register references to safe modules only for `server.fs.strict: 'extended'` ([#&#8203;24206](vitejs/vite#24206)) ([a78fb8a](vitejs/vite@a78fb8a))
- **html:** keep entry script side effects when another importer loads it first ([#&#8203;24135](vitejs/vite#24135)) ([ef295d5](vitejs/vite@ef295d5))
- keep plugin error messages as thrown ([#&#8203;24148](vitejs/vite#24148)) ([24eb35a](vitejs/vite@24eb35a))
- **optimizer:** detect in-place patch file edits in lockfile hash ([#&#8203;24126](vitejs/vite#24126)) ([d7dfe9b](vitejs/vite@d7dfe9b))
- **optimizer:** respect top-level tsconfig option when scanning and bundling deps ([#&#8203;24119](vitejs/vite#24119)) ([760a953](vitejs/vite@760a953))
- preserve drive letter case in `safeRealpathSync` on Windows ([#&#8203;24146](vitejs/vite#24146)) ([3567471](vitejs/vite@3567471))
- **server:** allow closing uninitialized `DevEnvironment` ([#&#8203;24184](vitejs/vite#24184)) ([826dcb8](vitejs/vite@826dcb8))
- **server:** keep `this.addWatchFile` in `load` hook on a module's first request ([#&#8203;23861](vitejs/vite#23861)) ([b516bc2](vitejs/vite@b516bc2))
- **build:** keep `magicString` and `ast` in transform hook meta ([#&#8203;24067](vitejs/vite#24067)) ([c0d590d](vitejs/vite@c0d590d))
- **bundled-dev:** load lazy chunks under `base` ([#&#8203;23798](vitejs/vite#23798)) ([d5c79c7](vitejs/vite@d5c79c7))
- **client:** fallback to waiting on main thread for server start when trusted types are enforced ([#&#8203;21483](vitejs/vite#21483)) ([2dcd391](vitejs/vite@2dcd391))
- **css:** match pure css chunk imports by relative path ([#&#8203;24105](vitejs/vite#24105)) ([76f9618](vitejs/vite@76f9618))
- **css:** no-externalize by default even for SSR ([#&#8203;24075](vitejs/vite#24075)) ([f898888](vitejs/vite@f898888))
- **deps:** update all non-major dependencies ([#&#8203;24103](vitejs/vite#24103)) ([c11a671](vitejs/vite@c11a671))
- **deps:** update rolldown-related dependencies ([#&#8203;24028](vitejs/vite#24028)) ([a1b813b](vitejs/vite@a1b813b))
- **deps:** update rolldown-related dependencies ([#&#8203;24104](vitejs/vite#24104)) ([1404166](vitejs/vite@1404166))
- escape import specifiers ([#&#8203;24074](vitejs/vite#24074)) ([9bf5965](vitejs/vite@9bf5965))
- escape imported name in `transformCjsImport` ([#&#8203;24080](vitejs/vite#24080)) ([0512d78](vitejs/vite@0512d78))

##### Performance Improvements

- **build:** shift source maps for preload rewrites instead of re-tracing them ([#&#8203;24049](vitejs/vite#24049)) ([dbce0c6](vitejs/vite@dbce0c6))
- **build:** skip build import analysis hooks for unaffected chunks ([#&#8203;24150](vitejs/vite#24150)) ([1a06564](vitejs/vite@1a06564))
- **build:** skip terser plugin when terser is not used ([#&#8203;24149](vitejs/vite#24149)) ([3bde8ed](vitejs/vite@3bde8ed))
- use native binary for getting process start time on windows ([#&#8203;24087](vitejs/vite#24087)) ([4bf4f32](vitejs/vite@4bf4f32))

##### Documentation

- replace stale esbuild references in option JSDoc ([#&#8203;24196](vitejs/vite#24196)) ([0a19968](vitejs/vite@0a19968))

##### Miscellaneous Chores

- **css:** fix typo in `preliminaryNameToChunkMap` ([#&#8203;24195](vitejs/vite#24195)) ([3eabee5](vitejs/vite@3eabee5))
- add debug log when falling back to powershell for process start time ([#&#8203;24090](vitejs/vite#24090)) ([880f1cf](vitejs/vite@880f1cf))
- **deps:** update dependency es-module-lexer to v3 ([#&#8203;23603](vitejs/vite#23603)) ([785b0ad](vitejs/vite@785b0ad))

##### Code Refactoring

- **build:** skip license plugin via applyToEnvironment when disabled ([#&#8203;24134](vitejs/vite#24134)) ([3c22583](vitejs/vite@3c22583))
- simplify some conditions ([#&#8203;24125](vitejs/vite#24125)) ([a379269](vitejs/vite@a379269))
- **bundled-dev:** serve the rolldown dev runtime as a single file ([#&#8203;23587](vitejs/vite#23587)) ([69df8ad](vitejs/vite@69df8ad))
- drop unused capture groups in test-only regexes ([#&#8203;24097](vitejs/vite#24097)) ([2decae3](vitejs/vite@2decae3))
- remove `optimizeDeps.rollupOptions` ([#&#8203;23760](vitejs/vite#23760)) ([0651e2c](vitejs/vite@0651e2c))

##### Build System

- look up dts identifier replacements by imported name ([#&#8203;24068](vitejs/vite#24068)) ([f60fac3](vitejs/vite@f60fac3))

##### Beta Changelogs

##### [8.4.0-beta.2](vitejs/vite@v8.4.0-beta.1...v8.4.0-beta.2) (2026-11-25)

See [8.4.0-beta.2 changelog](https://github.com/vitejs/vite/blob/v8.4.0-beta.2/packages/vite/CHANGELOG.md)

##### [8.4.0-beta.1](vitejs/vite@v8.3.8...v8.4.0-beta.1) (2026-11-16)

See [8.4.0-beta.1 changelog](https://github.com/vitejs/vite/blob/v8.4.0-beta.1/packages/vite/CHANGELOG.md)

</details>

---

### Configuration

📅 **Schedule**: (UTC)

- Branch creation
  - At any time (no schedule defined)
- Automerge
  - At any time (no schedule defined)

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Mend Renovate CLI](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0NC4yMzUuMyIsInVwZGF0ZWRJblZlciI6IjQ0LjIzNS4zIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6W119-->

Reviewed-on: https://git.baer.one/phbaer/dtvp/pulls/371
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feat: build feat: html p2-edge-case Bug, but has workaround or limited in scope (priority)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants