Skip to content
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

chore: revert "fix(ssr): hoist import statements to the top (#12274)" #12527

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 31 additions & 42 deletions packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ test('export * from', async () => {
),
).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
const __vite_ssr_import_1__ = await __vite_ssr_import__(\\"react\\");
__vite_ssr_exportAll__(__vite_ssr_import_0__);
const __vite_ssr_import_1__ = await __vite_ssr_import__(\\"react\\");
__vite_ssr_exportAll__(__vite_ssr_import_1__);"
`)
})
Expand All @@ -130,20 +130,9 @@ test('export then import minified', async () => {
`export * from 'vue';import {createApp} from 'vue';`,
),
).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
const __vite_ssr_import_1__ = await __vite_ssr_import__(\\"vue\\");
__vite_ssr_exportAll__(__vite_ssr_import_1__);"
`)
})

test('hoist import to top', async () => {
expect(
await ssrTransformSimpleCode(
`path.resolve('server.js');import path from 'node:path';`,
),
).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"node:path\\");
__vite_ssr_import_0__.default.resolve('server.js');"
"const __vite_ssr_import_1__ = await __vite_ssr_import__(\\"vue\\");
__vite_ssr_exportAll__(__vite_ssr_import_1__);const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
"
`)
})

Expand Down Expand Up @@ -380,8 +369,8 @@ function c({ _ = bar() + foo() }) {}
`,
),
).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"foo\\");

"
const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"foo\\");

const a = ({ _ = __vite_ssr_import_0__.foo() }) => {}
function b({ _ = __vite_ssr_import_0__.bar() }) {}
Expand All @@ -402,8 +391,8 @@ const a = () => {
`,
),
).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"foo\\");

"
const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"foo\\");

const a = () => {
const { type: n = 'bar' } = {}
Expand All @@ -425,8 +414,8 @@ const foo = {}
`,
),
).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"foo\\");

"
const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"foo\\");

const foo = {}

Expand Down Expand Up @@ -468,8 +457,8 @@ objRest()
`,
),
).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");

"
const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");


function a() {
Expand Down Expand Up @@ -518,8 +507,8 @@ const obj = {
`,
),
).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"foo\\");

"
const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"foo\\");


const bar = 'bar'
Expand Down Expand Up @@ -550,8 +539,8 @@ class A {
`,
),
).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");

"
const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");


const add = __vite_ssr_import_0__.add;
Expand Down Expand Up @@ -582,8 +571,8 @@ class A {
`,
),
).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"foo\\");

"
const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"foo\\");


const bar = 'bar'
Expand Down Expand Up @@ -628,8 +617,8 @@ bbb()
`,
),
).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");

"
const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");


function foobar() {
Expand Down Expand Up @@ -673,15 +662,15 @@ test('jsx', async () => {
const result = await transformWithEsbuild(code, id)
expect(await ssrTransformSimpleCode(result.code, '/foo.jsx'))
.toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"react\\");
const __vite_ssr_import_1__ = await __vite_ssr_import__(\\"foo\\");
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"react\\");

const __vite_ssr_import_1__ = await __vite_ssr_import__(\\"foo\\");

function Bar({ Slot: Slot2 = /* @__PURE__ */ __vite_ssr_import_0__.default.createElement(__vite_ssr_import_1__.Foo, null) }) {
return /* @__PURE__ */ __vite_ssr_import_0__.default.createElement(__vite_ssr_import_0__.default.Fragment, null, /* @__PURE__ */ __vite_ssr_import_0__.default.createElement(Slot2, null));
}
"
`)
function Bar({ Slot: Slot2 = /* @__PURE__ */ __vite_ssr_import_0__.default.createElement(__vite_ssr_import_1__.Foo, null) }) {
return /* @__PURE__ */ __vite_ssr_import_0__.default.createElement(__vite_ssr_import_0__.default.Fragment, null, /* @__PURE__ */ __vite_ssr_import_0__.default.createElement(Slot2, null));
}
"
`)
})

test('continuous exports', async () => {
Expand Down Expand Up @@ -812,8 +801,8 @@ function test() {
return [foo, bar]
}`),
).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"foobar\\");

"
const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"foobar\\");

function test() {
if (true) {
Expand All @@ -839,8 +828,8 @@ function test() {
return bar;
}`),
).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"foobar\\");

"
const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"foobar\\");

function test() {
[__vite_ssr_import_0__.foo];
Expand Down
14 changes: 6 additions & 8 deletions packages/vite/src/node/ssr/ssrTransform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,11 @@ async function ssrTransformScript(
const idToImportMap = new Map<string, string>()
const declaredConst = new Set<string>()

function defineImport(source: string) {
function defineImport(node: Node, source: string) {
deps.add(source)
const importId = `__vite_ssr_import_${uid++}__`
// There will be an error if the module is called before it is imported,
// so the module import statement is hoisted to the top
s.appendLeft(
0,
s.appendRight(
node.start,
`const ${importId} = await ${ssrImportKey}(${JSON.stringify(source)});\n`,
)
return importId
Expand All @@ -120,8 +118,8 @@ async function ssrTransformScript(
// import { baz } from 'foo' --> baz -> __import_foo__.baz
// import * as ok from 'foo' --> ok -> __import_foo__
if (node.type === 'ImportDeclaration') {
const importId = defineImport(node.source.value as string)
s.remove(node.start, node.end)
const importId = defineImport(node, node.source.value as string)
for (const spec of node.specifiers) {
if (spec.type === 'ImportSpecifier') {
idToImportMap.set(
Expand Down Expand Up @@ -163,7 +161,7 @@ async function ssrTransformScript(
s.remove(node.start, node.end)
if (node.source) {
// export { foo, bar } from './foo'
const importId = defineImport(node.source.value as string)
const importId = defineImport(node, node.source.value as string)
for (const spec of node.specifiers) {
defineExport(
node.end,
Expand Down Expand Up @@ -212,7 +210,7 @@ async function ssrTransformScript(
// export * from './foo'
if (node.type === 'ExportAllDeclaration') {
s.remove(node.start, node.end)
const importId = defineImport(node.source.value as string)
const importId = defineImport(node, node.source.value as string)
if (node.exported) {
defineExport(node.end, node.exported.name, `${importId}`)
} else {
Expand Down