Skip to content

Commit a4775bf

Browse files
committed
chore: revert "fix(ssr): hoist import statements to the top (vitejs#12274)"
This reverts commit 33baff5.
1 parent da43936 commit a4775bf

File tree

2 files changed

+37
-50
lines changed

2 files changed

+37
-50
lines changed

packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts

+31-42
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ test('export * from', async () => {
103103
),
104104
).toMatchInlineSnapshot(`
105105
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
106-
const __vite_ssr_import_1__ = await __vite_ssr_import__(\\"react\\");
107106
__vite_ssr_exportAll__(__vite_ssr_import_0__);
107+
const __vite_ssr_import_1__ = await __vite_ssr_import__(\\"react\\");
108108
__vite_ssr_exportAll__(__vite_ssr_import_1__);"
109109
`)
110110
})
@@ -130,20 +130,9 @@ test('export then import minified', async () => {
130130
`export * from 'vue';import {createApp} from 'vue';`,
131131
),
132132
).toMatchInlineSnapshot(`
133-
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
134-
const __vite_ssr_import_1__ = await __vite_ssr_import__(\\"vue\\");
135-
__vite_ssr_exportAll__(__vite_ssr_import_1__);"
136-
`)
137-
})
138-
139-
test('hoist import to top', async () => {
140-
expect(
141-
await ssrTransformSimpleCode(
142-
`path.resolve('server.js');import path from 'node:path';`,
143-
),
144-
).toMatchInlineSnapshot(`
145-
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"node:path\\");
146-
__vite_ssr_import_0__.default.resolve('server.js');"
133+
"const __vite_ssr_import_1__ = await __vite_ssr_import__(\\"vue\\");
134+
__vite_ssr_exportAll__(__vite_ssr_import_1__);const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
135+
"
147136
`)
148137
})
149138

@@ -380,8 +369,8 @@ function c({ _ = bar() + foo() }) {}
380369
`,
381370
),
382371
).toMatchInlineSnapshot(`
383-
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"foo\\");
384-
372+
"
373+
const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"foo\\");
385374
386375
const a = ({ _ = __vite_ssr_import_0__.foo() }) => {}
387376
function b({ _ = __vite_ssr_import_0__.bar() }) {}
@@ -402,8 +391,8 @@ const a = () => {
402391
`,
403392
),
404393
).toMatchInlineSnapshot(`
405-
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"foo\\");
406-
394+
"
395+
const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"foo\\");
407396
408397
const a = () => {
409398
const { type: n = 'bar' } = {}
@@ -425,8 +414,8 @@ const foo = {}
425414
`,
426415
),
427416
).toMatchInlineSnapshot(`
428-
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"foo\\");
429-
417+
"
418+
const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"foo\\");
430419
431420
const foo = {}
432421
@@ -468,8 +457,8 @@ objRest()
468457
`,
469458
),
470459
).toMatchInlineSnapshot(`
471-
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
472-
460+
"
461+
const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
473462
474463
475464
function a() {
@@ -518,8 +507,8 @@ const obj = {
518507
`,
519508
),
520509
).toMatchInlineSnapshot(`
521-
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"foo\\");
522-
510+
"
511+
const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"foo\\");
523512
524513
525514
const bar = 'bar'
@@ -550,8 +539,8 @@ class A {
550539
`,
551540
),
552541
).toMatchInlineSnapshot(`
553-
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
554-
542+
"
543+
const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
555544
556545
557546
const add = __vite_ssr_import_0__.add;
@@ -582,8 +571,8 @@ class A {
582571
`,
583572
),
584573
).toMatchInlineSnapshot(`
585-
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"foo\\");
586-
574+
"
575+
const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"foo\\");
587576
588577
589578
const bar = 'bar'
@@ -628,8 +617,8 @@ bbb()
628617
`,
629618
),
630619
).toMatchInlineSnapshot(`
631-
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
632-
620+
"
621+
const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
633622
634623
635624
function foobar() {
@@ -673,15 +662,15 @@ test('jsx', async () => {
673662
const result = await transformWithEsbuild(code, id)
674663
expect(await ssrTransformSimpleCode(result.code, '/foo.jsx'))
675664
.toMatchInlineSnapshot(`
676-
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"react\\");
677-
const __vite_ssr_import_1__ = await __vite_ssr_import__(\\"foo\\");
665+
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"react\\");
678666
667+
const __vite_ssr_import_1__ = await __vite_ssr_import__(\\"foo\\");
679668
680-
function Bar({ Slot: Slot2 = /* @__PURE__ */ __vite_ssr_import_0__.default.createElement(__vite_ssr_import_1__.Foo, null) }) {
681-
return /* @__PURE__ */ __vite_ssr_import_0__.default.createElement(__vite_ssr_import_0__.default.Fragment, null, /* @__PURE__ */ __vite_ssr_import_0__.default.createElement(Slot2, null));
682-
}
683-
"
684-
`)
669+
function Bar({ Slot: Slot2 = /* @__PURE__ */ __vite_ssr_import_0__.default.createElement(__vite_ssr_import_1__.Foo, null) }) {
670+
return /* @__PURE__ */ __vite_ssr_import_0__.default.createElement(__vite_ssr_import_0__.default.Fragment, null, /* @__PURE__ */ __vite_ssr_import_0__.default.createElement(Slot2, null));
671+
}
672+
"
673+
`)
685674
})
686675

687676
test('continuous exports', async () => {
@@ -812,8 +801,8 @@ function test() {
812801
return [foo, bar]
813802
}`),
814803
).toMatchInlineSnapshot(`
815-
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"foobar\\");
816-
804+
"
805+
const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"foobar\\");
817806
818807
function test() {
819808
if (true) {
@@ -839,8 +828,8 @@ function test() {
839828
return bar;
840829
}`),
841830
).toMatchInlineSnapshot(`
842-
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"foobar\\");
843-
831+
"
832+
const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"foobar\\");
844833
845834
function test() {
846835
[__vite_ssr_import_0__.foo];

packages/vite/src/node/ssr/ssrTransform.ts

+6-8
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,11 @@ async function ssrTransformScript(
9494
const idToImportMap = new Map<string, string>()
9595
const declaredConst = new Set<string>()
9696

97-
function defineImport(source: string) {
97+
function defineImport(node: Node, source: string) {
9898
deps.add(source)
9999
const importId = `__vite_ssr_import_${uid++}__`
100-
// There will be an error if the module is called before it is imported,
101-
// so the module import statement is hoisted to the top
102-
s.appendLeft(
103-
0,
100+
s.appendRight(
101+
node.start,
104102
`const ${importId} = await ${ssrImportKey}(${JSON.stringify(source)});\n`,
105103
)
106104
return importId
@@ -120,8 +118,8 @@ async function ssrTransformScript(
120118
// import { baz } from 'foo' --> baz -> __import_foo__.baz
121119
// import * as ok from 'foo' --> ok -> __import_foo__
122120
if (node.type === 'ImportDeclaration') {
123-
const importId = defineImport(node.source.value as string)
124121
s.remove(node.start, node.end)
122+
const importId = defineImport(node, node.source.value as string)
125123
for (const spec of node.specifiers) {
126124
if (spec.type === 'ImportSpecifier') {
127125
idToImportMap.set(
@@ -163,7 +161,7 @@ async function ssrTransformScript(
163161
s.remove(node.start, node.end)
164162
if (node.source) {
165163
// export { foo, bar } from './foo'
166-
const importId = defineImport(node.source.value as string)
164+
const importId = defineImport(node, node.source.value as string)
167165
for (const spec of node.specifiers) {
168166
defineExport(
169167
node.end,
@@ -212,7 +210,7 @@ async function ssrTransformScript(
212210
// export * from './foo'
213211
if (node.type === 'ExportAllDeclaration') {
214212
s.remove(node.start, node.end)
215-
const importId = defineImport(node.source.value as string)
213+
const importId = defineImport(node, node.source.value as string)
216214
if (node.exported) {
217215
defineExport(node.end, node.exported.name, `${importId}`)
218216
} else {

0 commit comments

Comments
 (0)