Skip to content

Commit c568778

Browse files
authored
fix(compiler-sfc): avoid gen useCssVars when targeting SSR (#6979)
close #6926
1 parent d8990fc commit c568778

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

packages/compiler-sfc/__tests__/cssVars.spec.ts

+68
Original file line numberDiff line numberDiff line change
@@ -272,5 +272,73 @@ describe('CSS vars injection', () => {
272272
`export default {\n setup(__props, { expose: __expose }) {\n __expose();\n\n_useCssVars(_ctx => ({\n "xxxxxxxx-background": (_unref(background))\n}))`
273273
)
274274
})
275+
276+
describe('skip codegen in SSR', () => {
277+
test('script setup, inline', () => {
278+
const { content } = compileSFCScript(
279+
`<script setup>
280+
let size = 1
281+
</script>\n` +
282+
`<style>
283+
div {
284+
font-size: v-bind(size);
285+
}
286+
</style>`,
287+
{
288+
inlineTemplate: true,
289+
templateOptions: {
290+
ssr: true
291+
}
292+
}
293+
)
294+
expect(content).not.toMatch(`_useCssVars`)
295+
})
296+
297+
// #6926
298+
test('script, non-inline', () => {
299+
const { content } = compileSFCScript(
300+
`<script setup>
301+
let size = 1
302+
</script>\n` +
303+
`<style>
304+
div {
305+
font-size: v-bind(size);
306+
}
307+
</style>`,
308+
{
309+
inlineTemplate: false,
310+
templateOptions: {
311+
ssr: true
312+
}
313+
}
314+
)
315+
expect(content).not.toMatch(`_useCssVars`)
316+
})
317+
318+
test('normal script', () => {
319+
const { content } = compileSFCScript(
320+
`<script>
321+
export default {
322+
setup() {
323+
return {
324+
size: ref('100px')
325+
}
326+
}
327+
}
328+
</script>\n` +
329+
`<style>
330+
div {
331+
font-size: v-bind(size);
332+
}
333+
</style>`,
334+
{
335+
templateOptions: {
336+
ssr: true
337+
}
338+
}
339+
)
340+
expect(content).not.toMatch(`_useCssVars`)
341+
})
342+
})
275343
})
276344
})

packages/compiler-sfc/src/compileScript.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,7 @@ export function compileScript(
765765
if (
766766
sfc.cssVars.length &&
767767
// no need to do this when targeting SSR
768-
!(options.inlineTemplate && options.templateOptions?.ssr)
768+
!options.templateOptions?.ssr
769769
) {
770770
ctx.helperImports.add(CSS_VARS_HELPER)
771771
ctx.helperImports.add('unref')

packages/compiler-sfc/src/script/normalScript.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export function processNormalScript(
5555
const s = new MagicString(content)
5656
rewriteDefaultAST(scriptAst.body, s, defaultVar)
5757
content = s.toString()
58-
if (cssVars.length) {
58+
if (cssVars.length && !ctx.options.templateOptions?.ssr) {
5959
content += genNormalScriptCssVarsCode(
6060
cssVars,
6161
bindings,

0 commit comments

Comments
 (0)