Skip to content

Commit f3183db

Browse files
committed
feat: 支持多种格式注释&支持Mpx文件过滤
1 parent fa451cf commit f3183db

File tree

3 files changed

+229
-134
lines changed

3 files changed

+229
-134
lines changed

packages/webpack-plugin/lib/index.js

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ const wxssLoaderPath = normalize.lib('wxss/index')
5858
const wxmlLoaderPath = normalize.lib('wxml/loader')
5959
const wxsLoaderPath = normalize.lib('wxs/loader')
6060
const styleCompilerPath = normalize.lib('style-compiler/index')
61-
const styleStripConditionalPath = normalize.lib('style-compiler/strip-conditional-loader')
6261
const templateCompilerPath = normalize.lib('template-compiler/index')
6362
const jsonCompilerPath = normalize.lib('json-compiler/index')
6463
const jsonThemeCompilerPath = normalize.lib('json-compiler/theme')
@@ -1918,42 +1917,9 @@ try {
19181917
normalModuleFactory.hooks.afterResolve.tap('MpxWebpackPlugin', ({ createData }) => {
19191918
const { queryObj } = parseRequest(createData.request)
19201919
const loaders = createData.loaders
1921-
1922-
// 样式 loader 类型检测和条件编译 loader 插入的工具函数
1923-
const STYLE_LOADER_TYPES = ['stylus-loader', 'sass-loader', 'less-loader', 'css-loader', wxssLoaderPath]
1924-
const injectStyleStripLoader = (loaders) => {
1925-
// 检查是否已经存在 stripLoader
1926-
const hasStripLoader = loaders.some(loader => {
1927-
const loaderPath = toPosix(loader.loader)
1928-
return loaderPath.includes('style-compiler/strip-conditional-loader')
1929-
})
1930-
if (hasStripLoader) {
1931-
return
1932-
}
1933-
const loaderTypes = new Map(STYLE_LOADER_TYPES.map(type => [`node_modules/${type}`, -1]))
1934-
loaders.forEach((loader, index) => {
1935-
const currentLoader = toPosix(loader.loader)
1936-
for (const [key] of loaderTypes) {
1937-
if (currentLoader.includes(key)) {
1938-
loaderTypes.set(key, index)
1939-
break
1940-
}
1941-
}
1942-
})
1943-
const targetIndex = STYLE_LOADER_TYPES
1944-
.map(type => loaderTypes.get(`node_modules/${type}`))
1945-
.find(index => index !== -1)
1946-
1947-
if (targetIndex !== undefined) {
1948-
loaders.splice(targetIndex + 1, 0, { loader: styleStripConditionalPath })
1949-
}
1950-
}
19511920
if (queryObj.mpx && queryObj.mpx !== MPX_PROCESSED_FLAG) {
19521921
const type = queryObj.type
19531922
const extract = queryObj.extract
1954-
if (type === 'styles') {
1955-
injectStyleStripLoader(loaders)
1956-
}
19571923

19581924
switch (type) {
19591925
case 'styles':
@@ -2006,7 +1972,6 @@ try {
20061972
}
20071973
// mpxStyleOptions 为 mpx style 文件的标识,避免 Vue 文件插入 styleCompiler 后导致 vue scoped 样式隔离失效
20081974
if (isWeb(mpx.mode) && queryObj.mpxStyleOptions) {
2009-
injectStyleStripLoader(loaders)
20101975
const firstLoader = loaders[0] ? toPosix(loaders[0].loader) : ''
20111976
const isPitcherRequest = firstLoader.includes('node_modules/vue-loader/lib/loaders/pitcher')
20121977
let cssLoaderIndex = -1

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

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ class Node {
1111

1212
// 提取 css string 为 token
1313
function tokenize(cssString) {
14-
const regex = /\/\*\s*@mpx-(if|elif|else|endif)(?:\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*@mpx-(if|elif|else|endif)(?:\s*\(([\s\S]*?)\))?\s*\*\/)|(?:\/\/\s*@mpx-(if|elif|else|endif)(?:\s*\((.*?)\))?\s*)|(?:<!--\s*@mpx-(if|elif|else|endif)(?:\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
137146
const rawReadFile = fs.readFile
138147

139148
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)
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

154157
function startFSStripForCss(defs) {
155158
function shouldStrip(path) {
156-
return typeof path === 'string' && /\.(styl|scss|sass|less|css)$/.test(path)
159+
return typeof path === 'string' && /\.(styl|scss|sass|less|css|mpx)$/.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

Comments
 (0)