From 40b670f7b89ed4ac1f3e7aade0962084d243da34 Mon Sep 17 00:00:00 2001 From: Dunqing Date: Fri, 22 Sep 2023 17:38:30 +0800 Subject: [PATCH 1/7] test: test case ready --- playground/ssr/__tests__/ssr.spec.ts | 6 ++++++ playground/ssr/src/app.js | 6 ++++++ playground/ssr/src/circular-import/a.js | 5 +++++ playground/ssr/src/circular-import/b.js | 5 +++++ playground/ssr/src/circular-import/index.js | 5 +++++ 5 files changed, 27 insertions(+) create mode 100644 playground/ssr/src/circular-import/a.js create mode 100644 playground/ssr/src/circular-import/b.js create mode 100644 playground/ssr/src/circular-import/index.js diff --git a/playground/ssr/__tests__/ssr.spec.ts b/playground/ssr/__tests__/ssr.spec.ts index 2d62a7079feecb..680fb1b6822f70 100644 --- a/playground/ssr/__tests__/ssr.spec.ts +++ b/playground/ssr/__tests__/ssr.spec.ts @@ -12,6 +12,12 @@ test(`circular dependencies modules doesn't throw`, async () => { ) }) +test.only(`circular import doesn't throw`, async () => { + await page.goto(`${url}/circular-import`) + + expect(await page.textContent('.circ-import')).toMatchInlineSnapshot() +}) + test(`deadlock doesn't happen`, async () => { await page.goto(`${url}/forked-deadlock`) diff --git a/playground/ssr/src/app.js b/playground/ssr/src/app.js index 5e10dfe45937e3..b151504d973401 100644 --- a/playground/ssr/src/app.js +++ b/playground/ssr/src/app.js @@ -3,6 +3,7 @@ import { escapeHtml } from './utils' const pathRenderers = { '/': renderRoot, '/circular-dep': renderCircularDep, + '/circular-import': renderCircularImport, '/forked-deadlock': renderForkedDeadlock, } @@ -34,6 +35,11 @@ async function renderCircularDep(rootDir) { return `
${escapeHtml(getValueAB())}
` } +async function renderCircularImport(rootDir) { + const { logA } = await import('./circular-import/index.js') + return `
${escapeHtml(logA())}
` +} + async function renderForkedDeadlock(rootDir) { const { commonModuleExport } = await import('./forked-deadlock/common-module') commonModuleExport() diff --git a/playground/ssr/src/circular-import/a.js b/playground/ssr/src/circular-import/a.js new file mode 100644 index 00000000000000..fba792a5d70956 --- /dev/null +++ b/playground/ssr/src/circular-import/a.js @@ -0,0 +1,5 @@ +import { getB } from './b' + +export const A = '__A__' + +console.log(`B is: ${getB()}`) diff --git a/playground/ssr/src/circular-import/b.js b/playground/ssr/src/circular-import/b.js new file mode 100644 index 00000000000000..c0e732b29e2035 --- /dev/null +++ b/playground/ssr/src/circular-import/b.js @@ -0,0 +1,5 @@ +export function getB() { + return '__B__' +} + +export { A } from './a' diff --git a/playground/ssr/src/circular-import/index.js b/playground/ssr/src/circular-import/index.js new file mode 100644 index 00000000000000..3e08d3432fdd04 --- /dev/null +++ b/playground/ssr/src/circular-import/index.js @@ -0,0 +1,5 @@ +import { A } from './b' + +export function logA() { + console.log(`A is: ${A}`) +} From a89134849f3acd28d818eda0f0b1672f8c4285ac Mon Sep 17 00:00:00 2001 From: Dunqing Date: Fri, 22 Sep 2023 20:10:50 +0800 Subject: [PATCH 2/7] fix(ssr): crash on circular import --- .../node/ssr/__tests__/ssrTransform.spec.ts | 70 +++++++++---------- packages/vite/src/node/ssr/ssrTransform.ts | 23 +++--- playground/ssr/__tests__/ssr.spec.ts | 6 +- playground/ssr/src/circular-import/a.js | 2 +- playground/ssr/src/circular-import/index.js | 2 +- 5 files changed, 49 insertions(+), 54 deletions(-) diff --git a/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts b/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts index c8810edc6d2b3d..575fcb0f763a84 100644 --- a/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts +++ b/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts @@ -104,9 +104,9 @@ test('export * from', async () => { ).toMatchInlineSnapshot(` "const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\"); __vite_ssr_exportAll__(__vite_ssr_import_0__); + const __vite_ssr_import_1__ = await __vite_ssr_import__(\\"react\\"); __vite_ssr_exportAll__(__vite_ssr_import_1__); - " `) }) @@ -132,8 +132,7 @@ 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\\"); + "const __vite_ssr_import_1__ = await __vite_ssr_import__(\\"vue\\"); __vite_ssr_exportAll__(__vite_ssr_import_1__); " `) @@ -145,8 +144,8 @@ test('hoist import to top', async () => { `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');" + "__vite_ssr_import_0__.default.resolve('server.js');const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"node:path\\"); + " `) }) @@ -383,8 +382,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() }) {} @@ -405,8 +404,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' } = {} @@ -428,8 +427,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 = {} @@ -471,8 +470,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() { @@ -521,8 +520,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' @@ -553,8 +552,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; @@ -585,8 +584,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' @@ -631,8 +630,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() { @@ -677,8 +676,8 @@ test('jsx', async () => { 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_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)); @@ -828,8 +827,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) { @@ -855,8 +854,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]; @@ -887,8 +886,8 @@ for (const test in tests) { console.log(test) }`), ).toMatchInlineSnapshot(` - "const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"./test.js\\"); - + " + const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"./test.js\\"); for (const test of tests) { @@ -919,8 +918,8 @@ const Baz = class extends Foo {} `, ) expect(result?.code).toMatchInlineSnapshot(` - "const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"./foo\\"); - + " + const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"./foo\\"); console.log(__vite_ssr_import_0__.default, __vite_ssr_import_0__.Bar); @@ -940,9 +939,10 @@ test('import assertion attribute', async () => { import('./bar.json', { assert: { type: 'json' } }); `), ).toMatchInlineSnapshot(` - "const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"./foo.json\\"); - - - __vite_ssr_dynamic_import__('./bar.json', { assert: { type: 'json' } }); - "`) + " + const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"./foo.json\\"); + + __vite_ssr_dynamic_import__('./bar.json', { assert: { type: 'json' } }); + " + `) }) diff --git a/packages/vite/src/node/ssr/ssrTransform.ts b/packages/vite/src/node/ssr/ssrTransform.ts index e13e2dec37d624..51f5c507beeff1 100644 --- a/packages/vite/src/node/ssr/ssrTransform.ts +++ b/packages/vite/src/node/ssr/ssrTransform.ts @@ -95,16 +95,11 @@ async function ssrTransformScript( const idToImportMap = new Map() const declaredConst = new Set() - // hoist at the start of the file, after the hashbang - const hoistIndex = code.match(hashbangRE)?.[0].length ?? 0 - - function defineImport(source: string) { + function defineImport(index = 0, 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( - hoistIndex, + index, `const ${importId} = await ${ssrImportKey}(${JSON.stringify(source)});\n`, ) return importId @@ -124,7 +119,7 @@ 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) + const importId = defineImport(node.start, node.source.value as string) s.remove(node.start, node.end) for (const spec of node.specifiers) { if (spec.type === 'ImportSpecifier') { @@ -167,11 +162,10 @@ 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) - // hoist re-exports near the defined import so they are immediately exported + const importId = defineImport(node.start, node.source.value as string) for (const spec of node.specifiers) { defineExport( - hoistIndex, + node.start, spec.exported.name, `${importId}.${spec.local.name}`, ) @@ -217,12 +211,11 @@ async function ssrTransformScript( // export * from './foo' if (node.type === 'ExportAllDeclaration') { s.remove(node.start, node.end) - const importId = defineImport(node.source.value as string) - // hoist re-exports near the defined import so they are immediately exported + const importId = defineImport(node.start, node.source.value as string) if (node.exported) { - defineExport(hoistIndex, node.exported.name, `${importId}`) + defineExport(node.start, node.exported.name, `${importId}`) } else { - s.appendLeft(hoistIndex, `${ssrExportAllKey}(${importId});\n`) + s.appendLeft(node.start, `${ssrExportAllKey}(${importId});\n`) } } } diff --git a/playground/ssr/__tests__/ssr.spec.ts b/playground/ssr/__tests__/ssr.spec.ts index 680fb1b6822f70..8ad88619309b45 100644 --- a/playground/ssr/__tests__/ssr.spec.ts +++ b/playground/ssr/__tests__/ssr.spec.ts @@ -12,10 +12,12 @@ test(`circular dependencies modules doesn't throw`, async () => { ) }) -test.only(`circular import doesn't throw`, async () => { +test(`circular import doesn't throw`, async () => { await page.goto(`${url}/circular-import`) - expect(await page.textContent('.circ-import')).toMatchInlineSnapshot() + expect(await page.textContent('.circ-import')).toMatchInlineSnapshot( + '"A is: __A__"', + ) }) test(`deadlock doesn't happen`, async () => { diff --git a/playground/ssr/src/circular-import/a.js b/playground/ssr/src/circular-import/a.js index fba792a5d70956..00c8645fd78b12 100644 --- a/playground/ssr/src/circular-import/a.js +++ b/playground/ssr/src/circular-import/a.js @@ -2,4 +2,4 @@ import { getB } from './b' export const A = '__A__' -console.log(`B is: ${getB()}`) +export const B = getB() diff --git a/playground/ssr/src/circular-import/index.js b/playground/ssr/src/circular-import/index.js index 3e08d3432fdd04..c9ec0fc50238b4 100644 --- a/playground/ssr/src/circular-import/index.js +++ b/playground/ssr/src/circular-import/index.js @@ -1,5 +1,5 @@ import { A } from './b' export function logA() { - console.log(`A is: ${A}`) + return `A is: ${A}` } From 6ba59304aaa5f3d48cf8f5ff0e5613f23f7bf37b Mon Sep 17 00:00:00 2001 From: Dunqing Date: Fri, 22 Sep 2023 20:18:11 +0800 Subject: [PATCH 3/7] fix: remove dead code --- packages/vite/src/node/ssr/ssrTransform.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/vite/src/node/ssr/ssrTransform.ts b/packages/vite/src/node/ssr/ssrTransform.ts index 51f5c507beeff1..ef4d74e2850a27 100644 --- a/packages/vite/src/node/ssr/ssrTransform.ts +++ b/packages/vite/src/node/ssr/ssrTransform.ts @@ -34,8 +34,6 @@ export const ssrDynamicImportKey = `__vite_ssr_dynamic_import__` export const ssrExportAllKey = `__vite_ssr_exportAll__` export const ssrImportMetaKey = `__vite_ssr_import_meta__` -const hashbangRE = /^#!.*\n/ - export async function ssrTransform( code: string, inMap: SourceMap | { mappings: '' } | null, From 241ed9fe03c5e36f15bfb3d9c569522307730ad1 Mon Sep 17 00:00:00 2001 From: Dunqing Date: Sat, 23 Sep 2023 09:09:30 +0800 Subject: [PATCH 4/7] refactor: hoist imports --- packages/vite/src/node/ssr/ssrTransform.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/vite/src/node/ssr/ssrTransform.ts b/packages/vite/src/node/ssr/ssrTransform.ts index ef4d74e2850a27..7dc7776a23abb8 100644 --- a/packages/vite/src/node/ssr/ssrTransform.ts +++ b/packages/vite/src/node/ssr/ssrTransform.ts @@ -34,6 +34,8 @@ export const ssrDynamicImportKey = `__vite_ssr_dynamic_import__` export const ssrExportAllKey = `__vite_ssr_exportAll__` export const ssrImportMetaKey = `__vite_ssr_import_meta__` +const hashbangRE = /^#!.*\n/ + export async function ssrTransform( code: string, inMap: SourceMap | { mappings: '' } | null, @@ -93,6 +95,9 @@ async function ssrTransformScript( const idToImportMap = new Map() const declaredConst = new Set() + // hoist at the start of the file, after the hashbang + const hoistIndex = code.match(hashbangRE)?.[0].length ?? 0 + function defineImport(index = 0, source: string) { deps.add(source) const importId = `__vite_ssr_import_${uid++}__` @@ -117,7 +122,7 @@ 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.start, node.source.value as string) + const importId = defineImport(hoistIndex, node.source.value as string) s.remove(node.start, node.end) for (const spec of node.specifiers) { if (spec.type === 'ImportSpecifier') { From 074e6089eaf163846e79c99be2b7ff978919148c Mon Sep 17 00:00:00 2001 From: Dunqing Date: Sat, 23 Sep 2023 09:12:33 +0800 Subject: [PATCH 5/7] test: update --- .../node/ssr/__tests__/ssrTransform.spec.ts | 61 ++++++++++--------- 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts b/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts index 575fcb0f763a84..94618e8e86742c 100644 --- a/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts +++ b/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts @@ -132,7 +132,8 @@ test('export then import minified', async () => { `export * from 'vue';import {createApp} from 'vue';`, ), ).toMatchInlineSnapshot(` - "const __vite_ssr_import_1__ = await __vite_ssr_import__(\\"vue\\"); + "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__); " `) @@ -144,8 +145,8 @@ test('hoist import to top', async () => { `path.resolve('server.js');import path from 'node:path';`, ), ).toMatchInlineSnapshot(` - "__vite_ssr_import_0__.default.resolve('server.js');const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"node:path\\"); - " + "const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"node:path\\"); + __vite_ssr_import_0__.default.resolve('server.js');" `) }) @@ -382,8 +383,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() }) {} @@ -404,8 +405,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' } = {} @@ -427,8 +428,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 = {} @@ -470,8 +471,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() { @@ -520,8 +521,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' @@ -552,8 +553,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; @@ -584,8 +585,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' @@ -630,8 +631,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() { @@ -676,9 +677,9 @@ test('jsx', async () => { 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\\"); + 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)); } @@ -827,8 +828,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) { @@ -854,8 +855,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]; @@ -886,8 +887,8 @@ for (const test in tests) { console.log(test) }`), ).toMatchInlineSnapshot(` - " - const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"./test.js\\"); + "const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"./test.js\\"); + for (const test of tests) { @@ -918,8 +919,8 @@ const Baz = class extends Foo {} `, ) expect(result?.code).toMatchInlineSnapshot(` - " - const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"./foo\\"); + "const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"./foo\\"); + console.log(__vite_ssr_import_0__.default, __vite_ssr_import_0__.Bar); @@ -939,9 +940,9 @@ test('import assertion attribute', async () => { import('./bar.json', { assert: { type: 'json' } }); `), ).toMatchInlineSnapshot(` - " - const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"./foo.json\\"); + "const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"./foo.json\\"); + __vite_ssr_dynamic_import__('./bar.json', { assert: { type: 'json' } }); " `) From c3bb8a768a2937562a2f98170c6eeb9420fffbba Mon Sep 17 00:00:00 2001 From: Dunqing Date: Tue, 26 Sep 2023 20:44:29 +0800 Subject: [PATCH 6/7] test: update snap --- packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts b/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts index 1604fa76c65cdd..3d1295d3937ac7 100644 --- a/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts +++ b/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts @@ -964,14 +964,14 @@ console.log(foo + 2) `), ).toMatchInlineSnapshot(` "const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"./foo\\"); - 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 + 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) " From df08ae87c275a1e7e8bc813aedd80b1e215534dd Mon Sep 17 00:00:00 2001 From: Dunqing Date: Fri, 6 Oct 2023 19:56:16 -0500 Subject: [PATCH 7/7] Update packages/vite/src/node/ssr/ssrTransform.ts Co-authored-by: Bjorn Lu --- packages/vite/src/node/ssr/ssrTransform.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vite/src/node/ssr/ssrTransform.ts b/packages/vite/src/node/ssr/ssrTransform.ts index 7dc7776a23abb8..817ddb3ccd341e 100644 --- a/packages/vite/src/node/ssr/ssrTransform.ts +++ b/packages/vite/src/node/ssr/ssrTransform.ts @@ -98,7 +98,7 @@ async function ssrTransformScript( // hoist at the start of the file, after the hashbang const hoistIndex = code.match(hashbangRE)?.[0].length ?? 0 - function defineImport(index = 0, source: string) { + function defineImport(index: number, source: string) { deps.add(source) const importId = `__vite_ssr_import_${uid++}__` s.appendLeft(