-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
79 lines (69 loc) · 2.37 KB
/
utils.js
File metadata and controls
79 lines (69 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const isHarmony = (targets) => targets === 'harmony'
const isWx = (targets) => targets === 'wx'
const isHermesJs = (targets) => ['ios', 'android', 'harmony'].includes(targets)
// 比较两个 babel 插件名称是否为同一个插件
function isSamePlugin(pluginA, pluginB) {
return normalizeName(pluginA) === normalizeName(pluginB)
}
// babel 插件名称解析规则,插件可以简写,需考虑完整名称和简写名称
const normalizeName = (name) => {
// 处理 module: 前缀
if (name.startsWith('module:')) {
return name.slice(7) // 移除 "module:" 前缀
}
// 绝对路径或相对路径直接返回
if (name.startsWith('/') || name.startsWith('./')) {
return name
}
// 已经是完整的 babel 插件名称
if (name.startsWith('babel-plugin-') || name.startsWith('@babel/plugin-')) {
return name
}
// @babel/ 开头的包名
if (name.startsWith('@babel/')) {
// 检查是否包含子路径
const parts = name.split('/')
if (parts.length > 2) {
return name // 有子路径,直接返回
}
return `@babel/plugin-${parts[1]}`
}
// 其他 scoped package
if (name.startsWith('@')) {
const parts = name.split('/')
if (parts.length === 1) {
// 只有 scope 名称
return `${parts[0]}/babel-plugin`
} else if (parts.length > 2) {
// 有子路径,直接返回
return name
} else {
// @scope/mod 的情况
const scopeName = parts[0]
const modName = parts[1]
// 检查是否已经是 babel-plugin- 格式
if (modName.startsWith('babel-plugin-')) {
return name
} else if (modName.includes('babel-plugin-')) {
// prefix-babel-plugin-mod 的情况
return name
} else {
return `${scopeName}/babel-plugin-${modName}`
}
}
}
// 普通包名,添加 babel-plugin- 前缀
return `babel-plugin-${name}`
}
const includePlugin = (plugins, plugin) => {
const normalizedPlugin = normalizeName(plugin)
return !plugins.some(p => isSamePlugin(p, normalizedPlugin))
}
module.exports = {
isHarmony,
isWx,
isHermesJs,
isSamePlugin,
normalizeName,
includePlugin
}