Skip to content

Commit 86cc770

Browse files
committed
feat(cli): 支持编译样式文件中 import 的样式文件,closes #746closes #758
1 parent b065c9a commit 86cc770

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

packages/taro-cli/src/util/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,16 +397,20 @@ exports.cssImports = function (content) {
397397

398398
exports.processStyleImports = function (content, adapter) {
399399
const style = []
400+
const imports = []
401+
const styleReg = new RegExp(`\.${exports.MINI_APP_FILES[adapter].STYLE}`)
400402
content = content.replace(exports.CSS_IMPORT_REG, (m, $1, $2) => {
401-
if (new RegExp(`\.${adapter}`).test($2)) {
403+
if (styleReg.test($2)) {
402404
style.push(m)
405+
imports.push($2)
403406
return ''
404407
}
405408
return m
406409
})
407410
return {
408411
content,
409-
style
412+
style,
413+
imports
410414
}
411415
}
412416

packages/taro-cli/src/weapp.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,6 +1258,17 @@ async function processStyleWithPostCSS (styleObj) {
12581258
return postcssResult.css
12591259
}
12601260

1261+
function compileImportStyles (filePath, importStyles) {
1262+
if (importStyles.length) {
1263+
importStyles.forEach(async importItem => {
1264+
const importFilePath = path.resolve(filePath, '..', importItem)
1265+
if (fs.existsSync(importFilePath)) {
1266+
await compileDepStyles(importFilePath.replace(sourceDir, outputDir), [importFilePath])
1267+
}
1268+
})
1269+
}
1270+
}
1271+
12611272
function compileDepStyles (outputFilePath, styleFiles, isComponent) {
12621273
if (isBuildingStyles[outputFilePath]) {
12631274
return Promise.resolve({})
@@ -1269,6 +1280,7 @@ function compileDepStyles (outputFilePath, styleFiles, isComponent) {
12691280
const pluginName = Util.FILE_PROCESSOR_MAP[fileExt]
12701281
const fileContent = fs.readFileSync(filePath).toString()
12711282
const cssImportsRes = Util.processStyleImports(fileContent, buildAdapter)
1283+
compileImportStyles(filePath, cssImportsRes.imports)
12721284
if (pluginName) {
12731285
return npmProcess.callPlugin(pluginName, cssImportsRes.content, filePath, pluginsConfig[pluginName] || {})
12741286
.then(res => ({

0 commit comments

Comments
 (0)