Skip to content

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

@hi-ogawa

Description

@hi-ogawa

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    feat: ssrp2-edge-caseBug, but has workaround or limited in scope (priority)

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions