1
1
// https://github.com/vuejs/core/blob/main/packages/compiler-sfc/src/cssVars.ts#L47-L61
2
2
3
3
const vBindCssVarReg = / \b v - b i n d \( \s * (?: ' ( [ ^ ' ] + ) ' | " ( [ ^ " ] + ) " | ( [ a - z _ ] \w * ) ) \s * \) / gi;
4
- const commentReg1 = / \/ \* ( [ \s \S ] * ?) \* \/ / g;
5
- const commentReg2 = / \/ \/ ( [ \s \S ] * ?) \n / g;
4
+ export const commentReg = / (?< = \/ \* ) [ \s \S ] * ?(? = \* \/ ) | (?< = \/ \/ ) [ \s \S ] * ?(? = \n ) / g;
6
5
7
- export function * parseCssVars ( styleContent : string ) {
8
- styleContent = clearComments ( styleContent ) ;
9
- const matchs = styleContent . matchAll ( vBindCssVarReg ) ;
6
+ export function * parseCssVars ( css : string ) {
7
+ css = fillBlank ( css , commentReg ) ;
8
+ const matchs = css . matchAll ( vBindCssVarReg ) ;
10
9
for ( const match of matchs ) {
11
10
const matchText = match . slice ( 1 ) . find ( t => t ) ;
12
11
if ( matchText ) {
13
- const offset = match . index + styleContent . slice ( match . index ) . indexOf ( matchText ) ;
12
+ const offset = match . index + css . slice ( match . index ) . indexOf ( matchText ) ;
14
13
yield { offset, text : matchText } ;
15
14
}
16
15
}
17
16
}
18
17
19
- export function clearComments ( css : string ) {
20
- return css
21
- . replace ( commentReg1 , match => `/*${ ' ' . repeat ( match . length - 4 ) } */` )
22
- . replace ( commentReg2 , match => `//${ ' ' . repeat ( match . length - 3 ) } \n` ) ;
23
- }
18
+ export function fillBlank ( css : string , ...regs : RegExp [ ] ) {
19
+ for ( const reg of regs ) {
20
+ css = css . replace ( reg , ( match ) => ' ' . repeat ( match . length ) ) ;
21
+ }
22
+ return css ;
23
+ }
0 commit comments