Skip to content

Commit 94546be

Browse files
hi-ogawasapphi-red
andauthoredNov 26, 2024··
fix(ssr): replace __vite_ssr_identity__ with (0, ...) and inject ; between statements (#18748)
Co-authored-by: sapphi-red <49056869+sapphi-red@users.noreply.github.com>
1 parent 554f45f commit 94546be

File tree

2 files changed

+247
-112
lines changed

2 files changed

+247
-112
lines changed
 

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

+217-102
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ test('named import', async () => {
2626
`import { ref } from 'vue';function foo() { return ref(0) }`,
2727
),
2828
).toMatchInlineSnapshot(`
29-
"const __vite_ssr_identity__ = v => v;
30-
const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["ref"]});
31-
function foo() { return __vite_ssr_identity__(__vite_ssr_import_0__.ref)(0) }"
29+
"const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["ref"]});
30+
function foo() { return (0,__vite_ssr_import_0__.ref)(0) }"
3231
`)
3332
})
3433

@@ -38,9 +37,8 @@ test('named import: arbitrary module namespace specifier', async () => {
3837
`import { "some thing" as ref } from 'vue';function foo() { return ref(0) }`,
3938
),
4039
).toMatchInlineSnapshot(`
41-
"const __vite_ssr_identity__ = v => v;
42-
const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["some thing"]});
43-
function foo() { return __vite_ssr_identity__(__vite_ssr_import_0__["some thing"])(0) }"
40+
"const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["some thing"]});
41+
function foo() { return (0,__vite_ssr_import_0__["some thing"])(0) }"
4442
`)
4543
})
4644

@@ -121,7 +119,7 @@ test('export * from', async () => {
121119
).toMatchInlineSnapshot(`
122120
"const __vite_ssr_import_0__ = await __vite_ssr_import__("vue");
123121
__vite_ssr_exportAll__(__vite_ssr_import_0__);
124-
122+
;
125123
const __vite_ssr_import_1__ = await __vite_ssr_import__("react");
126124
__vite_ssr_exportAll__(__vite_ssr_import_1__);
127125
"
@@ -153,8 +151,8 @@ test('export as arbitrary module namespace identifier', async () => {
153151
`const something = "Something";export { something as "arbitrary string" };`,
154152
),
155153
).toMatchInlineSnapshot(`
156-
"const something = "Something";
157-
Object.defineProperty(__vite_ssr_exports__, "arbitrary string", { enumerable: true, configurable: true, get(){ return something }});"
154+
"const something = "Something";
155+
Object.defineProperty(__vite_ssr_exports__, "arbitrary string", { enumerable: true, configurable: true, get(){ return something }});"
158156
`)
159157
})
160158

@@ -223,9 +221,8 @@ test('do not rewrite method definition', async () => {
223221
`import { fn } from 'vue';class A { fn() { fn() } }`,
224222
)
225223
expect(result?.code).toMatchInlineSnapshot(`
226-
"const __vite_ssr_identity__ = v => v;
227-
const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["fn"]});
228-
class A { fn() { __vite_ssr_identity__(__vite_ssr_import_0__.fn)() } }"
224+
"const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["fn"]});
225+
class A { fn() { (0,__vite_ssr_import_0__.fn)() } }"
229226
`)
230227
expect(result?.deps).toEqual(['vue'])
231228
})
@@ -387,7 +384,7 @@ test('should declare variable for imported super class', async () => {
387384
).toMatchInlineSnapshot(`
388385
"const __vite_ssr_import_0__ = await __vite_ssr_import__("./dependency", {"importedNames":["Foo"]});
389386
const Foo = __vite_ssr_import_0__.Foo;
390-
class A extends Foo {}
387+
class A extends Foo {};
391388
class B extends Foo {}
392389
Object.defineProperty(__vite_ssr_exports__, "B", { enumerable: true, configurable: true, get(){ return B }});
393390
Object.defineProperty(__vite_ssr_exports__, "default", { enumerable: true, configurable: true, value: A });"
@@ -399,23 +396,23 @@ test('should handle default export variants', async () => {
399396
// default anonymous functions
400397
expect(await ssrTransformSimpleCode(`export default function() {}\n`))
401398
.toMatchInlineSnapshot(`
402-
"__vite_ssr_exports__.default = function() {}
403-
"
404-
`)
399+
"__vite_ssr_exports__.default = function() {}
400+
"
401+
`)
405402
// default anonymous class
406403
expect(await ssrTransformSimpleCode(`export default class {}\n`))
407404
.toMatchInlineSnapshot(`
408-
"__vite_ssr_exports__.default = class {}
409-
"
410-
`)
405+
"__vite_ssr_exports__.default = class {}
406+
"
407+
`)
411408
// default named functions
412409
expect(
413410
await ssrTransformSimpleCode(
414411
`export default function foo() {}\n` +
415412
`foo.prototype = Object.prototype;`,
416413
),
417414
).toMatchInlineSnapshot(`
418-
"function foo() {}
415+
"function foo() {};
419416
foo.prototype = Object.prototype;
420417
Object.defineProperty(__vite_ssr_exports__, "default", { enumerable: true, configurable: true, value: foo });"
421418
`)
@@ -425,7 +422,7 @@ test('should handle default export variants', async () => {
425422
`export default class A {}\n` + `export class B extends A {}`,
426423
),
427424
).toMatchInlineSnapshot(`
428-
"class A {}
425+
"class A {};
429426
class B extends A {}
430427
Object.defineProperty(__vite_ssr_exports__, "B", { enumerable: true, configurable: true, get(){ return B }});
431428
Object.defineProperty(__vite_ssr_exports__, "default", { enumerable: true, configurable: true, value: A });"
@@ -451,25 +448,24 @@ test('sourcemap is correct for hoisted imports', async () => {
451448
const result = (await ssrTransform(code, null, 'input.js', code))!
452449

453450
expect(result.code).toMatchInlineSnapshot(`
454-
"const __vite_ssr_identity__ = v => v;
455-
const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["foo"]});
451+
"const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["foo"]});
456452
const __vite_ssr_import_1__ = await __vite_ssr_import__("vue2", {"importedNames":["bar"]});
457453
458454
459455
460-
console.log(__vite_ssr_identity__(__vite_ssr_import_0__.foo), __vite_ssr_identity__(__vite_ssr_import_1__.bar));
456+
console.log((0,__vite_ssr_import_0__.foo), (0,__vite_ssr_import_1__.bar));
461457
462458
"
463459
`)
464460

465461
const traceMap = new TraceMap(result.map as any)
466-
expect(originalPositionFor(traceMap, { line: 2, column: 0 })).toStrictEqual({
462+
expect(originalPositionFor(traceMap, { line: 1, column: 0 })).toStrictEqual({
467463
source: 'input.js',
468464
line: 5,
469465
column: 0,
470466
name: null,
471467
})
472-
expect(originalPositionFor(traceMap, { line: 3, column: 0 })).toStrictEqual({
468+
expect(originalPositionFor(traceMap, { line: 2, column: 0 })).toStrictEqual({
473469
source: 'input.js',
474470
line: 6,
475471
column: 0,
@@ -536,15 +532,14 @@ test('overwrite bindings', async () => {
536532
`function g() { const f = () => { const inject = true }; console.log(inject) }\n`,
537533
),
538534
).toMatchInlineSnapshot(`
539-
"const __vite_ssr_identity__ = v => v;
540-
const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["inject"]});
541-
const a = { inject: __vite_ssr_import_0__.inject }
542-
const b = { test: __vite_ssr_import_0__.inject }
535+
"const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["inject"]});
536+
const a = { inject: __vite_ssr_import_0__.inject };
537+
const b = { test: __vite_ssr_import_0__.inject };
543538
function c() { const { test: inject } = { test: true }; console.log(inject) }
544-
const d = __vite_ssr_import_0__.inject
545-
function f() { console.log(__vite_ssr_identity__(__vite_ssr_import_0__.inject)) }
539+
const d = __vite_ssr_import_0__.inject;
540+
function f() { console.log((0,__vite_ssr_import_0__.inject)) }
546541
function e() { const { inject } = { inject: true } }
547-
function g() { const f = () => { const inject = true }; console.log(__vite_ssr_identity__(__vite_ssr_import_0__.inject)) }
542+
function g() { const f = () => { const inject = true }; console.log((0,__vite_ssr_import_0__.inject)) }
548543
"
549544
`)
550545
})
@@ -566,13 +561,12 @@ function c({ _ = bar() + foo() }) {}
566561
`,
567562
),
568563
).toMatchInlineSnapshot(`
569-
"const __vite_ssr_identity__ = v => v;
570-
const __vite_ssr_import_0__ = await __vite_ssr_import__("foo", {"importedNames":["foo","bar"]});
564+
"const __vite_ssr_import_0__ = await __vite_ssr_import__("foo", {"importedNames":["foo","bar"]});
571565
572566
573-
const a = ({ _ = __vite_ssr_identity__(__vite_ssr_import_0__.foo)() }) => {}
574-
function b({ _ = __vite_ssr_identity__(__vite_ssr_import_0__.bar)() }) {}
575-
function c({ _ = __vite_ssr_identity__(__vite_ssr_import_0__.bar)() + __vite_ssr_identity__(__vite_ssr_import_0__.foo)() }) {}
567+
const a = ({ _ = (0,__vite_ssr_import_0__.foo)() }) => {};
568+
function b({ _ = (0,__vite_ssr_import_0__.bar)() }) {}
569+
function c({ _ = (0,__vite_ssr_import_0__.bar)() + (0,__vite_ssr_import_0__.foo)() }) {}
576570
"
577571
`)
578572
})
@@ -593,7 +587,7 @@ const a = () => {
593587
594588
595589
const a = () => {
596-
const { type: n = 'bar' } = {}
590+
const { type: n = 'bar' } = {};
597591
console.log(n)
598592
}
599593
"
@@ -615,7 +609,7 @@ const foo = {}
615609
"const __vite_ssr_import_0__ = await __vite_ssr_import__("foo", {"importedNames":["n","m"]});
616610
617611
618-
const foo = {}
612+
const foo = {};
619613
620614
{
621615
const { [__vite_ssr_import_0__.n]: m } = foo
@@ -655,8 +649,7 @@ objRest()
655649
`,
656650
),
657651
).toMatchInlineSnapshot(`
658-
"const __vite_ssr_identity__ = v => v;
659-
const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["remove","add","get","set","rest","objRest"]});
652+
"const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["remove","add","get","set","rest","objRest"]});
660653
661654
662655
@@ -666,22 +659,22 @@ objRest()
666659
a: { b: { c: [ add ] }},
667660
d: [{ get }, set, ...rest],
668661
...objRest
669-
} = foo
662+
} = foo;
670663
671-
remove()
672-
add()
673-
get()
674-
set()
675-
rest()
664+
remove();
665+
add();
666+
get();
667+
set();
668+
rest();
676669
objRest()
677670
}
678671
679-
__vite_ssr_identity__(__vite_ssr_import_0__.remove)()
680-
__vite_ssr_identity__(__vite_ssr_import_0__.add)()
681-
__vite_ssr_identity__(__vite_ssr_import_0__.get)()
682-
__vite_ssr_identity__(__vite_ssr_import_0__.set)()
683-
__vite_ssr_identity__(__vite_ssr_import_0__.rest)()
684-
__vite_ssr_identity__(__vite_ssr_import_0__.objRest)()
672+
(0,__vite_ssr_import_0__.remove)();
673+
(0,__vite_ssr_import_0__.add)();
674+
(0,__vite_ssr_import_0__.get)();
675+
(0,__vite_ssr_import_0__.set)();
676+
(0,__vite_ssr_import_0__.rest)();
677+
(0,__vite_ssr_import_0__.objRest)()
685678
"
686679
`)
687680
})
@@ -710,7 +703,7 @@ const obj = {
710703
711704
712705
713-
const bar = 'bar'
706+
const bar = 'bar';
714707
715708
const obj = {
716709
foo() {},
@@ -774,7 +767,7 @@ class A {
774767
775768
776769
777-
const bar = 'bar'
770+
const bar = 'bar';
778771
779772
class A {
780773
foo() {}
@@ -816,31 +809,30 @@ bbb()
816809
`,
817810
),
818811
).toMatchInlineSnapshot(`
819-
"const __vite_ssr_identity__ = v => v;
820-
const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["aaa","bbb","ccc","ddd"]});
812+
"const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["aaa","bbb","ccc","ddd"]});
821813
822814
823815
824816
function foobar() {
825-
ddd()
817+
ddd();
826818
827819
const aaa = () => {
828-
bbb(ccc)
820+
bbb(ccc);
829821
ddd()
830-
}
822+
};
831823
const bbb = () => {
832824
console.log('hi')
833-
}
834-
const ccc = 1
825+
};
826+
const ccc = 1;
835827
function ddd() {}
836828
837-
aaa()
838-
bbb()
829+
aaa();
830+
bbb();
839831
ccc()
840832
}
841833
842-
__vite_ssr_identity__(__vite_ssr_import_0__.aaa)()
843-
__vite_ssr_identity__(__vite_ssr_import_0__.bbb)()
834+
(0,__vite_ssr_import_0__.aaa)();
835+
(0,__vite_ssr_import_0__.bbb)()
844836
"
845837
`)
846838
})
@@ -862,12 +854,11 @@ test('jsx', async () => {
862854
const result = await transformWithEsbuild(code, id)
863855
expect(await ssrTransformSimpleCode(result.code, '/foo.jsx'))
864856
.toMatchInlineSnapshot(`
865-
"const __vite_ssr_identity__ = v => v;
866-
const __vite_ssr_import_0__ = await __vite_ssr_import__("react", {"importedNames":["default"]});
857+
"const __vite_ssr_import_0__ = await __vite_ssr_import__("react", {"importedNames":["default"]});
867858
const __vite_ssr_import_1__ = await __vite_ssr_import__("foo", {"importedNames":["Foo","Slot"]});
868859
869860
870-
function Bar({ Slot: Slot2 = /* @__PURE__ */ __vite_ssr_import_0__.default.createElement(__vite_ssr_identity__(__vite_ssr_import_1__.Foo), null) }) {
861+
function Bar({ Slot: Slot2 = /* @__PURE__ */ __vite_ssr_import_0__.default.createElement((0,__vite_ssr_import_1__.Foo), null) }) {
871862
return /* @__PURE__ */ __vite_ssr_import_0__.default.createElement(__vite_ssr_import_0__.default.Fragment, null, /* @__PURE__ */ __vite_ssr_import_0__.default.createElement(Slot2, null));
872863
}
873864
"
@@ -887,7 +878,7 @@ export function fn1() {
887878
"
888879
function fn1() {
889880
}
890-
Object.defineProperty(__vite_ssr_exports__, "fn1", { enumerable: true, configurable: true, get(){ return fn1 }});function fn2() {
881+
Object.defineProperty(__vite_ssr_exports__, "fn1", { enumerable: true, configurable: true, get(){ return fn1 }});;function fn2() {
891882
}
892883
Object.defineProperty(__vite_ssr_exports__, "fn2", { enumerable: true, configurable: true, get(){ return fn2 }});
893884
"
@@ -939,9 +930,8 @@ import foo from "foo"`,
939930
),
940931
).toMatchInlineSnapshot(`
941932
"#!/usr/bin/env node
942-
const __vite_ssr_identity__ = v => v;
943933
const __vite_ssr_import_0__ = await __vite_ssr_import__("foo", {"importedNames":["default"]});
944-
console.log(__vite_ssr_identity__(__vite_ssr_import_0__.default));
934+
console.log((0,__vite_ssr_import_0__.default));
945935
"
946936
`)
947937
})
@@ -955,10 +945,9 @@ foo()`,
955945
),
956946
).toMatchInlineSnapshot(`
957947
"#!/usr/bin/env node
958-
const __vite_ssr_identity__ = v => v;
959948
const __vite_ssr_import_0__ = await __vite_ssr_import__("foo", {"importedNames":["foo"]});
960949
961-
__vite_ssr_identity__(__vite_ssr_import_0__.foo)()"
950+
(0,__vite_ssr_import_0__.foo)()"
962951
`)
963952
})
964953

@@ -992,34 +981,33 @@ export class Test {
992981
};`.trim()
993982

994983
expect(await ssrTransformSimpleCode(code)).toMatchInlineSnapshot(`
995-
"const __vite_ssr_identity__ = v => v;
996-
const __vite_ssr_import_0__ = await __vite_ssr_import__("foobar", {"importedNames":["foo","bar"]});
984+
"const __vite_ssr_import_0__ = await __vite_ssr_import__("foobar", {"importedNames":["foo","bar"]});
997985
998986
if (false) {
999-
const foo = 'foo'
987+
const foo = 'foo';
1000988
console.log(foo)
1001989
} else if (false) {
1002-
const [bar] = ['bar']
990+
const [bar] = ['bar'];
1003991
console.log(bar)
1004992
} else {
1005-
console.log(__vite_ssr_identity__(__vite_ssr_import_0__.foo))
1006-
console.log(__vite_ssr_identity__(__vite_ssr_import_0__.bar))
1007-
}
993+
console.log((0,__vite_ssr_import_0__.foo));
994+
console.log((0,__vite_ssr_import_0__.bar))
995+
};
1008996
class Test {
1009997
constructor() {
1010998
if (false) {
1011-
const foo = 'foo'
999+
const foo = 'foo';
10121000
console.log(foo)
10131001
} else if (false) {
1014-
const [bar] = ['bar']
1002+
const [bar] = ['bar'];
10151003
console.log(bar)
10161004
} else {
1017-
console.log(__vite_ssr_identity__(__vite_ssr_import_0__.foo))
1018-
console.log(__vite_ssr_identity__(__vite_ssr_import_0__.bar))
1005+
console.log((0,__vite_ssr_import_0__.foo));
1006+
console.log((0,__vite_ssr_import_0__.bar))
10191007
}
10201008
}
10211009
}
1022-
Object.defineProperty(__vite_ssr_exports__, "Test", { enumerable: true, configurable: true, get(){ return Test }});;"
1010+
Object.defineProperty(__vite_ssr_exports__, "Test", { enumerable: true, configurable: true, get(){ return Test }});;;"
10231011
`)
10241012
})
10251013

