From 1c102d517de52531faf5765632703977a17de65a Mon Sep 17 00:00:00 2001 From: Alexander Lichter Date: Thu, 2 Jan 2025 20:44:14 +0100 Subject: [PATCH] fix(ssr): fix semicolon injection by ssr transform (#19097) Co-authored-by: Hiroshi Ogawa --- .../node/ssr/__tests__/ssrTransform.spec.ts | 37 ++++++++++++++++++- packages/vite/src/node/ssr/ssrTransform.ts | 2 +- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts b/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts index d7db971e56218c..226e0ef176278e 100644 --- a/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts +++ b/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts @@ -1285,6 +1285,14 @@ switch (1) { f() break } + +if(0){}f() + +if(0){}else{}f() + +switch(1){}f() + +{}f(1) `), ).toMatchInlineSnapshot(` " @@ -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,{}) + }" + `, + ) +}) diff --git a/packages/vite/src/node/ssr/ssrTransform.ts b/packages/vite/src/node/ssr/ssrTransform.ts index 1294ece439f1c4..8c44859bc49129 100644 --- a/packages/vite/src/node/ssr/ssrTransform.ts +++ b/packages/vite/src/node/ssr/ssrTransform.ts @@ -364,7 +364,7 @@ async function ssrTransformScript( stmt.type !== 'BlockStatement' && stmt.type !== 'ImportDeclaration' ) { - s.appendRight(stmt.end, ';') + s.appendLeft(stmt.end, ';') } } },