Skip to content

Commit 83bffe4

Browse files
anOrangeluckyadam
authored andcommitted
feat(cli): 项目配置中支持添加 pathAlias 配置 import 路径自定义别名 (#1401)
1 parent d950033 commit 83bffe4

File tree

4 files changed

+46
-4
lines changed

4 files changed

+46
-4
lines changed

packages/taro-cli/src/h5.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,16 @@ function processEntry (code, filePath) {
369369
enter (astPath) {
370370
const node = astPath.node
371371
const source = node.source
372-
const value = source.value
373372
const specifiers = node.specifiers
373+
let value = source.value
374+
const pathAlias = projectConfig.pathAlias || {}
375+
if (Util.isAliasPath(value, Object.keys(pathAlias))) {
376+
let reg = new RegExp(`^(${Object.keys(pathAlias).join('|')})/`)
377+
value = value.replace(reg, function (matched, $1) {
378+
return './' + path.relative(path.dirname(filePath), sourceDir) + pathAlias[$1] + '/'
379+
})
380+
source.value = value
381+
}
374382
if (!Util.isNpmPkg(value)) {
375383
if (value.indexOf('.') === 0) {
376384
const pathArr = value.split('/')
@@ -631,8 +639,16 @@ function processOthers (code, filePath, fileType) {
631639
enter (astPath) {
632640
const node = astPath.node
633641
const source = node.source
634-
const value = source.value
642+
let value = source.value
635643
const specifiers = node.specifiers
644+
const pathAlias = projectConfig.pathAlias || {}
645+
if (Util.isAliasPath(value, Object.keys(pathAlias))) {
646+
let reg = new RegExp(`^(${Object.keys(pathAlias).join('|')})/`)
647+
value = value.replace(reg, function (matched, $1) {
648+
return path.relative(path.dirname(filePath), sourceDir) + pathAlias[$1] + '/'
649+
})
650+
source.value = value
651+
}
636652
if (!Util.isNpmPkg(value)) {
637653
if (Util.REG_SCRIPTS.test(value)) {
638654
const dirname = path.dirname(value)

packages/taro-cli/src/rn/transformJS.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const wxTransformer = require('@tarojs/transformer-wx')
99
const Util = require('../util')
1010
const babylonConfig = require('../config/babylon')
1111
const {source: toAst} = require('../util/ast_convert')
12+
const CONFIG = require('../config')
1213

1314
const reactImportDefaultName = 'React'
1415
let taroImportDefaultName // import default from @tarojs/taro
@@ -216,10 +217,19 @@ function parseJSCode ({code, filePath, isEntryFile, projectConfig}) {
216217
ImportDeclaration (astPath) {
217218
const node = astPath.node
218219
const source = node.source
219-
const value = source.value
220+
let value = source.value
220221
const valueExtname = path.extname(value)
221222
const specifiers = node.specifiers
222-
223+
const pathAlias = projectConfig.pathAlias || {}
224+
if (Util.isAliasPath(value, Object.keys(pathAlias))) {
225+
let reg = new RegExp(`^(${Object.keys(pathAlias).join('|')})/`)
226+
value = value.replace(reg, function (matched, $1) {
227+
const sourceDirName = projectConfig.sourceRoot || CONFIG.SOURCE_DIR
228+
const sourceDir = path.join(process.cwd(), sourceDirName)
229+
return './' + path.relative(path.dirname(filePath), sourceDir) + pathAlias[$1] + '/'
230+
})
231+
source.value = value
232+
}
223233
// 引入的包为 npm 包
224234
if (!Util.isNpmPkg(value)) {
225235
// import 样式处理

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,13 @@ exports.isNpmPkg = function (name) {
185185
return true
186186
}
187187

188+
exports.isAliasPath = function (name, prefixs = []) {
189+
if (prefixs.length === 0) {
190+
return false
191+
}
192+
return new RegExp(`^(${prefixs.join('|')})/`).test(name)
193+
}
194+
188195
exports.promoteRelativePath = function (fPath) {
189196
const fPathArr = fPath.split(path.sep)
190197
let dotCount = 0

packages/taro-cli/src/weapp.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,15 @@ function parseAst (type, ast, depComponents, sourceFilePath, filePath, npmSkip =
383383
const source = node.source
384384
let value = source.value
385385
const specifiers = node.specifiers
386+
// 替换用户自定义前缀
387+
let pathAlias = projectConfig.pathAlias || {}
388+
if (Util.isAliasPath(value, Object.keys(pathAlias))) {
389+
let reg = new RegExp(`^(${Object.keys(pathAlias).join('|')})/`)
390+
value = value.replace(reg, function (matched, $1) {
391+
return './' + path.relative(path.dirname(sourceFilePath), sourceDir) + pathAlias[$1] + '/'
392+
})
393+
source.value = value
394+
}
386395
if (Util.isNpmPkg(value) && notExistNpmList.indexOf(value) < 0) {
387396
if (value === taroJsComponents) {
388397
astPath.remove()

0 commit comments

Comments
 (0)