]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-sfc): fix style injection when using normal script + setup
authorEvan You <yyx990803@gmail.com>
Fri, 28 May 2021 22:40:35 +0000 (18:40 -0400)
committerEvan You <yyx990803@gmail.com>
Fri, 28 May 2021 22:40:45 +0000 (18:40 -0400)
fix #3688

packages/compiler-sfc/__tests__/__snapshots__/cssVars.spec.ts.snap
packages/compiler-sfc/__tests__/cssVars.spec.ts
packages/compiler-sfc/src/cssVars.ts

index a2926e8fee11efe22d0af171e312376ab115398b..99f963804b5396e86ff74bf625cdefcd022f8326 100644 (file)
@@ -109,3 +109,26 @@ return { color, size, ref }
 
 }"
 `;
+
+exports[`CSS vars injection w/ normal <script> binding analysis 1`] = `
+"
+      const __default__ = {
+        setup() {
+          return {
+            size: ref('100px')
+          }
+        }
+      }
+      
+import { useCssVars as _useCssVars } from 'vue'
+const __injectCSSVars__ = () => {
+_useCssVars(_ctx => ({
+  \\"xxxxxxxx-size\\": (_ctx.size)
+}))}
+const __setup__ = __default__.setup
+__default__.setup = __setup__
+  ? (props, ctx) => { __injectCSSVars__();return __setup__(props, ctx) }
+  : __injectCSSVars__
+
+export default __default__"
+`;
index d9d41ecce8ec94c59292373e3f14a5280e48d862..bdeada0571442808340f6758ab30f06385c36d6c 100644 (file)
@@ -17,6 +17,30 @@ describe('CSS vars injection', () => {
     assertCode(content)
   })
 
+  test('w/ normal <script> binding analysis', () => {
+    const { content } = compileSFCScript(
+      `<script>
+      export default {
+        setup() {
+          return {
+            size: ref('100px')
+          }
+        }
+      }
+      </script>\n` +
+        `<style>
+          div {
+            font-size: v-bind(size);
+          }
+        </style>`
+    )
+    expect(content).toMatch(`_useCssVars(_ctx => ({
+  "${mockId}-size": (_ctx.size)
+})`)
+    expect(content).toMatch(`import { useCssVars as _useCssVars } from 'vue'`)
+    assertCode(content)
+  })
+
   test('w/ <script setup> binding analysis', () => {
     const { content } = compileSFCScript(
       `<script setup>
index 4fb721f963dd04e1cbdfb1fefdca79b2a1df079f..021724fd190ab8bad476c306ce4786d77848c32b 100644 (file)
@@ -76,7 +76,7 @@ export function genCssVarsCode(
   const context = createTransformContext(createRoot([]), {
     prefixIdentifiers: true,
     inline: true,
-    bindingMetadata: bindings
+    bindingMetadata: bindings.__isScriptSetup === false ? undefined : bindings
   })
   const transformed = processExpression(exp, context)
   const transformedString =