Skip to content

Commit

Permalink
fix(ssr): fix semicolon injection by ssr transform (#19097)
Browse files Browse the repository at this point in the history
Co-authored-by: Hiroshi Ogawa <hi.ogawa.zz@gmail.com>
  • Loading branch information
TheAlexLichter and hi-ogawa authored Jan 2, 2025
1 parent 677508b commit 1c102d5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
37 changes: 36 additions & 1 deletion packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1285,6 +1285,14 @@ switch (1) {
f()
break
}
if(0){}f()
if(0){}else{}f()
switch(1){}f()
{}f(1)
`),
).toMatchInlineSnapshot(`
"
Expand Down Expand Up @@ -1346,7 +1354,34 @@ switch (1) {
x;
(0,__vite_ssr_import_0__.f)();
break
}
};
if(0){};(0,__vite_ssr_import_0__.f)();
if(0){}else{};(0,__vite_ssr_import_0__.f)();
switch(1){};(0,__vite_ssr_import_0__.f)();
{}(0,__vite_ssr_import_0__.f)(1)
"
`)
})

test('does not break minified code', async () => {
// Based on https://unpkg.com/@headlessui/vue@1.7.23/dist/components/transitions/transition.js
expect(
await ssrTransformSimpleCode(
`import O from 'a';
const c = () => {
if(true){return}O(1,{})
}`,
),
).toMatchInlineSnapshot(
`
"const __vite_ssr_import_0__ = await __vite_ssr_import__("a", {"importedNames":["default"]});
const c = () => {
if(true){return};(0,__vite_ssr_import_0__.default)(1,{})
}"
`,
)
})
2 changes: 1 addition & 1 deletion packages/vite/src/node/ssr/ssrTransform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ async function ssrTransformScript(
stmt.type !== 'BlockStatement' &&
stmt.type !== 'ImportDeclaration'
) {
s.appendRight(stmt.end, ';')
s.appendLeft(stmt.end, ';')
}
}
},
Expand Down

0 comments on commit 1c102d5

Please sign in to comment.