Skip to content

fix(rolldown_plugin_vite_build_import_analysis): only wrap dynamic imports with static specifiers in __vitePreload - #10879

Merged
shulaoda merged 6 commits into
rolldown:mainfrom
smeng9:fix/async-preload-thunk
Oct 21, 2026
Merged

shulaoda merged 6 commits into
rolldown:mainfrom
smeng9:fix/async-preload-thunk

Conversation

@smeng9

@smeng9 smeng9 commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

Fixes downstream vitejs/vite#23485

@codspeed

codspeed Bot commented Sep 14, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

⚠️ 4 benchmarks spent significant time in system calls

System calls cannot be consistently instrumented, so they are not included in the measure, which understates the real cost. Please switch to the Walltime instrument to accurately measure system calls.

Measurement and system calls

✅ 7 untouched benchmarks
⏩ 10 skipped benchmarks1


Comparing smeng9:fix/async-preload-thunk (a1d34fa) with main (648591f)2

Open in CodSpeed

Footnotes

  1. 10 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

  2. No successful run was found on main (10eff60) during the generation of this report, so 648591f was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@smeng9 smeng9 changed the title fix: __vitePreload inlined awaited import should be transform with correct syntax fix: __vitePreload inlined awaited import should be transformed with correct syntax Sep 17, 2026
@sapphi-red

sapphi-red commented Sep 24, 2026 •

Copy link
Copy Markdown
Member

I think we should skip wrapping with __vitePreload like v7 if the specifier of the dynamic import is not a static string because Vite / Rolldown won't know what to preload anyway.

If we were to wrap, we have to keep the sideeffect outside of the __vitePreload. The output would have to be like

let _temp = await resolveSpecifier(spec)
return await __vitePreload(() => import(_temp), []);

. Generating this is a bit tricky.

Shaoyu Meng and others added 4 commits October 13, 2026 13:04
A dynamic import whose argument contains an `await` is wrapped as

    __vitePreload(() => import(await resolveUrl()), [...])

The thunk is a *synchronous* arrow, so the `await` is stranded in a
non-async scope and the emitted chunk fails to parse:

    SyntaxError: Unexpected identifier. import call expects one or two
    arguments

Emit `async () => ...` instead. This is validity-preserving for every
argument expression and inert for the common string/identifier case,
since __vitePreload invokes and awaits the thunk immediately. The
destructuring path (construct_vite_preload_call) already builds an async
arrow; this brings the two remaining paths in line.

Not a pre-existing Vite behaviour: Vite 7, which ran this transform in
JS on Rollup, left dynamic imports with non-analyzable specifiers
unwrapped, so nothing could be stranded. Rolldown wraps them too, which
is what surfaces the bug.

The same fix shape was applied to vitest browser modes equivalent thunk
in vitest-dev/vitest#10784.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
The awaited specifier is computed at runtime, so rolldown never emits
lib.js as a chunk and the dynamic import cannot resolve when the built
module is executed. Export the loaders instead of invoking them: simply
importing dist/main.js is the assertion, since a synchronous preload
thunk makes that module a syntax error.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@smeng9
smeng9 force-pushed the fix/async-preload-thunk branch 3 times, most recently from 59a948d to 92b8d23 Compare October 13, 2026 20:41
…static specifier

Match Vite 7: wrap `import()` in `__vitePreload` only when its specifier is a
string or template literal. A computed specifier such as
`import(await resolveSpecifier())` gives nothing to preload, and wrapping it in
the synchronous thunk left the `await` outside an async scope.

Replaces the earlier change that made the thunk `async`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@smeng9
smeng9 force-pushed the fix/async-preload-thunk branch from 92b8d23 to 012dda2 Compare October 13, 2026 20:53
@smeng9

smeng9 commented Oct 13, 2026

Copy link
Copy Markdown
Contributor Author

Hi @sapphi-red @IWANABETHATGUY

I have gotten back to this issue. I have changed to skip wrapping with __vitePreload like v7.

@shulaoda

Copy link
Copy Markdown
Member

Thanks!

@shulaoda shulaoda changed the title fix: __vitePreload inlined awaited import should be transformed with correct syntax fix(rolldown_plugin_vite_build_import_analysis): only wrap dynamic imports with static specifiers in __vitePreload Oct 21, 2026
@shulaoda
shulaoda merged commit 547a5a7 into rolldown:main Oct 21, 2026
33 checks passed
@rolldown-guard rolldown-guard Bot mentioned this pull request Oct 21, 2026
shulaoda added a commit that referenced this pull request Oct 21, 2026
## [1.2.15] - 2026-10-21

### 🚀 Features

- rolldown_plugin_vite_resolve: add `isBundled` to pick the browser-external stub format (#11260) by @h-a-n-a
- dev: mark entry modules in the HMR module graph (#11252) by @h-a-n-a
- mark module starts with `// <id>` comments instead of `#region` pairs (#11235) by @IWANABETHATGUY

### 🐛 Bug Fixes

- plugin: stop `this.resolve` recursion between plugins with `skipSelf: true` (#11181) by @anzal1
- rolldown_plugin_vite_resolve: handle external `require` for `ssr.target: 'webworker'` (#11205) by @sapphi-red
- rolldown_plugin_vite_build_import_analysis: only wrap dynamic imports with static specifiers in __vitePreload (#10879) by @smeng9
- strict-execution-order: wrap eager modules that forward wrapped definers (#11186) by @hyfdev
- chunk-import-map: keep common chunk keys unique when enabling inline common chunks (#11230) by @hyfdev
- only register signal-exit handler when a trace subscriber is active (#11208) by @DaniilIsupov

### 🧪 Testing

- fold hand-unrolled Rust test tables (#11212) by @Brooooooklyn

### ⚙️ Miscellaneous Tasks

- deps: upgrade oxc to 0.155.0 (#11228) by @Dunqing
- rollup-tests: retriage failed tests (#11251) by @sapphi-red
- deps: update rollup submodule for tests to v4.64.12 (#11249) by @rolldown-guard[bot]
- deps: update test262 submodule for tests (#11242) by @rolldown-guard[bot]

### ❤️ New Contributors

* @anzal1 made their first contribution in [#11181](#11181)
* @smeng9 made their first contribution in [#10879](#10879)
* @DaniilIsupov made their first contribution in [#11208](#11208)

Co-authored-by: shulaoda <[email protected]>
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.

3 participants