Skip to content

Commit 1c102d5

Browse files
fix(ssr): fix semicolon injection by ssr transform (#19097)
Co-authored-by: Hiroshi Ogawa <[email protected]>
1 parent 677508b commit 1c102d5

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

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

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1285,6 +1285,14 @@ switch (1) {
12851285
f()
12861286
break
12871287
}
1288+
1289+
if(0){}f()
1290+
1291+
if(0){}else{}f()
1292+
1293+
switch(1){}f()
1294+
1295+
{}f(1)
12881296
`),
12891297
).toMatchInlineSnapshot(`
12901298
"
@@ -1346,7 +1354,34 @@ switch (1) {
13461354
x;
13471355
(0,__vite_ssr_import_0__.f)();
13481356
break
1349-
}
1357+
};
1358+
1359+
if(0){};(0,__vite_ssr_import_0__.f)();
1360+
1361+
if(0){}else{};(0,__vite_ssr_import_0__.f)();
1362+
1363+
switch(1){};(0,__vite_ssr_import_0__.f)();
1364+
1365+
{}(0,__vite_ssr_import_0__.f)(1)
13501366
"
13511367
`)
13521368
})
1369+
1370+
test('does not break minified code', async () => {
1371+
// Based on https://unpkg.com/@headlessui/[email protected]/dist/components/transitions/transition.js
1372+
expect(
1373+
await ssrTransformSimpleCode(
1374+
`import O from 'a';
1375+
const c = () => {
1376+
if(true){return}O(1,{})
1377+
}`,
1378+
),
1379+
).toMatchInlineSnapshot(
1380+
`
1381+
"const __vite_ssr_import_0__ = await __vite_ssr_import__("a", {"importedNames":["default"]});
1382+
const c = () => {
1383+
if(true){return};(0,__vite_ssr_import_0__.default)(1,{})
1384+
}"
1385+
`,
1386+
)
1387+
})

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ async function ssrTransformScript(
364364
stmt.type !== 'BlockStatement' &&
365365
stmt.type !== 'ImportDeclaration'
366366
) {
367-
s.appendRight(stmt.end, ';')
367+
s.appendLeft(stmt.end, ';')
368368
}
369369
}
370370
},

0 commit comments

Comments
 (0)