From 13e2da2dcffeb8e0f102b07503abf6e328c4e922 Mon Sep 17 00:00:00 2001 From: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Date: Fri, 18 Oct 2024 13:27:52 +0900 Subject: [PATCH] fix: hoist second import --- .../node/ssr/__tests__/ssrTransform.spec.ts | 35 ++++++++++++------- packages/vite/src/node/ssr/ssrTransform.ts | 10 ++---- 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts b/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts index 4580d622a4c9a2..4e3c5bf1f612e5 100644 --- a/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts +++ b/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts @@ -444,19 +444,30 @@ test('sourcemap source', async () => { }) test('sourcemap is correct for hoisted imports', async () => { - const map = ( - await ssrTransform( - `\n\n\nimport { foo } from 'vue';`, - null, - 'input.js', - 'export const a = 1 /* */', - ) - )?.map + const code = `\n\n\nconsole.log(foo, bar);\nimport { foo } from 'vue';\nimport { bar } from 'vue2';` + const result = (await ssrTransform(code, null, 'input.js', code))! + + expect(result.code).toMatchInlineSnapshot(` + "const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["foo"]}); + const __vite_ssr_import_1__ = await __vite_ssr_import__("vue2", {"importedNames":["bar"]}); + + - const traceMap = new TraceMap(map as any) + console.log(__vite_ssr_import_0__.foo, __vite_ssr_import_1__.bar); + + " + `) + + const traceMap = new TraceMap(result.map as any) expect(originalPositionFor(traceMap, { line: 1, column: 0 })).toStrictEqual({ source: 'input.js', - line: 4, + line: 5, + column: 0, + name: null, + }) + expect(originalPositionFor(traceMap, { line: 2, column: 0 })).toStrictEqual({ + source: 'input.js', + line: 6, column: 0, name: null, }) @@ -843,8 +854,8 @@ 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", {"importedNames":["default"]}); - const __vite_ssr_import_1__ = await __vite_ssr_import__("foo", {"importedNames":["Foo","Slot"]}); + "const __vite_ssr_import_1__ = await __vite_ssr_import__("foo", {"importedNames":["Foo","Slot"]}); + const __vite_ssr_import_0__ = await __vite_ssr_import__("react", {"importedNames":["default"]}); function Bar({ Slot: Slot2 = /* @__PURE__ */ __vite_ssr_import_0__.default.createElement(__vite_ssr_import_1__.Foo, null) }) { diff --git a/packages/vite/src/node/ssr/ssrTransform.ts b/packages/vite/src/node/ssr/ssrTransform.ts index 147a8ad3dd9fa5..293ec8bbbf1f92 100644 --- a/packages/vite/src/node/ssr/ssrTransform.ts +++ b/packages/vite/src/node/ssr/ssrTransform.ts @@ -98,7 +98,7 @@ async function ssrTransformScript( const declaredConst = new Set() // hoist at the start of the file, after the hashbang - let hoistIndex = hashbangRE.exec(code)?.[0].length ?? 0 + const hoistIndex = hashbangRE.exec(code)?.[0].length ?? 0 function defineImport( index: number, @@ -125,8 +125,6 @@ async function ssrTransformScript( } const metadataStr = metadata ? `, ${JSON.stringify(metadata)}` : '' - // 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.update( importNode.start, importNode.end, @@ -135,14 +133,12 @@ async function ssrTransformScript( )}${metadataStr});\n`, ) + // There will be an error if the module is called before it is imported, + // so the module import statement is hoisted to the top if (importNode.start !== index) { s.move(importNode.start, importNode.end, index) } - if (index === hoistIndex) { - hoistIndex = importNode.end - } - return importId }