]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-sfc): duplicated injected css var with repeated vars in style (#2802)
authorpatak <matias.capeletto@gmail.com>
Thu, 15 Jul 2021 20:45:37 +0000 (22:45 +0200)
committerGitHub <noreply@github.com>
Thu, 15 Jul 2021 20:45:37 +0000 (16:45 -0400)
packages/compiler-sfc/__tests__/__snapshots__/cssVars.spec.ts.snap
packages/compiler-sfc/__tests__/cssVars.spec.ts
packages/compiler-sfc/src/cssVars.ts

index a5effc3530f7c53a48c1fc3d7880e0cd3fbdd775..44eb668d491ff5e0a6e5d525f82576b94dfc3883 100644 (file)
@@ -66,6 +66,25 @@ return { color }
 }"
 `;
 
+exports[`CSS vars injection codegen w/ <script setup> using the same var multiple times 1`] = `
+"import { useCssVars as _useCssVars, unref as _unref } from 'vue'
+
+export default {
+  expose: [],
+  setup(__props) {
+
+_useCssVars(_ctx => ({
+  \\"xxxxxxxx-color\\": (color)
+}))
+
+        const color = 'red'
+        
+return { color }
+}
+
+}"
+`;
+
 exports[`CSS vars injection generating correct code for nested paths 1`] = `
 "const a = 1
 const __default__ = {}
index bdeada0571442808340f6758ab30f06385c36d6c..879c5026fa1030bce08ee13ae8d0872bfebd5b8a 100644 (file)
@@ -160,5 +160,26 @@ describe('CSS vars injection', () => {
         ).content
       )
     })
+
+    test('w/ <script setup> using the same var multiple times', () => {
+      const { content } = compileSFCScript(
+        `<script setup>
+        const color = 'red'
+        </script>\n` +
+          `<style>
+          div {
+            color: v-bind(color);
+          }
+          p {
+            color: v-bind(color);
+          }
+        </style>`
+      )
+      // color should only be injected once, even if it is twice in style
+      expect(content).toMatch(`_useCssVars(_ctx => ({
+  "${mockId}-color": (color)
+})`)
+      assertCode(content)
+    })
   })
 })
index 021724fd190ab8bad476c306ce4786d77848c32b..884742f2978956b13880666d542d325cab999963 100644 (file)
@@ -37,7 +37,10 @@ export function parseCssVars(sfc: SFCDescriptor): string[] {
   sfc.styles.forEach(style => {
     let match
     while ((match = cssVarRE.exec(style.content))) {
-      vars.push(match[1] || match[2] || match[3])
+      const variable = match[1] || match[2] || match[3]
+      if (!vars.includes(variable)) {
+        vars.push(variable)
+      }
     }
   })
   return vars