This repository has been archived by the owner on Feb 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
47 lines (39 loc) · 1.45 KB
/
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const { createMacro, MacroError } = require('babel-plugin-macros')
const plugin = require('react-hot-loader/babel')
function macro({ babel, references, state }) {
if (!references.default) {
throw new MacroError('react-hot-reload.macro requires a default import')
}
const { types: t } = babel
const { node: referenceNode, scope } = references.default[0]
if (process.env.NODE_ENV === 'production') {
state.file.path.traverse({
CallExpression(path) {
if (path.node.callee.name !== referenceNode.name) {
return
}
path.replaceWith(t.identifier(
path.node.arguments[0].name,
))
},
})
scope.removeBinding(referenceNode.name)
} else {
// Do apply react-hot-loader plugin first
plugin(babel)
state.file.path.traverse({
ImportDefaultSpecifier({ node, parentPath }) {
if (node.local.name !== referenceNode.name) {
return
}
const identifier = t.identifier(referenceNode.name)
const declaration = t.importDeclaration(
[t.importSpecifier(identifier, identifier)],
t.stringLiteral('react-hot-loader/root'),
)
parentPath.insertAfter(declaration)
},
})
}
}
module.exports = createMacro(macro)