Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler-sfc): ssr compile with cssVars setup-maybe-ref inject unref (fix #6201) #6218

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -1039,19 +1039,22 @@ return (_ctx, _cache) => {
`;

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

import { ref } from 'vue'
import { ref,computed } from 'vue'

export default {
__ssrInlineRender: true,
setup(__props) {

const count = ref(0)
const background = computed(() => 'red')

return (_ctx, _push, _parent, _attrs) => {
const _cssVars = { style: {
\\"--xxxxxxxx-count\\": (count.value)
\\"--xxxxxxxx-count\\": (count.value),
\\"--xxxxxxxx-background\\": (_unref(background))
}}
_push(\`<!--[--><div\${
_ssrRenderAttrs(_cssVars)
Expand Down
6 changes: 4 additions & 2 deletions packages/compiler-sfc/__tests__/compileScript.spec.ts
Expand Up @@ -707,15 +707,16 @@ defineExpose({ foo: 123 })
const { content } = compile(
`
<script setup>
import { ref } from 'vue'
import { ref,computed } from 'vue'
const count = ref(0)
const background = computed(() => 'red')
</script>
<template>
<div>{{ count }}</div>
<div>static</div>
</template>
<style>
div { color: v-bind(count) }
div { color: v-bind(count);background: v-bind(background); }
</style>
`,
{
Expand All @@ -729,6 +730,7 @@ defineExpose({ foo: 123 })
expect(content).toMatch(`return (_ctx, _push`)
expect(content).toMatch(`ssrInterpolate`)
expect(content).not.toMatch(`useCssVars`)
expect(content).toMatch(`unref as _unref`)
expect(content).toMatch(`"--${mockId}-count": (count.value)`)
assertCode(content)
})
Expand Down
28 changes: 17 additions & 11 deletions packages/compiler-sfc/src/compileScript.ts
Expand Up @@ -1310,17 +1310,23 @@ export function compileScript(
}

// 8. inject `useCssVars` calls
if (
cssVars.length &&
// no need to do this when targeting SSR
!(options.inlineTemplate && options.templateOptions?.ssr)
) {
helperImports.add(CSS_VARS_HELPER)
helperImports.add('unref')
s.prependRight(
startOffset,
`\n${genCssVarsCode(cssVars, bindingMetadata, scopeId, isProd)}\n`
)
if (cssVars.length) {
if (options.inlineTemplate && options.templateOptions?.ssr) {
// ssr w/ `setup-maybe-ref` inject `unref`
if (
Object.values(bindingMetadata).includes(BindingTypes.SETUP_MAYBE_REF)
) {
helperImports.add('unref')
}
} else {
// no need to do this when targeting SSR
helperImports.add(CSS_VARS_HELPER)
helperImports.add('unref')
s.prependRight(
startOffset,
`\n${genCssVarsCode(cssVars, bindingMetadata, scopeId, isProd)}\n`
)
}
}

// 9. finalize setup() argument signature
Expand Down