fix(ssr/sfc-css-vars): fix v-bind css vars codegen for SSR
fix #5443 close #5444
This commit is contained in:
parent
2a9e9a4096
commit
efea4a8b57
@ -205,7 +205,7 @@ export default {
|
|||||||
expose();
|
expose();
|
||||||
|
|
||||||
let __temp, __restore
|
let __temp, __restore
|
||||||
if (ok) {
|
if (ok) {
|
||||||
for (let a of [1,2,3]) {
|
for (let a of [1,2,3]) {
|
||||||
(
|
(
|
||||||
([__temp,__restore] = _withAsyncContext(() => a)),
|
([__temp,__restore] = _withAsyncContext(() => a)),
|
||||||
@ -240,7 +240,7 @@ export default {
|
|||||||
expose();
|
expose();
|
||||||
|
|
||||||
let __temp, __restore
|
let __temp, __restore
|
||||||
if (ok) {
|
if (ok) {
|
||||||
while (d) {
|
while (d) {
|
||||||
(
|
(
|
||||||
([__temp,__restore] = _withAsyncContext(() => 5)),
|
([__temp,__restore] = _withAsyncContext(() => 5)),
|
||||||
@ -295,7 +295,7 @@ export default {
|
|||||||
expose();
|
expose();
|
||||||
|
|
||||||
let __temp, __restore
|
let __temp, __restore
|
||||||
if (ok) {
|
if (ok) {
|
||||||
let a = 'foo'
|
let a = 'foo'
|
||||||
;(
|
;(
|
||||||
([__temp,__restore] = _withAsyncContext(() => 0)),
|
([__temp,__restore] = _withAsyncContext(() => 0)),
|
||||||
@ -351,7 +351,7 @@ if (ok) {
|
|||||||
__restore()
|
__restore()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
(
|
(
|
||||||
([__temp,__restore] = _withAsyncContext(() => 5)),
|
([__temp,__restore] = _withAsyncContext(() => 5)),
|
||||||
await __temp,
|
await __temp,
|
||||||
@ -1039,8 +1039,7 @@ return (_ctx, _cache) => {
|
|||||||
`;
|
`;
|
||||||
|
|
||||||
exports[`SFC compile <script setup> inlineTemplate mode ssr codegen 1`] = `
|
exports[`SFC compile <script setup> inlineTemplate mode ssr codegen 1`] = `
|
||||||
"import { useCssVars as _useCssVars, unref as _unref } from 'vue'
|
"import { ssrRenderAttrs as _ssrRenderAttrs, ssrInterpolate as _ssrInterpolate } from \\"vue/server-renderer\\"
|
||||||
import { ssrRenderAttrs as _ssrRenderAttrs, ssrInterpolate as _ssrInterpolate } from \\"vue/server-renderer\\"
|
|
||||||
|
|
||||||
import { ref } from 'vue'
|
import { ref } from 'vue'
|
||||||
|
|
||||||
@ -1048,15 +1047,11 @@ export default {
|
|||||||
__ssrInlineRender: true,
|
__ssrInlineRender: true,
|
||||||
setup(__props) {
|
setup(__props) {
|
||||||
|
|
||||||
_useCssVars(_ctx => ({
|
|
||||||
\\"xxxxxxxx-count\\": (count.value)
|
|
||||||
}))
|
|
||||||
|
|
||||||
const count = ref(0)
|
const count = ref(0)
|
||||||
|
|
||||||
return (_ctx, _push, _parent, _attrs) => {
|
return (_ctx, _push, _parent, _attrs) => {
|
||||||
const _cssVars = { style: {
|
const _cssVars = { style: {
|
||||||
\\"xxxxxxxx-count\\": (count.value)
|
\\"--xxxxxxxx-count\\": (count.value)
|
||||||
}}
|
}}
|
||||||
_push(\`<!--[--><div\${
|
_push(\`<!--[--><div\${
|
||||||
_ssrRenderAttrs(_cssVars)
|
_ssrRenderAttrs(_cssVars)
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { BindingTypes } from '@vue/compiler-core'
|
import { BindingTypes } from '@vue/compiler-core'
|
||||||
import { compileSFCScript as compile, assertCode } from './utils'
|
import { compileSFCScript as compile, assertCode, mockId } from './utils'
|
||||||
|
|
||||||
describe('SFC compile <script setup>', () => {
|
describe('SFC compile <script setup>', () => {
|
||||||
test('should expose top level declarations', () => {
|
test('should expose top level declarations', () => {
|
||||||
@ -168,7 +168,7 @@ defineExpose({ foo: 123 })
|
|||||||
expect(content).toMatch(/\bexpose\(\{ foo: 123 \}\)/)
|
expect(content).toMatch(/\bexpose\(\{ foo: 123 \}\)/)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('<script> after <script setup> the script content not end with `\\n`',() => {
|
test('<script> after <script setup> the script content not end with `\\n`', () => {
|
||||||
const { content } = compile(`
|
const { content } = compile(`
|
||||||
<script setup>
|
<script setup>
|
||||||
import { x } from './x'
|
import { x } from './x'
|
||||||
@ -726,6 +726,8 @@ defineExpose({ foo: 123 })
|
|||||||
expect(content).toMatch(`\n __ssrInlineRender: true,\n`)
|
expect(content).toMatch(`\n __ssrInlineRender: true,\n`)
|
||||||
expect(content).toMatch(`return (_ctx, _push`)
|
expect(content).toMatch(`return (_ctx, _push`)
|
||||||
expect(content).toMatch(`ssrInterpolate`)
|
expect(content).toMatch(`ssrInterpolate`)
|
||||||
|
expect(content).not.toMatch(`useCssVars`)
|
||||||
|
expect(content).toMatch(`"--${mockId}-count": (count.value)`)
|
||||||
assertCode(content)
|
assertCode(content)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@ -1196,7 +1198,7 @@ const emit = defineEmits(['a', 'b'])
|
|||||||
})
|
})
|
||||||
|
|
||||||
test('multiple `if` nested statements', () => {
|
test('multiple `if` nested statements', () => {
|
||||||
assertAwaitDetection(`if (ok) {
|
assertAwaitDetection(`if (ok) {
|
||||||
let a = 'foo'
|
let a = 'foo'
|
||||||
await 0 + await 1
|
await 0 + await 1
|
||||||
await 2
|
await 2
|
||||||
@ -1212,13 +1214,13 @@ const emit = defineEmits(['a', 'b'])
|
|||||||
await 3
|
await 3
|
||||||
await 4
|
await 4
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
await 5
|
await 5
|
||||||
}`)
|
}`)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('multiple `if while` nested statements', () => {
|
test('multiple `if while` nested statements', () => {
|
||||||
assertAwaitDetection(`if (ok) {
|
assertAwaitDetection(`if (ok) {
|
||||||
while (d) {
|
while (d) {
|
||||||
await 5
|
await 5
|
||||||
}
|
}
|
||||||
@ -1237,7 +1239,7 @@ const emit = defineEmits(['a', 'b'])
|
|||||||
})
|
})
|
||||||
|
|
||||||
test('multiple `if for` nested statements', () => {
|
test('multiple `if for` nested statements', () => {
|
||||||
assertAwaitDetection(`if (ok) {
|
assertAwaitDetection(`if (ok) {
|
||||||
for (let a of [1,2,3]) {
|
for (let a of [1,2,3]) {
|
||||||
await a
|
await a
|
||||||
}
|
}
|
||||||
|
@ -431,11 +431,12 @@ export function compileScript(
|
|||||||
prop.key
|
prop.key
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const propKey = prop.key.type === 'StringLiteral'
|
const propKey =
|
||||||
? prop.key.value
|
prop.key.type === 'StringLiteral'
|
||||||
: (prop.key as Identifier).name
|
? prop.key.value
|
||||||
|
: (prop.key as Identifier).name
|
||||||
|
|
||||||
if (prop.value.type === 'AssignmentPattern') {
|
if (prop.value.type === 'AssignmentPattern') {
|
||||||
// default value { foo = 123 }
|
// default value { foo = 123 }
|
||||||
const { left, right } = prop.value
|
const { left, right } = prop.value
|
||||||
@ -1304,7 +1305,11 @@ export function compileScript(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 8. inject `useCssVars` calls
|
// 8. inject `useCssVars` calls
|
||||||
if (cssVars.length) {
|
if (
|
||||||
|
cssVars.length &&
|
||||||
|
// no need to do this when targeting SSR
|
||||||
|
!(options.inlineTemplate && options.templateOptions?.ssr)
|
||||||
|
) {
|
||||||
helperImports.add(CSS_VARS_HELPER)
|
helperImports.add(CSS_VARS_HELPER)
|
||||||
helperImports.add('unref')
|
helperImports.add('unref')
|
||||||
s.prependRight(
|
s.prependRight(
|
||||||
|
@ -202,7 +202,7 @@ function doCompileTemplate({
|
|||||||
cacheHandlers: true,
|
cacheHandlers: true,
|
||||||
ssrCssVars:
|
ssrCssVars:
|
||||||
ssr && ssrCssVars && ssrCssVars.length
|
ssr && ssrCssVars && ssrCssVars.length
|
||||||
? genCssVarsFromList(ssrCssVars, shortId, isProd)
|
? genCssVarsFromList(ssrCssVars, shortId, isProd, true)
|
||||||
: '',
|
: '',
|
||||||
scopeId: scoped ? longId : undefined,
|
scopeId: scoped ? longId : undefined,
|
||||||
slotted,
|
slotted,
|
||||||
|
@ -18,10 +18,13 @@ const cssVarRE = /v-bind\s*\(((?:[^)(]+|\((?:[^)(]+|\([^)(]*\))*\))*)\)/g
|
|||||||
export function genCssVarsFromList(
|
export function genCssVarsFromList(
|
||||||
vars: string[],
|
vars: string[],
|
||||||
id: string,
|
id: string,
|
||||||
isProd: boolean
|
isProd: boolean,
|
||||||
|
isSSR = false
|
||||||
): string {
|
): string {
|
||||||
return `{\n ${vars
|
return `{\n ${vars
|
||||||
.map(key => `"${genVarName(id, key, isProd)}": (${key})`)
|
.map(
|
||||||
|
key => `"${isSSR ? `--` : ``}${genVarName(id, key, isProd)}": (${key})`
|
||||||
|
)
|
||||||
.join(',\n ')}\n}`
|
.join(',\n ')}\n}`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user