@@ -26,8 +26,7 @@ function tokenize(cssString) {
2626 // match[2] 为条件(如果存在)
2727 tokens . push ( {
2828 type : match [ 1 ] , // 'if'、'elif'、'else' 或 'endif'
29- condition : match [ 2 ] ? match [ 2 ] . trim ( ) : null ,
30- rawValue : match [ 0 ]
29+ condition : match [ 2 ] ? match [ 2 ] . trim ( ) : null
3130 } )
3231 lastIndex = regex . lastIndex
3332 }
@@ -52,7 +51,6 @@ function parse(cssString) {
5251 currentChildren . push ( node )
5352 } else if ( token . type === 'if' ) {
5453 const node = new Node ( 'If' , token . condition )
55- node . rawValue = token . rawValue || ''
5654 currentChildren . push ( node )
5755 nodeStack . push ( currentChildren )
5856 currentChildren = node . children
@@ -62,7 +60,6 @@ function parse(cssString) {
6260 }
6361 currentChildren = nodeStack [ nodeStack . length - 1 ]
6462 const node = new Node ( 'ElseIf' , token . condition )
65- node . rawValue = token . rawValue || ''
6663 currentChildren . push ( node )
6764 currentChildren = node . children
6865 } else if ( token . type === 'else' ) {
@@ -71,16 +68,12 @@ function parse(cssString) {
7168 }
7269 currentChildren = nodeStack [ nodeStack . length - 1 ]
7370 const node = new Node ( 'Else' )
74- node . rawValue = token . rawValue || ''
7571 currentChildren . push ( node )
7672 currentChildren = node . children
7773 } else if ( token . type === 'endif' ) {
78- const node = new Node ( 'EndIf' )
79- node . rawValue = token . rawValue || ''
8074 if ( nodeStack . length > 0 ) {
8175 currentChildren = nodeStack . pop ( )
8276 }
83- currentChildren . push ( node )
8477 }
8578 } )
8679 return ast
@@ -109,22 +102,17 @@ function traverseAndEvaluate(ast, defs) {
109102 } else if ( node . type === 'If' ) {
110103 // 直接判断 If 节点
111104 batchedIf = false
112- output += node . rawValue || ''
113105 if ( evaluateCondition ( node . condition , defs ) ) {
114106 traverse ( node . children )
115107 batchedIf = true
116108 }
117109 } else if ( node . type === 'ElseIf' && ! batchedIf ) {
118- output += node . rawValue || ''
119110 if ( evaluateCondition ( node . condition , defs ) ) {
120111 traverse ( node . children )
121112 batchedIf = true
122113 }
123114 } else if ( node . type === 'Else' && ! batchedIf ) {
124- output += node . rawValue || ''
125115 traverse ( node . children )
126- } else if ( node . type === 'EndIf' ) {
127- output += node . rawValue || ''
128116 }
129117 }
130118 }
@@ -140,30 +128,35 @@ function traverseAndEvaluate(ast, defs) {
140128 */
141129function stripCondition ( content , defs ) {
142130 const ast = parse ( content )
143- const result = traverseAndEvaluate ( ast , defs )
144- return result
131+ return traverseAndEvaluate ( ast , defs )
145132}
146- /**
147- * @param {StripByPostcssOption } options
148- */
149- async function stripByPostcss ( options ) {
150- const defs = options . defs ?? { }
151- const afterConditionStrip = stripCondition ( options . css , defs )
152- return {
153- css : afterConditionStrip
133+
134+ let proxyReadFileSync
135+ let proxyReadFile
136+ const rawReadFileSync = fs . readFileSync
137+ const rawReadFile = fs . readFile
138+
139+ function 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 )
148+ }
149+ fs . readFile = function ( path , options , callback ) {
150+ return proxyReadFile ( path , options , callback )
154151 }
155152}
156153
157- function rewriteReadFileSyncForCss ( defs ) {
154+ function startFSStripForCss ( defs ) {
158155 function shouldStrip ( path ) {
159156 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 )
160157 }
161-
162- const readFileSync = fs . readFileSync
163- const readFile = fs . readFile
164-
165- fs . readFileSync = function ( path , options ) {
166- const content = readFileSync . call ( fs , path , options )
158+ proxyReadFileSync = function ( path , options ) {
159+ const content = rawReadFileSync . call ( fs , path , options )
167160 if ( shouldStrip ( path ) ) {
168161 try {
169162 if ( typeof content === 'string' ) {
@@ -176,8 +169,7 @@ function rewriteReadFileSyncForCss(defs) {
176169 return content
177170 }
178171
179- fs . readFile = function ( path , options , callback ) {
180- // 处理参数重载
172+ proxyReadFile = function ( path , options , callback ) {
181173 let cb = callback
182174 if ( typeof options === 'function' ) {
183175 cb = options
@@ -198,11 +190,10 @@ function rewriteReadFileSyncForCss(defs) {
198190 }
199191 cb ( null , data )
200192 }
201-
202193 if ( options ) {
203- return readFile . call ( fs , path , options , wrappedCallback )
194+ return rawReadFile . call ( fs , path , options , wrappedCallback )
204195 }
205- return readFile . call ( fs , path , wrappedCallback )
196+ return rawReadFile . call ( fs , path , wrappedCallback )
206197 }
207198}
208199/**
@@ -212,12 +203,15 @@ function rewriteReadFileSyncForCss(defs) {
212203 */
213204module . exports = async function ( css ) {
214205 this . cacheable ( )
206+
215207 const callback = this . async ( )
208+
216209 const mpx = this . getMpx ( )
217210 const result = stripCondition ( css , mpx . defs )
211+
218212 callback ( null , result )
219213}
220214
221- module . exports . stripByPostcss = stripByPostcss
222215module . exports . stripCondition = stripCondition
223- module . exports . rewriteReadFileSyncForCss = rewriteReadFileSyncForCss
216+ module . exports . rewriteFSForCss = rewriteFSForCss
217+ module . exports . startFSStripForCss = startFSStripForCss
0 commit comments