Skip to content

wrong execution order with re-export on Vite SSR #19839

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
7 tasks done
hi-ogawa opened this issue Apr 11, 2025 · 0 comments · Fixed by #19841
Closed
7 tasks done

wrong execution order with re-export on Vite SSR #19839

hi-ogawa opened this issue Apr 11, 2025 · 0 comments · Fixed by #19841
Labels
feat: ssr p2-edge-case Bug, but has workaround or limited in scope (priority)

Comments

@hi-ogawa
Copy link
Collaborator

hi-ogawa commented Apr 11, 2025

Describe the bug

Related vitest-dev/vitest#4143 (comment)

  • main.js
export * from './dep1.js';
import './dep2.js';
  • dep1.js
console.log('dep1');
export {};
  • dep2.js
console.log('dep2');
export {};

On Node:

❯ node src/main.js
dep1
dep2

On Vite module runner:

❯ node repro.js
dep2
dep1

SSR transform doesn't hoist __vite_ssr_import__ for re-export. I think hoisting should give more intuitive execution order. The issue is recognized by a following test and probably already discussed in #14468 and #14441.

test('import and export ordering', async () => {
// Given all imported modules logs `mod ${mod}` on execution,
// and `foo` is `bar`, the logging order should be:
// "mod a", "mod foo", "mod b", "bar1", "bar2"
expect(
await ssrTransformSimpleCode(`
console.log(foo + 1)
export * from './a'
import { foo } from './foo'
export * from './b'
console.log(foo + 2)
`),
).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__("./foo", {"importedNames":["foo"]});
console.log(__vite_ssr_import_0__.foo + 1);
const __vite_ssr_import_1__ = await __vite_ssr_import__("./a");__vite_ssr_exportAll__(__vite_ssr_import_1__);
;
const __vite_ssr_import_2__ = await __vite_ssr_import__("./b");__vite_ssr_exportAll__(__vite_ssr_import_2__);
;
console.log(__vite_ssr_import_0__.foo + 2)
"
`)
})

The situation might have changed after #18983, so I'll check what happens if we hoist these now.

Reproduction

https://stackblitz.com/edit/vitejs-vite-ffvhx4qg?file=src%2Fmain.js

Steps to reproduce

Open stackblitz

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 20.19.0 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.8.2 - /usr/local/bin/npm
    pnpm: 8.15.6 - /usr/local/bin/pnpm
  npmPackages:
    vite: ^6.2.2 => 6.2.6

Used Package Manager

npm

Logs

No response

Validations

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: ssr p2-edge-case Bug, but has workaround or limited in scope (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant