]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(sfc/style-vars): improve ignore style variable bindings in comments (#4202)
authoredison <daiwei521@126.com>
Wed, 28 Jul 2021 14:51:25 +0000 (22:51 +0800)
committerGitHub <noreply@github.com>
Wed, 28 Jul 2021 14:51:25 +0000 (10:51 -0400)
packages/compiler-sfc/__tests__/__snapshots__/cssVars.spec.ts.snap
packages/compiler-sfc/__tests__/cssVars.spec.ts
packages/compiler-sfc/src/cssVars.ts

index 9616b8fdfd7b89a9183ee9d32842154e6c9f3df8..dd45d4546ecb1dd696c958adf446c2d791c21583 100644 (file)
@@ -50,11 +50,17 @@ export default __default__"
 `;
 
 exports[`CSS vars injection codegen should ignore comments 1`] = `
-"export default {
+"import { useCssVars as _useCssVars, unref as _unref } from 'vue'
+
+export default {
   setup(__props, { expose }) {
   expose()
-const color = 'red'
-return { color }
+
+_useCssVars(_ctx => ({
+  \\"xxxxxxxx-width\\": (width)
+}))
+const color = 'red';const width = 100
+return { color, width }
 }
 
 }"
index 10aa33f7419ce932d7238f202901eb4c7f4eac4c..67447310c6ff48ad9e7002c017fd09abec44b453 100644 (file)
@@ -164,13 +164,17 @@ describe('CSS vars injection', () => {
     //#4185
     test('should ignore comments', () => {
       const { content } = compileSFCScript(
-        `<script setup>const color = 'red'</script>\n` +
+        `<script setup>const color = 'red';const width = 100</script>\n` +
           `<style>
+            /* comment **/
             div{ /* color: v-bind(color); */ width:20; }
+            div{ width: v-bind(width); }
+            /* comment */
           </style>`
       )
 
-      expect(content).not.toMatch(`_useCssVars`)
+      expect(content).not.toMatch(`"${mockId}-color": (color)`)
+      expect(content).toMatch(`"${mockId}-width": (width)`)
       assertCode(content)
     })
 
index 5c7b80c9c70d0b0527cbc2799083caee5a272e81..f51269f126978f3b80b25b94eac8b7b13f46654e 100644 (file)
@@ -12,8 +12,7 @@ import { PluginCreator } from 'postcss'
 import hash from 'hash-sum'
 
 export const CSS_VARS_HELPER = `useCssVars`
-export const cssVarRE =
-  /\bv-bind\(\s*(?:'([^']+)'|"([^"]+)"|([^'"][^)]*))\s*\)/g
+export const cssVarRE = /\bv-bind\(\s*(?:'([^']+)'|"([^"]+)"|([^'"][^)]*))\s*\)/g
 
 export function genCssVarsFromList(
   vars: string[],
@@ -38,7 +37,7 @@ export function parseCssVars(sfc: SFCDescriptor): string[] {
   sfc.styles.forEach(style => {
     let match
     // ignore v-bind() in comments /* ... */
-    const content = style.content.replace(/\/\*[\s\S]*\*\//g, '')
+    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)) {