Repository navigation
fix: report effective moduleSideEffects in getModuleInfo and this.resolve - #11422
Conversation
8fd1bd8 to
38a8e40
Compare
Merging this PR will not alter performance1 warning
Comparing Footnotes
|
…olve Plugin hooks were the only source copied onto ModuleInfo and resolve results, so both APIs stayed null when Rolldown decided side effects from treeshake.moduleSideEffects or package.json. Report the value the bundler applies instead, without changing treeshake behavior.
38a8e40 to
61af0c5
Compare
hyfdev
left a comment
There was a problem hiding this comment.
Thanks for the PR, and sorry for the slow reply.
Reporting the effective value from both APIs is right.
Rollup's this.resolve also fills in treeshake.moduleSideEffects when no hook sets a value (getResolvedIdWithDefaults).
Without a hook value, externals report 'no-treeshake', and so does every module with treeshake: false.
Rollup falls back to true in both cases (test, treeshake: false).
Could these fall back to true too?
reported_module_side_effects repeats the order in normalize_side_effects.
The copy has already drifted: it passes resolved_id.id to treeshake.moduleSideEffects, while main passes raw_id() since #11302.
Could both call one shared function?
When no hook sets moduleSideEffects, getModuleInfo and this.resolve now report true for externals and for builds with treeshake: false, matching Rollup. Inclusion is unchanged. Both paths share module_side_effects_decision, which passes raw_id() into treeshake.moduleSideEffects.
|
thanks @hyfdev! externals and treeshake: false now fall back to true like rollup, and both paths go through one shared fn now so it uses raw_id() too. merged main in as well |
|
Thanks, both points are fixed. Two things are left. CI fails because the
With The plugin returns the A hook
Could a hook |
A hook true used to request statement analysis even with treeshake: false. Plugins that return this.resolve(), such as @rollup/plugin-alias, then dropped the import of a side-effect-free module. That true now counts as no value for inclusion, while getModuleInfo and this.resolve still report true.
|
@hyfdev updated the dev-watch test and a hook true now counts as no value when treeshake is off, added a test for the alias case |
|
I merged |
✅ Deploy Preview for rolldown-rs ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
…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 <[email protected]> Co-authored-by: 翠 <[email protected]>
## [1.3.4] - 2026-11-26 ### 🚀 Features - add builtin `injectPlugin` with `include`/`exclude` (#11290) by @onmax - magic-string: support explicit sourcemap locations (#11884) by @IWANABETHATGUY - introduce `declaredModuleSize` to allow plugins to declare a module's real size (#11844) by @hyfdev - binding: add dropInner() to 12 binding classes and extract release_arc helper (#11855) by @Brooooooklyn - inline-common-chunks: add `maxDuplicatedSize` to keep widely copied chunks as files (#11845) by @mattlewis92 - cli: report when a watch round finishes (#11820) by @hyfdev - rolldown_plugin_vite_build_import_analysis: avoid stripping import attributes when `experimental.importAttributes` is enabled (#11802) by @sapphi-red - import-attributes: warn when a non-ESM output drops the import attributes of an external (#11797) by @shulaoda - warn when the `watch` option is passed to `build()` (#11781) by @shulaoda ### 🐛 Bug Fixes - keep_names: restore hoisted function names before the chunk's modules run (#11962) by @hyfdev - external: don't drop `_/` from relative external imports (#11968) by @hyfdev - external: don't panic on empty output filename with relative externals (#11963) by @hyfdev - use the same path as direct imports for externals re-exported with `export *` (#11964) by @hyfdev - sourcemap: don't panic on empty output filename (#11966) by @hyfdev - sourcemap: treat empty `sourcemapFileNames` as unset (#11967) by @hyfdev - rename clashing names in execution order with `chunkModulesOrder: 'module-id'` (#11943) by @hyfdev - code-splitting: stop counting JSON modules as 0 bytes (#11939) by @hyfdev - fetch changed modules from their creating resolution in incremental rebuilds (#11837) by @h-a-n-a - code-splitting: keep import order through entry re-exports (#11906) by @hyfdev - cjs: keep CommonJS named exports in `export *` of a dynamically imported module (#11835) by @hyfdev - plugin: remove internal keys from plugin and code splitting group contexts (#11914) by @hyfdev - remove the leftover `watch.notify` from the option validator (#11918) by @shulaoda - return the group chunk file from `getFileName()` for a merged emitted entry (#11858) by @IWANABETHATGUY - dev: let `this.load` of an unchanged module finish during a rebuild (#11911) by @hyfdev - code-splitting: avoid an extra shared chunk when an entry only re-exports another entry (#11847) by @hyfdev - treeshake: remove unused objects containing identity function calls (#11852) by @hyfdev - cjs: avoid `ReferenceError` when re-exporting from an external `export *` in another chunk (#11848) by @hyfdev - binding: remove stale WASI workaround in to_binding_error (#11890) by @Brooooooklyn - inline-const: respect `inlineConst` options for default exports (#11851) by @hyfdev - magic-string: keep trailing lines in decoded maps (#11882) by @IWANABETHATGUY - magic-string: preserve column mappings through trim (#11881) by @IWANABETHATGUY - magic-string: map each line of multiline replacements (#11880) by @IWANABETHATGUY - follow Rollup for module meta and moduleSideEffects set by plugins (#11792) by @h-a-n-a - code-splitting: import the bindings a CommonJS entry re-exports from an ES module (#11839) by @hyfdev - cjs: stop overwriting `module.exports` of a CommonJS entry that re-exports an ES module (#11846) by @hyfdev - sourcemap: accept undefined source entries from plugins (#11850) by @fengmk2 - code-splitting: unblock top-level awaited dynamic imports in manual groups (#11819) by @hyfdev - runtime: resolve `require()` from the calling chunk in Node ESM output (#11834) by @hyfdev - plugin: throw instead of panicking when a saved transform context calls `getCombinedSourcemap` (#11838) by @hyfdev - code-splitting: only code-less entries keep side-effect-free static import edges (#11199) by @mattlewis92 - make `getCombinedSourcemap` work with `experimental.nativeMagicString` (#11107) by @IWANABETHATGUY - plugin: avoid hangs when resolveId awaits this.load() (#11824) by @shulaoda - keep template chunk names from overwriting explicit file names (#11831) by @hyfdev - code-splitting: skip redundant re-export chunks (#11817) by @hyfdev - preserve build errors when `buildEnd` fails (#11828) by @sapphi-red - preserve top-level using with topLevelVar (#11804) by @akshathayadav378-boop - treeshake: keep array destructuring that calls an unknown iterator (#11812) by @IWANABETHATGUY - preserve `export` names starting with `*` in ModuleInfo.exports (#11825) by @sapphi-red - treeshake: keep calls to functions that direct `eval` can replace (#11811) by @IWANABETHATGUY - keep each source's content in a multi-source transform sourcemap (#11246) by @IWANABETHATGUY - treeshake: keep calls to empty functions with a rest parameter default (#11810) by @IWANABETHATGUY - plugin: allow this.load() while resolving entries (#11816) by @hyfdev - cli: avoid a WASI hang when watch mode exits normally (#11815) by @shulaoda - report effective moduleSideEffects in getModuleInfo and this.resolve (#11422) by @cestercian - code-splitting: avoid an import cycle through an entry's re-exports (#11803) by @hyfdev - treeshake: apply @__NO_SIDE_EFFECTS__ to functions exported after declaration (#11222) by @araishi - code-splitting: keep unused entry re-exports from loading lazy chunks (#11753) by @hyfdev - code-splitting: keep re-exported bindings out of dynamic import namespaces (#11799) by @hyfdev - perf-kit: Node mode times out under the inspector with `--ready exit` or an anchored `stdout:` pattern (#11416) by @IWANABETHATGUY - treeshaking: drop dead dynamic imports that form a cycle (#11768) by @hyfdev - binding: release the WASI runtime when a callable builtin plugin hook rejects (#11786) by @shulaoda - perf-kit: print real imports in the why chain (#11500) by @IWANABETHATGUY - chunk: name emitted chunks before shared chunks (#11790) by @hyfdev - cjs: forward exports from duplicate CommonJS entries (#11756) by @hyfdev - code-splitting: keep chained emitted entries from becoming empty facades (#11090) by @Huzaifa-Asif ### 💼 Other - wasi: bundle self-contained WASI loaders, staging script, and reactor checks (#11843) by @Brooooooklyn ### 🚜 Refactor - scan: remove an unreachable branch when collecting dynamic import entries (#11919) by @shulaoda - cli: replace the unreachable prototype pollution guard with a test (#11925) by @shulaoda - remove the unused `DeterminedSideEffects::to_module_side_effects` (#11926) by @shulaoda - remove more unused types, functions and re-exports (#11922) by @shulaoda - link_stage: handle required modules only in the pre-pass (#11920) by @shulaoda - cli: remove the leftover `inject` from the CLI options type (#11924) by @shulaoda - rolldown_plugin_vite_build_import_analysis: remove the unused `optimizeModulePreloadRelativePaths` option (#11923) by @shulaoda - remove experimental `freeExternalMemory` (#11886) by @shulaoda - rust: drop direct tokio dependencies from core library crates (#11841) by @Brooooooklyn - rolldown_plugin: move `normalize_transform_sourcemap` to utils (#11247) by @IWANABETHATGUY - remove the unused `plugins_are_slow` method (#11800) by @shulaoda - remove parallel plugin support (#11789) by @h-a-n-a - remove the unused `is_css` sourcemap flag (#11782) by @shulaoda ### 📚 Documentation - rename javascript to JavaScript (#11958) by @mdong1909 - say that resolveId fails the build when the same id is external and not external (#11910) by @h-a-n-a - explain which resolution sets meta and moduleSideEffects in resolveId (#11907) by @h-a-n-a - resolve the import in the `moduleSideEffects` plugin example (#11860) by @hyfdev - clarify side-effect-free module behavior (#11856) by @IWANABETHATGUY - clarify the behavior when `buildEnd` / `renderError` itself errors (#11829) by @sapphi-red - add docs for `code` / `plugin` / `pluginCode` of `RolldownLog` (#11827) by @sapphi-red - explain that a module in an entry chunk runs after the common chunks it imports (#11822) by @hyfdev - magic-string: document `generateDecodedMap` and the segments of `hires: false` maps (#11791) by @IWANABETHATGUY ### ⚡ Performance - binding: return memory to the OS once rolldown is idle (#11655) by @shulaoda - scan: reserve symbol capacity for each module's facade symbols (#11854) by @magurotuna - runtime: stop using bound functions as `__copyProps` getters (#11826) by @magurotuna - replace `IndexVec<ModuleIdx, bool>` with `IndexBitSet` (#11805) by @IWANABETHATGUY - rolldown_plugin_vite_manifest: look up chunk names by file name once per build (#11697) by @Boshen - drop module sourcemaps after the chunk is joined (#11773) by @shulaoda ### 🧪 Testing - magic-string: re-enable two passing compatibility tests (#11879) by @IWANABETHATGUY - node: add standalone runtime/proxy/retained-context tests and vitest binding staging (#11842) by @Brooooooklyn - cli: stop watch CLI tests from hanging on Windows (#11821) by @shulaoda ### ⚙️ Miscellaneous Tasks - delete cancelled pkg.pr.new runs on main and debounce for 15 mins (#11928) by @shulaoda - remove the unused `serde` features of rolldown, rolldown_plugin and rolldown_common (#11927) by @shulaoda - deps: update vite-plus to v1.8.0 in the workspace and CI (#11896) by @camc314 - deps: update rollup submodule for tests to v4.68.8 (#11901) by @rolldown-guard[bot] - deps: update test262 submodule for tests (#11902) by @rolldown-guard[bot] - remove the graphite merge-when-ready label check (#11904) by @shulaoda - deps: pin walkdir to 2.5.0 (#11897) by @hyfdev - deps: remove remove-unused-vars (#11895) by @camc314 - deps: update rust crate testing_macros to v3 (#11876) by @renovate[bot] - deps: update rust crates (#11873) by @renovate[bot] - deps: update npm packages (#11872) by @renovate[bot] - deps: upgrade oxc to 0.160.0 (#11885) by @camc314 - deps: update github actions (#11874) by @renovate[bot] - rust: standalone runtime prep (devtools sync writer, TestDir, 1ms debounce, Wasm sourcemap inline, parking_lot_core patch) (#11840) by @Brooooooklyn - run setup steps one by one on Windows (#11849) by @shulaoda ###◀️ Revert - "feat(binding): add dropInner() to 12 binding classes and extract release_arc helper (#11855)" (#11898) by @shulaoda - "fix(code-splitting): keep chained emitted entries from becoming empty facades (#11090)" (#11891) by @hyfdev ### ❤️ New Contributors * @fengmk2 made their first contribution in [#11850](#11850) * @akshathayadav378-boop made their first contribution in [#11804](#11804) * @araishi made their first contribution in [#11222](#11222) * @Huzaifa-Asif made their first contribution in [#11090](#11090) Co-authored-by: shulaoda <[email protected]>
getModuleInfo(id).moduleSideEffectsandthis.resolve(id).moduleSideEffectsonly reported a value a plugin hook had set, so anything decided bytreeshake.moduleSideEffectsorpackage.jsonsideEffectscame back asnull.They now report the value Rolldown actually applies, in the documented order: hook value, then
treeshake.moduleSideEffects, thenpackage.jsonsideEffects, thentrue. Tree-shaking output can change when a plugin returns thethis.resolve()result as its own, as@rollup/plugin-aliasdoes, because that result now carries the effective value.getModuleInfomatches Rollup'sboolean | 'no-treeshake'.For
this.resolve, it reports the value that the resolved module would be bundled with, which is what the issue asks for. Rollup also reports thetreeshake.moduleSideEffectsvalue there.Added a fixture for #11330 covering package.json
sideEffects: false, the treeshake option, a hook value and the default.Checks run locally:
just build-rolldown, rolldown-teststest:main,cargo testfor rolldown / rolldown_common / rolldown_plugin,cargo clippy --workspace --all-targets -- --deny warnings,cargo fmt --check,vp fmt,cargo ls-lint.Fixes #11330