]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(sfc/style-vars): should ignore style variable bindings in comments (#4188)
authoredison <daiwei521@126.com>
Mon, 26 Jul 2021 15:19:56 +0000 (23:19 +0800)
committerGitHub <noreply@github.com>
Mon, 26 Jul 2021 15:19:56 +0000 (11:19 -0400)
fix #4185

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

index 0c6a96a091aa7da233f98420f5deae19812428e3..9616b8fdfd7b89a9183ee9d32842154e6c9f3df8 100644 (file)
@@ -49,6 +49,17 @@ __default__.setup = __setup__
 export default __default__"
 `;
 
+exports[`CSS vars injection codegen should ignore comments 1`] = `
+"export default {
+  setup(__props, { expose }) {
+  expose()
+const color = 'red'
+return { color }
+}
+
+}"
+`;
+
 exports[`CSS vars injection codegen w/ <script setup> 1`] = `
 "import { useCssVars as _useCssVars, unref as _unref } from 'vue'
 
index 879c5026fa1030bce08ee13ae8d0872bfebd5b8a..10aa33f7419ce932d7238f202901eb4c7f4eac4c 100644 (file)
@@ -161,6 +161,19 @@ describe('CSS vars injection', () => {
       )
     })
 
+    //#4185
+    test('should ignore comments', () => {
+      const { content } = compileSFCScript(
+        `<script setup>const color = 'red'</script>\n` +
+          `<style>
+            div{ /* color: v-bind(color); */ width:20; }
+          </style>`
+      )
+
+      expect(content).not.toMatch(`_useCssVars`)
+      assertCode(content)
+    })
+
     test('w/ <script setup> using the same var multiple times', () => {
       const { content } = compileSFCScript(
         `<script setup>
index 853db99f7fe506f4052029854650cdc51f712c24..5c7b80c9c70d0b0527cbc2799083caee5a272e81 100644 (file)
@@ -37,7 +37,9 @@ export function parseCssVars(sfc: SFCDescriptor): string[] {
   const vars: string[] = []
   sfc.styles.forEach(style => {
     let match
-    while ((match = cssVarRE.exec(style.content))) {
+    // ignore v-bind() in comments /* ... */
+    const content = style.content.replace(/\/\*[\s\S]*\*\//g, '')
+    while ((match = cssVarRE.exec(content))) {
       const variable = match[1] || match[2] || match[3]
       if (!vars.includes(variable)) {
         vars.push(variable)