-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
50 lines (49 loc) · 1.83 KB
/
Copy pathindex.js
File metadata and controls
50 lines (49 loc) · 1.83 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
module.exports = function(babel) {
const { types: t } = babel;
const isProduction = process.env.NODE_ENV === 'production';
return {
visitor: {
IfStatement(path, state) {
// 处理if判断
if (t.isIdentifier(path.node.test, { name: 'DEBUG' })) {
if (isProduction) {
path.remove();
} else {
const { consequent, alternate } = path.node;
path.replaceWith(t.ifStatement(t.booleanLiteral(true), consequent, alternate));
}
}
},
UnaryExpression(path) {
// 处理一元表达式
const { operator, argument } = path.node;
if (operator === "!" && t.isIdentifier(argument, { name: "DEBUG" })) {
path.replaceWith(isProduction);
}
},
ConditionalExpression(path) {
// 处理三元运算符
const { test, consequent, alternate } = path.node;
if (t.isIdentifier(test, { name: "DEBUG" })) {
const replacement = t.conditionalExpression(
t.booleanLiteral(isProduction ? false : true),
consequent,
alternate
)
path.replaceWith(replacement);
}
},
LogicalExpression(path) {
// 处理逻辑运算符
const { left, right, operator } = path.node;
if (operator === "&&") {
const condition = left;
const trueBlock = t.blockStatement([t.returnStatement(right)]);
const falseBlock = t.blockStatement([t.returnStatement(t.booleanLiteral(false))]);
const ifStatement = t.ifStatement(condition, trueBlock, falseBlock);
path.replaceWith(ifStatement);
}
}
}
};
};