@@ -12,8 +12,8 @@ import { PluginCreator } from 'postcss'
12
12
import hash from 'hash-sum'
13
13
14
14
export const CSS_VARS_HELPER = `useCssVars`
15
- export const cssVarRE =
16
- / \b v - b i n d \s * \( \s * (?: ' ( [ ^ ' ] + ) ' | " ( [ ^ " ] + ) " | ( [ ^ ' " ] [ ^ ; ] * ) ) \s * \) / g
15
+ // match v-bind() with max 2-levels of nested parens.
16
+ const cssVarRE = / v - b i n d \s * \( ( (?: [ ^ ) ( ] + | \( (?: [ ^ ) ( ] + | \ ([ ^ ) ( ] * \) ) * \) ) * ) \) / g
17
17
18
18
export function genCssVarsFromList (
19
19
vars : string [ ] ,
@@ -33,14 +33,25 @@ function genVarName(id: string, raw: string, isProd: boolean): string {
33
33
}
34
34
}
35
35
36
+ function noramlizeExpression ( exp : string ) {
37
+ exp = exp . trim ( )
38
+ if (
39
+ ( exp [ 0 ] === `'` && exp [ exp . length - 1 ] === `'` ) ||
40
+ ( exp [ 0 ] === `"` && exp [ exp . length - 1 ] === `"` )
41
+ ) {
42
+ return exp . slice ( 1 , - 1 )
43
+ }
44
+ return exp
45
+ }
46
+
36
47
export function parseCssVars ( sfc : SFCDescriptor ) : string [ ] {
37
48
const vars : string [ ] = [ ]
38
49
sfc . styles . forEach ( style => {
39
50
let match
40
51
// ignore v-bind() in comments /* ... */
41
52
const content = style . content . replace ( / \/ \* ( [ \s \S ] * ?) \* \/ / g, '' )
42
53
while ( ( match = cssVarRE . exec ( content ) ) ) {
43
- const variable = match [ 1 ] || match [ 2 ] || match [ 3 ]
54
+ const variable = noramlizeExpression ( match [ 1 ] )
44
55
if ( ! vars . includes ( variable ) ) {
45
56
vars . push ( variable )
46
57
}
@@ -62,8 +73,8 @@ export const cssVarsPlugin: PluginCreator<CssVarsPluginOptions> = opts => {
62
73
Declaration ( decl ) {
63
74
// rewrite CSS variables
64
75
if ( cssVarRE . test ( decl . value ) ) {
65
- decl . value = decl . value . replace ( cssVarRE , ( _ , $1 , $2 , $3 ) => {
66
- return `var(--${ genVarName ( id , $1 || $2 || $3 , isProd ) } )`
76
+ decl . value = decl . value . replace ( cssVarRE , ( _ , $1 ) => {
77
+ return `var(--${ genVarName ( id , noramlizeExpression ( $1 ) , isProd ) } )`
67
78
} )
68
79
}
69
80
}
0 commit comments