Skip to content

Commit efea4a8

Browse files
committed
fix(ssr/sfc-css-vars): fix v-bind css vars codegen for SSR
fix #5443 close #5444
1 parent 2a9e9a4 commit efea4a8

File tree

5 files changed

+31
-26
lines changed

5 files changed

+31
-26
lines changed

packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap

+6-11
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ export default {
205205
expose();
206206

207207
let __temp, __restore
208-
if (ok) {
208+
if (ok) {
209209
for (let a of [1,2,3]) {
210210
(
211211
([__temp,__restore] = _withAsyncContext(() => a)),
@@ -240,7 +240,7 @@ export default {
240240
expose();
241241

242242
let __temp, __restore
243-
if (ok) {
243+
if (ok) {
244244
while (d) {
245245
(
246246
([__temp,__restore] = _withAsyncContext(() => 5)),
@@ -295,7 +295,7 @@ export default {
295295
expose();
296296

297297
let __temp, __restore
298-
if (ok) {
298+
if (ok) {
299299
let a = 'foo'
300300
;(
301301
([__temp,__restore] = _withAsyncContext(() => 0)),
@@ -351,7 +351,7 @@ if (ok) {
351351
__restore()
352352
)
353353
}
354-
} else {
354+
} else {
355355
(
356356
([__temp,__restore] = _withAsyncContext(() => 5)),
357357
await __temp,
@@ -1039,24 +1039,19 @@ return (_ctx, _cache) => {
10391039
`;
10401040

10411041
exports[`SFC compile <script setup> inlineTemplate mode ssr codegen 1`] = `
1042-
"import { useCssVars as _useCssVars, unref as _unref } from 'vue'
1043-
import { ssrRenderAttrs as _ssrRenderAttrs, ssrInterpolate as _ssrInterpolate } from \\"vue/server-renderer\\"
1042+
"import { ssrRenderAttrs as _ssrRenderAttrs, ssrInterpolate as _ssrInterpolate } from \\"vue/server-renderer\\"
10441043

10451044
import { ref } from 'vue'
10461045

10471046
export default {
10481047
__ssrInlineRender: true,
10491048
setup(__props) {
10501049

1051-
_useCssVars(_ctx => ({
1052-
\\"xxxxxxxx-count\\": (count.value)
1053-
}))
1054-
10551050
const count = ref(0)
10561051

10571052
return (_ctx, _push, _parent, _attrs) => {
10581053
const _cssVars = { style: {
1059-
\\"xxxxxxxx-count\\": (count.value)
1054+
\\"--xxxxxxxx-count\\": (count.value)
10601055
}}
10611056
_push(\`<!--[--><div\${
10621057
_ssrRenderAttrs(_cssVars)

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

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { BindingTypes } from '@vue/compiler-core'
2-
import { compileSFCScript as compile, assertCode } from './utils'
2+
import { compileSFCScript as compile, assertCode, mockId } from './utils'
33

44
describe('SFC compile <script setup>', () => {
55
test('should expose top level declarations', () => {
@@ -168,7 +168,7 @@ defineExpose({ foo: 123 })
168168
expect(content).toMatch(/\bexpose\(\{ foo: 123 \}\)/)
169169
})
170170

171-
test('<script> after <script setup> the script content not end with `\\n`',() => {
171+
test('<script> after <script setup> the script content not end with `\\n`', () => {
172172
const { content } = compile(`
173173
<script setup>
174174
import { x } from './x'
@@ -726,6 +726,8 @@ defineExpose({ foo: 123 })
726726
expect(content).toMatch(`\n __ssrInlineRender: true,\n`)
727727
expect(content).toMatch(`return (_ctx, _push`)
728728
expect(content).toMatch(`ssrInterpolate`)
729+
expect(content).not.toMatch(`useCssVars`)
730+
expect(content).toMatch(`"--${mockId}-count": (count.value)`)
729731
assertCode(content)
730732
})
731733
})
@@ -1196,7 +1198,7 @@ const emit = defineEmits(['a', 'b'])
11961198
})
11971199

11981200
test('multiple `if` nested statements', () => {
1199-
assertAwaitDetection(`if (ok) {
1201+
assertAwaitDetection(`if (ok) {
12001202
let a = 'foo'
12011203
await 0 + await 1
12021204
await 2
@@ -1212,13 +1214,13 @@ const emit = defineEmits(['a', 'b'])
12121214
await 3
12131215
await 4
12141216
}
1215-
} else {
1217+
} else {
12161218
await 5
12171219
}`)
12181220
})
12191221

12201222
test('multiple `if while` nested statements', () => {
1221-
assertAwaitDetection(`if (ok) {
1223+
assertAwaitDetection(`if (ok) {
12221224
while (d) {
12231225
await 5
12241226
}
@@ -1237,7 +1239,7 @@ const emit = defineEmits(['a', 'b'])
12371239
})
12381240

12391241
test('multiple `if for` nested statements', () => {
1240-
assertAwaitDetection(`if (ok) {
1242+
assertAwaitDetection(`if (ok) {
12411243
for (let a of [1,2,3]) {
12421244
await a
12431245
}

packages/compiler-sfc/src/compileScript.ts

+11-6
Original file line numberDiff line numberDiff line change
@@ -431,11 +431,12 @@ export function compileScript(
431431
prop.key
432432
)
433433
}
434-
435-
const propKey = prop.key.type === 'StringLiteral'
436-
? prop.key.value
437-
: (prop.key as Identifier).name
438-
434+
435+
const propKey =
436+
prop.key.type === 'StringLiteral'
437+
? prop.key.value
438+
: (prop.key as Identifier).name
439+
439440
if (prop.value.type === 'AssignmentPattern') {
440441
// default value { foo = 123 }
441442
const { left, right } = prop.value
@@ -1304,7 +1305,11 @@ export function compileScript(
13041305
}
13051306

13061307
// 8. inject `useCssVars` calls
1307-
if (cssVars.length) {
1308+
if (
1309+
cssVars.length &&
1310+
// no need to do this when targeting SSR
1311+
!(options.inlineTemplate && options.templateOptions?.ssr)
1312+
) {
13081313
helperImports.add(CSS_VARS_HELPER)
13091314
helperImports.add('unref')
13101315
s.prependRight(

packages/compiler-sfc/src/compileTemplate.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ function doCompileTemplate({
202202
cacheHandlers: true,
203203
ssrCssVars:
204204
ssr && ssrCssVars && ssrCssVars.length
205-
? genCssVarsFromList(ssrCssVars, shortId, isProd)
205+
? genCssVarsFromList(ssrCssVars, shortId, isProd, true)
206206
: '',
207207
scopeId: scoped ? longId : undefined,
208208
slotted,

packages/compiler-sfc/src/cssVars.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@ const cssVarRE = /v-bind\s*\(((?:[^)(]+|\((?:[^)(]+|\([^)(]*\))*\))*)\)/g
1818
export function genCssVarsFromList(
1919
vars: string[],
2020
id: string,
21-
isProd: boolean
21+
isProd: boolean,
22+
isSSR = false
2223
): string {
2324
return `{\n ${vars
24-
.map(key => `"${genVarName(id, key, isProd)}": (${key})`)
25+
.map(
26+
key => `"${isSSR ? `--` : ``}${genVarName(id, key, isProd)}": (${key})`
27+
)
2528
.join(',\n ')}\n}`
2629
}
2730

0 commit comments

Comments
 (0)