-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransform.js
41 lines (39 loc) · 1.19 KB
/
transform.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
30
31
32
33
34
35
36
37
38
39
40
41
module.exports = function(babel) {
const types = babel.types;
const isModulesRequire = types.buildMatchMemberExpression('modules.require');
const isModulesDefine = types.buildMatchMemberExpression('modules.define');
function importYM() {
return types.variableDeclaration(
'var',
[types.variableDeclarator(
types.identifier('modules'),
types.callExpression(
types.identifier('require'),
[types.stringLiteral('ym')]
)
)]
);
}
return {
visitor: {
Program: {
enter() {
this.ymFound = false;
},
exit(path) {
if(this.ymFound) {
path.node.body.unshift(importYM());
}
}
},
CallExpression: {
enter(path) {
const callee = path.node.callee;
if(isModulesDefine(callee) || isModulesRequire(callee)) {
this.ymFound = true;
}
}
}
}
}
};