@@ -11,7 +11,12 @@ class Node {
1111
1212// 提取 css string 为 token
1313function tokenize ( cssString ) {
14- const regex = / \/ \* \s * @ m p x - ( i f | e l i f | e l s e | e n d i f ) (?: \s * \( ( .* ?) \) ) ? \s * \* \/ / g
14+ // Support /* ... */, // ..., and <!-- ... --> styles
15+ // 1. : /\/\*\s*@mpx-(if|elif|else|endif)(?:\s*\(([\s\S]*?)\))?\s*\*\//g
16+ // 2. : /\/\/\s*@mpx-(if|elif|else|endif)(?:\s*\((.*?)\))?\s*$/gm
17+ // 3. : /<!--\s*@mpx-(if|elif|else|endif)(?:\s*\(([\s\S]*?)\))?\s*-->/g
18+ // Combined:
19+ const regex = / (?: \/ \* \s * @ m p x - ( i f | e l i f | e l s e | e n d i f ) (?: \s * \( ( [ \s \S ] * ?) \) ) ? \s * \* \/ ) | (?: \/ \/ \s * @ m p x - ( i f | e l i f | e l s e | e n d i f ) (?: \s * \( ( .* ?) \) ) ? \s * ) | (?: < ! - - \s * @ m p x - ( i f | e l i f | e l s e | e n d i f ) (?: \s * \( ( [ \s \S ] * ?) \) ) ? \s * - - > ) / g
1520 const tokens = [ ]
1621 let lastIndex = 0
1722 let match
@@ -22,11 +27,15 @@ function tokenize(cssString) {
2227 const text = cssString . substring ( lastIndex , match . index )
2328 tokens . push ( { type : 'text' , content : text } )
2429 }
25- // match[1] 为关键字:if, elif, else, endif
26- // match[2] 为条件(如果存在)
30+ // 1,2: (/* ... */)
31+ // 3,4: (// ...)
32+ // 5,6: (<!-- ... -->)
33+ const type = match [ 1 ] || match [ 3 ] || match [ 5 ]
34+ const condition = ( match [ 2 ] || match [ 4 ] || match [ 6 ] )
35+
2736 tokens . push ( {
28- type : match [ 1 ] , // 'if'、'elif'、'else' 或 'endif'
29- condition : match [ 2 ] ? match [ 2 ] . trim ( ) : null
37+ type : type ,
38+ condition : condition ? condition . trim ( ) : null
3039 } )
3140 lastIndex = regex . lastIndex
3241 }
@@ -137,30 +146,30 @@ const rawReadFileSync = fs.readFileSync
137146const rawReadFile = fs . readFile
138147
139148function rewriteFSForCss ( ) {
140- proxyReadFileSync = function ( path , options ) {
141- return rawReadFileSync . call ( fs , path , options )
142- }
143- proxyReadFile = function ( path , options , callback ) {
144- return rawReadFile . call ( fs , path , options , callback )
145- }
146- fs . readFileSync = function ( path , options ) {
147- return proxyReadFileSync ( path , options )
149+ fs . readFileSync = function ( ) {
150+ return ( proxyReadFileSync || rawReadFileSync ) . apply ( fs , arguments )
148151 }
149- fs . readFile = function ( path , options , callback ) {
150- return proxyReadFile ( path , options , callback )
152+ fs . readFile = function ( ) {
153+ return ( proxyReadFile || rawReadFile ) . apply ( fs , arguments )
151154 }
152155}
153156
154157function startFSStripForCss ( defs ) {
155158 function shouldStrip ( path ) {
156- return typeof path === 'string' && / \. ( s t y l | s c s s | s a s s | l e s s | c s s ) $ / . test ( path )
159+ return typeof path === 'string' && / \. ( s t y l | s c s s | s a s s | l e s s | c s s | m p x ) $ / . test ( path )
157160 }
158161 proxyReadFileSync = function ( path , options ) {
159162 const content = rawReadFileSync . call ( fs , path , options )
160163 if ( shouldStrip ( path ) ) {
161164 try {
162165 if ( typeof content === 'string' ) {
163166 return stripCondition ( content , defs )
167+ } else if ( Buffer . isBuffer ( content ) ) {
168+ const str = content . toString ( 'utf-8' )
169+ const result = stripCondition ( str , defs )
170+ if ( result !== str ) {
171+ return Buffer . from ( result , 'utf-8' )
172+ }
164173 }
165174 } catch ( e ) {
166175 return content
@@ -169,31 +178,38 @@ function startFSStripForCss(defs) {
169178 return content
170179 }
171180
172- proxyReadFile = function ( path , options , callback ) {
173- let cb = callback
174- if ( typeof options === 'function' ) {
175- cb = options
176- options = null
181+ proxyReadFile = function ( ) {
182+ const args = Array . from ( arguments )
183+ const callback = args [ args . length - 1 ]
184+ const path = args [ 0 ]
185+
186+ if ( typeof callback !== 'function' ) {
187+ return rawReadFile . apply ( fs , args )
177188 }
178189
179190 const wrappedCallback = ( err , data ) => {
180- if ( err ) return cb ( err )
191+ if ( err ) return callback ( err )
181192 if ( shouldStrip ( path ) ) {
182193 try {
183194 if ( typeof data === 'string' ) {
184195 const result = stripCondition ( data , defs )
185- return cb ( null , result )
196+ return callback ( null , result )
197+ } else if ( Buffer . isBuffer ( data ) ) {
198+ const content = data . toString ( 'utf-8' )
199+ const result = stripCondition ( content , defs )
200+ if ( result !== content ) {
201+ return callback ( null , Buffer . from ( result , 'utf-8' ) )
202+ }
186203 }
187204 } catch ( e ) {
188- return cb ( null , data )
205+ return callback ( null , data )
189206 }
190207 }
191- cb ( null , data )
208+ callback ( null , data )
192209 }
193- if ( options ) {
194- return rawReadFile . call ( fs , path , options , wrappedCallback )
195- }
196- return rawReadFile . call ( fs , path , wrappedCallback )
210+
211+ args [ args . length - 1 ] = wrappedCallback
212+ return rawReadFile . apply ( fs , args )
197213 }
198214}
199215/**
0 commit comments