]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(ssr): forward helpers provided by CSS `v-bind` (#6489)
authorAnthony Fu <anthonyfu117@hotmail.com>
Mon, 29 Aug 2022 03:09:21 +0000 (11:09 +0800)
committerGitHub <noreply@github.com>
Mon, 29 Aug 2022 03:09:21 +0000 (23:09 -0400)
fix #6201

packages/compiler-ssr/__tests__/ssrInjectCssVars.spec.ts
packages/compiler-ssr/src/ssrCodegenTransform.ts

index b2e5b44518d301c58a8c2d72848d99fc2f2910eb..97df3045c67dea5f106bfb1e6feeba7bab31aeb9 100644 (file)
@@ -1,3 +1,4 @@
+import { BindingTypes } from '@vue/compiler-dom'
 import { compile } from '../src'
 
 describe('ssr: inject <style vars>', () => {
@@ -111,4 +112,25 @@ describe('ssr: inject <style vars>', () => {
       }"
     `)
   })
+
+  test('inject helpers', () => {
+    const result = compile(`<div/>`, {
+      inline: true,
+      bindingMetadata: { dynamic: BindingTypes.SETUP_MAYBE_REF },
+      ssrCssVars: '{ "--hash": (dynamic) }'
+    })
+
+    expect(result.code).toMatchInlineSnapshot(`
+      "(_ctx, _push, _parent, _attrs) => {
+        const _cssVars = { style: { \\"--hash\\": (_unref(dynamic)) }}
+        _push(\`<div\${_ssrRenderAttrs(_mergeProps(_attrs, _cssVars))}></div>\`)
+      }"
+    `)
+    expect(result.ast.helpers).toMatchInlineSnapshot(`
+      Array [
+        Symbol(mergeProps),
+        Symbol(unref),
+      ]
+    `)
+  })
 })
index a7f3ada15b4ed0f6f3a2c3ac1e589c4f8627a8eb..755862b52329d2abffefd81b8bc65216510dc0b0 100644 (file)
@@ -40,13 +40,17 @@ export function ssrCodegenTransform(ast: RootNode, options: CompilerOptions) {
   // we do this instead of inlining the expression to ensure the vars are
   // only resolved once per render
   if (options.ssrCssVars) {
+    const cssContext = createTransformContext(createRoot([]), options)
     const varsExp = processExpression(
       createSimpleExpression(options.ssrCssVars, false),
-      createTransformContext(createRoot([]), options)
+      cssContext
     )
     context.body.push(
       createCompoundExpression([`const _cssVars = { style: `, varsExp, `}`])
     )
+    Array.from(cssContext.helpers.keys()).forEach(helper =>
+      ast.helpers.push(helper)
+    )
   }
 
   const isFragment =