-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
29 lines (26 loc) · 1.06 KB
/
index.js
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
module.exports = function ({types: t}) {
return {
visitor: {
Identifier(path) {
// 程序不识别未声明的 DEBUG ,所以要将 DEBUG 转为 "DEBUG"
const parentNodeIsIfStatement = t.isIfStatement(path.parent);
const isDebug = path.node.name === "DEBUG";
if (isDebug && parentNodeIsIfStatement) {
// 把 Identifier 转换成 string
const stringNode = t.StringLiteral("DEBUG");
path.replaceWith(stringNode);
}
},
StringLiteral(path, state) {
const parentNodeIsIfStatement = t.isIfStatement(path.parent);
const isDebug = path.node.value === "DEBUG";
if(isDebug && parentNodeIsIfStatement) {
// 控制 env 是什么环境下来移除
if (process.env.NODE_ENV === state.opts.env) {
path.parentPath.remove();
}
}
}
}
}
}