-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
48 lines (39 loc) · 1.62 KB
/
index.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
import ts = require("typescript");
type SourceFileTransformerFactory = ts.TransformerFactory<ts.SourceFile>
export const removeExportTransformerFactory:SourceFileTransformerFactory=(context: ts.TransformationContext)=>{
return (node:ts.SourceFile)=>{
const visitor:ts.Visitor=(node:ts.Node)=>{
if(ts.isFunctionDeclaration(node)){
return ts.visitEachChild(node,visitor,context);
}
if(ts.isModifier(node)){
return undefined;
}
return node;
};
return ts.visitEachChild(node,visitor,context);
}
}
export const remove__esModuleFactory:SourceFileTransformerFactory=(context: ts.TransformationContext)=>{
const previousOnSubstituteNode=context.onSubstituteNode;
context.enableSubstitution(ts.SyntaxKind.ExpressionStatement);
context.onSubstituteNode=((hint,node)=>{
node=previousOnSubstituteNode(hint,node);
if(ts.isExpressionStatement(node)&&ts.isBinaryExpression(node.expression)&&ts.isPropertyAccessExpression(node.expression.left)){
let pe:ts.PropertyAccessExpression=node.expression.left;
if(pe.name.text==="__esModule"){
return ts.createEmptyStatement();
}
}
return node;
});
return node=>node;
}
function getCustomTransformersInternal(){
const customTransformers:ts.CustomTransformers={
before:[removeExportTransformerFactory],
after:[remove__esModuleFactory]
}
return customTransformers;
}
export const getCustomTransformers=getCustomTransformersInternal as any;