-
Notifications
You must be signed in to change notification settings - Fork 0
/
transform.ts
62 lines (58 loc) · 2.31 KB
/
transform.ts
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
import * as ts from 'typescript';
declare const process: {env: {NODE_ENV?: string}};
export default function(program: ts.Program, pluginOptions: {productionEnv: string}) {
return (ctx: ts.TransformationContext) => {
return (sourceFile: ts.SourceFile) => {
if (process.env.NODE_ENV !== (pluginOptions.productionEnv || 'production')) return sourceFile;
const checker = program.getTypeChecker();
function visitor(node: ts.Node): ts.Node | undefined {
if (isDebugImport(node)) {
return;
}
if (
ts.isExpressionStatement(node) &&
ts.isCallExpression(node.expression) &&
ts.isIdentifier(node.expression.expression) &&
isDebugSymbol(checker.getSymbolAtLocation(node.expression.expression))
) {
return;
}
if (
ts.isCallExpression(node) &&
ts.isIdentifier(node.expression) &&
isDebugSymbol(checker.getSymbolAtLocation(node.expression))
) {
if (isDebugSymbol(checker.getSymbolAtLocation(node.expression))) {
if (node.arguments.length > 0) {
return node.arguments[0];
} else {
return ts.createVoidZero();
}
}
}
return ts.visitEachChild(node, visitor, ctx);
}
return ts.visitEachChild(sourceFile, visitor, ctx);
};
};
}
function isDebugSymbol(symbol: ts.Symbol | undefined) {
if (symbol !== undefined && isDebugFilename(symbol.declarations[0].getSourceFile().fileName)) {
return true;
}
return (
symbol !== undefined &&
ts.isImportSpecifier(symbol.declarations[0]) &&
isDebugImport(symbol.declarations[0].parent.parent.parent)
);
}
function isDebugFilename(filename: string) {
return Boolean(filename.match(/(^|\W)debug(\.[jt]sx?)?$/));
}
function isDebugImport(node: ts.Node) {
return (
ts.isImportDeclaration(node) &&
ts.isStringLiteral(node.moduleSpecifier) &&
isDebugFilename(node.moduleSpecifier.text)
);
}