Skip to content

Commit

Permalink
fix: hoist second import
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Oct 18, 2024
1 parent ce5e036 commit 13e2da2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
35 changes: 23 additions & 12 deletions packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
Expand Down Expand Up @@ -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) }) {
Expand Down
10 changes: 3 additions & 7 deletions packages/vite/src/node/ssr/ssrTransform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ async function ssrTransformScript(
const declaredConst = new Set<string>()

// 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,
Expand All @@ -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,
Expand All @@ -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
}

Expand Down

0 comments on commit 13e2da2

Please sign in to comment.