@@ -1041,7 +1029,7 @@ function test() {
10411029
function test() {
10421030
if (true) {
10431031
var foo = () => { var why = 'would' }, bar = 'someone'
1044-
}
1032+
};
10451033
return [foo, bar]
10461034
}"
10471035
`)
@@ -1071,7 +1059,7 @@ function test() {
10711059
let foo = 10;
10721060
let bar = 10;
10731061
}
1074-
try {} catch (baz){ baz };
1062+
try {} catch (baz){ baz };;
10751063
return __vite_ssr_import_0__.bar;
10761064
}"
10771065
`)
@@ -1100,11 +1088,11 @@ for (const test in tests) {
11001088
11011089
for (const test of tests) {
11021090
console.log(test)
1103-
}
1091+
};
11041092
11051093
for (let test = 0; test < 10; test++) {
11061094
console.log(test)
1107-
}
1095+
};
11081096
11091097
for (const test in tests) {
11101098
console.log(test)
@@ -1126,16 +1114,15 @@ const Baz = class extends Foo {}
11261114
`,
11271115
)
11281116
expect(result?.code).toMatchInlineSnapshot(`
1129-
"const __vite_ssr_identity__ = v => v;
1130-
const __vite_ssr_import_0__ = await __vite_ssr_import__("./foo", {"importedNames":["default","Bar"]});
1117+
"const __vite_ssr_import_0__ = await __vite_ssr_import__("./foo", {"importedNames":["default","Bar"]});
11311118
11321119
11331120
1134-
console.log(__vite_ssr_identity__(__vite_ssr_import_0__.default), __vite_ssr_identity__(__vite_ssr_import_0__.Bar));
1121+
console.log((0,__vite_ssr_import_0__.default), (0,__vite_ssr_import_0__.Bar));
11351122
const obj = {
11361123
foo: class Foo {},
11371124
bar: class Bar {}
1138-
}
1125+
};
11391126
const Baz = class extends __vite_ssr_import_0__.default {}
11401127
"
11411128
`)
@@ -1171,14 +1158,14 @@ console.log(foo + 2)
11711158
).toMatchInlineSnapshot(`
11721159
"const __vite_ssr_import_0__ = await __vite_ssr_import__("./foo", {"importedNames":["foo"]});
11731160
1174-
console.log(__vite_ssr_import_0__.foo + 1)
1161+
console.log(__vite_ssr_import_0__.foo + 1);
11751162
const __vite_ssr_import_1__ = await __vite_ssr_import__("./a");
11761163
__vite_ssr_exportAll__(__vite_ssr_import_1__);
1177-
1164+
;
11781165
11791166
const __vite_ssr_import_2__ = await __vite_ssr_import__("./b");
11801167
__vite_ssr_exportAll__(__vite_ssr_import_2__);
1181-
1168+
;
11821169
console.log(__vite_ssr_import_0__.foo + 2)
11831170
"
11841171
`)
@@ -1193,15 +1180,143 @@ export * as bar from './bar'
11931180
console.log(bar)
11941181
`),
11951182
).toMatchInlineSnapshot(`
1196-
"const __vite_ssr_identity__ = v => v;
1197-
const __vite_ssr_import_0__ = await __vite_ssr_import__("./foo", {"importedNames":["foo"]});
1183+
"const __vite_ssr_import_0__ = await __vite_ssr_import__("./foo", {"importedNames":["foo"]});
11981184
11991185
1200-
__vite_ssr_exports__.default = __vite_ssr_identity__(__vite_ssr_import_0__.foo)()
1186+
__vite_ssr_exports__.default = (0,__vite_ssr_import_0__.foo)();
12011187
const __vite_ssr_import_1__ = await __vite_ssr_import__("./bar");
12021188
1203-
Object.defineProperty(__vite_ssr_exports__, "bar", { enumerable: true, configurable: true, get(){ return __vite_ssr_import_1__ }});
1189+
Object.defineProperty(__vite_ssr_exports__, "bar", { enumerable: true, configurable: true, get(){ return __vite_ssr_import_1__ }});;
12041190
console.log(bar)
12051191
"
12061192
`)
12071193
})
1194+
1195+
test('inject semicolon for (0, ...) wrapper', async () => {
1196+
expect(
1197+
await ssrTransformSimpleCode(`
1198+
import { f } from './f'
1199+
1200+
let x = 0;
1201+
1202+
x
1203+
f()
1204+
1205+
if (1)
1206+
x
1207+
f()
1208+
1209+
if (1)
1210+
x
1211+
else
1212+
x
1213+
f()
1214+
1215+
1216+
let y = x
1217+
f()
1218+
1219+
x /*;;*/ /*;;*/
1220+
f()
1221+
1222+
function z() {
1223+
x
1224+
f()
1225+
1226+
if (1) {
1227+
x
1228+
f()
1229+
}
1230+
}
1231+
1232+
let a = {}
1233+
f()
1234+
1235+
let b = () => {}
1236+
f()
1237+
1238+
function c() {
1239+
}
1240+
f()
1241+
1242+
class D {
1243+
}
1244+
f()
1245+
1246+
{
1247+
x
1248+
}
1249+
f()
1250+
1251+
switch (1) {
1252+
case 1:
1253+
x
1254+
f()
1255+
break
1256+
}
1257+
`),
1258+
).toMatchInlineSnapshot(`
1259+
"const __vite_ssr_import_0__ = await __vite_ssr_import__("./f", {"importedNames":["f"]});
1260+
1261+
1262+
1263+
let x = 0;
1264+
1265+
x;
1266+
(0,__vite_ssr_import_0__.f)();
1267+
1268+
if (1)
1269+
x;
1270+
(0,__vite_ssr_import_0__.f)();
1271+
1272+
if (1)
1273+
x
1274+
else
1275+
x;
1276+
(0,__vite_ssr_import_0__.f)();
1277+
1278+
1279+
let y = x;
1280+
(0,__vite_ssr_import_0__.f)();
1281+
1282+
x; /*;;*/ /*;;*/
1283+
(0,__vite_ssr_import_0__.f)();
1284+
1285+
function z() {
1286+
x;
1287+
(0,__vite_ssr_import_0__.f)();
1288+
1289+
if (1) {
1290+
x;
1291+
(0,__vite_ssr_import_0__.f)()
1292+
}
1293+
}
1294+
1295+
let a = {};
1296+
(0,__vite_ssr_import_0__.f)();
1297+
1298+
let b = () => {};
1299+
(0,__vite_ssr_import_0__.f)();
1300+
1301+
function c() {
1302+
}
1303+
(0,__vite_ssr_import_0__.f)();
1304+
1305+
class D {
1306+
}
1307+
(0,__vite_ssr_import_0__.f)();
1308+
1309+
{
1310+
x
1311+
}
1312+
(0,__vite_ssr_import_0__.f)();
1313+
1314+
switch (1) {
1315+
case 1:
1316+
x;
1317+
(0,__vite_ssr_import_0__.f)();
1318+
break
1319+
}
1320+
"
1321+
`)
1322+
})

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

