-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
32 lines (30 loc) · 783 Bytes
/
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
30
31
32
module.exports = function (babel) {
var makeRequire = babel.template(`
require('split-require')
`)
return {
inherits: require('babel-plugin-syntax-dynamic-import'),
visitor: {
Program: {
enter: function (path) {
this.helperId = path.scope.generateUidIdentifier('import')
this.usesImport = false
},
exit: function (path) {
if (this.usesImport) {
path.scope.push({
id: this.helperId,
init: makeRequire().expression
})
}
}
},
CallExpression: function (path) {
if (path.get('callee').isImport()) {
this.usesImport = true
path.get('callee').replaceWith(this.helperId)
}
}
}
}
}