Skip to content

Commit c4d39b2

Browse files
committed
feat: 调整fs readfile代理逻辑
1 parent bbf6dc2 commit c4d39b2

File tree

2 files changed

+35
-41
lines changed

2 files changed

+35
-41
lines changed

packages/webpack-plugin/lib/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ const LoadAsyncChunkModule = require('./react/LoadAsyncChunkModule')
7979
const ExternalModule = require('webpack/lib/ExternalModule')
8080
const { RetryRuntimeModule, RetryRuntimeGlobal } = require('./dependencies/RetryRuntimeModule')
8181
const checkVersionCompatibility = require('./utils/check-core-version-match')
82-
const { rewriteReadFileSyncForCss } = require('./style-compiler/strip-conditional-loader')
83-
82+
const { rewriteFSForCss, startFSStripForCss } = require('./style-compiler/strip-conditional-loader')
83+
rewriteFSForCss()
8484
checkVersionCompatibility()
8585

8686
const isProductionLikeMode = options => {
@@ -324,6 +324,8 @@ class MpxWebpackPlugin {
324324
}
325325

326326
apply (compiler) {
327+
// 注入 fs 代理
328+
startFSStripForCss(this.options.defs)
327329

328330
if (!compiler.__mpx__) {
329331
compiler.__mpx__ = true
@@ -479,8 +481,6 @@ class MpxWebpackPlugin {
479481
defsOpt[key] = JSON.stringify(defs[key])
480482
})
481483

482-
rewriteReadFileSyncForCss(defs)
483-
484484
// define mode & defs
485485
new DefinePlugin(defsOpt).apply(compiler)
486486

packages/webpack-plugin/lib/style-compiler/strip-conditional-loader.js

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*/
141129
function 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' && /\.(styl|scss|sass|less|css)$/.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
*/
213204
module.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
222215
module.exports.stripCondition = stripCondition
223-
module.exports.rewriteReadFileSyncForCss = rewriteReadFileSyncForCss
216+
module.exports.rewriteFSForCss = rewriteFSForCss
217+
module.exports.startFSStripForCss = startFSStripForCss

0 commit comments

Comments
 (0)