+30-10
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ export const ssrImportKey = `__vite_ssr_import__`
4444
export const ssrDynamicImportKey = `__vite_ssr_dynamic_import__`
4545
export const ssrExportAllKey = `__vite_ssr_exportAll__`
4646
export const ssrImportMetaKey = `__vite_ssr_import_meta__`
47-
const ssrIdentityFunction = `__vite_ssr_identity__`
4847

4948
const hashbangRE = /^#!.*\n/
5049

@@ -331,9 +330,23 @@ async function ssrTransformScript(
331330
}
332331
}
333332

334-
let injectIdentityFunction = false
335333
// 3. convert references to import bindings & import.meta references
336334
walk(ast, {
335+
onStatements(statements) {
336+
// ensure ";" between statements
337+
for (let i = 0; i < statements.length - 1; i++) {
338+
const stmt = statements[i]
339+
if (
340+
code[stmt.end - 1] !== ';' &&
341+
stmt.type !== 'FunctionDeclaration' &&
342+
stmt.type !== 'ClassDeclaration' &&
343+
stmt.type !== 'BlockStatement' &&
344+
stmt.type !== 'ImportDeclaration'
345+
) {
346+
s.appendRight(stmt.end, ';')
347+
}
348+
}
349+
},
337350
onIdentifier(id, parent, parentStack) {
338351
const grandparent = parentStack[1]
339352
const binding = idToImportMap.get(id.name)
@@ -363,11 +376,10 @@ async function ssrTransformScript(
363376
}
364377
} else if (parent.type === 'CallExpression') {
365378
s.update(id.start, id.end, binding)
366-
// wrap with identity function to avoid method binding `this`
379+
// wrap with (0, ...) to avoid method binding `this`
367380
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors#method_binding
368-
s.prependRight(id.start, `${ssrIdentityFunction}(`)
381+
s.prependRight(id.start, `(0,`)
369382
s.appendLeft(id.end, `)`)
370-
injectIdentityFunction = true
371383
} else if (
372384
// don't transform class name identifier
373385
!(parent.type === 'ClassExpression' && id === parent.id)
@@ -386,10 +398,6 @@ async function ssrTransformScript(
386398
},
387399
})
388400

389-
if (injectIdentityFunction) {
390-
s.prependLeft(fileStartIndex, `const ${ssrIdentityFunction} = v => v;\n`)
391-
}
392-
393401
let map = s.generateMap({ hires: 'boundary' })
394402
map.sources = [path.basename(url)]
395403
// needs to use originalCode instead of code
@@ -431,6 +439,7 @@ interface Visitors {
431439
) => void
432440
onImportMeta: (node: Node) => void
433441
onDynamicImport: (node: Node) => void
442+
onStatements: (statements: Node[]) => void
434443
}
435444

436445
const isNodeInPatternWeakSet = new WeakSet<_Node>()
@@ -444,7 +453,7 @@ const isNodeInPattern = (node: _Node): node is Property =>
444453
*/
445454
function walk(
446455
root: Node,
447-
{ onIdentifier, onImportMeta, onDynamicImport }: Visitors,
456+
{ onIdentifier, onImportMeta, onDynamicImport, onStatements }: Visitors,
448457
) {
449458
const parentStack: Node[] = []
450459
const varKindStack: VariableDeclaration['kind'][] = []
@@ -498,6 +507,17 @@ function walk(
498507
return this.skip()
499508
}
500509

510+
// for nodes that can contain multiple statements
511+
if (
512+
node.type === 'Program' ||
513+
node.type === 'BlockStatement' ||
514+
node.type === 'StaticBlock'
515+
) {
516+
onStatements(node.body as Node[])
517+
} else if (node.type === 'SwitchCase') {
518+
onStatements(node.consequent as Node[])
519+
}
520+
501521
// track parent stack, skip for "else-if"/"else" branches as acorn nests
502522
// the ast within "if" nodes instead of flattening them
503523
if (

0 commit comments

Comments
 (0)
Please sign in to comment.