-
Notifications
You must be signed in to change notification settings - Fork 0
/
transform.js
54 lines (50 loc) · 1.65 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
42
43
44
45
46
47
48
49
50
51
52
53
54
/** This babel-plugin adds
import { hot } from 'react-hot-loader';
export default hot(module)(__original_default_export)
*/
const p = require("path");
module.exports = function(babel) {
const { types: t } = babel;
return {
name: "Hot Export React Components",
visitor: {
Program(path, state) {
if (
state.opts.files.every(file => p.resolve(file) !== p.resolve(state.file.opts.filename))
) {
return;
}
path.unshiftContainer(
"body",
t.ImportDeclaration(
[t.ImportSpecifier(t.Identifier("hot"), t.Identifier("hot"))],
t.StringLiteral("react-hot-loader")
)
);
},
ExportDefaultDeclaration(path, state) {
if (
state.opts.files.every(file => p.resolve(file) !== p.resolve(state.file.opts.filename))
) {
return;
}
const declarationPath = path.get("declaration");
if (t.isIdentifier(declarationPath) || t.isExpression(declarationPath)) {
path.node.declaration = hotExpression(t, declarationPath.node);
} else if (
t.isClassDeclaration(declarationPath) ||
t.isFunctionDeclaration(declarationPath)
) {
const declaration = declarationPath.node;
const id = declaration.id || path.scope.generateUidIdentifierBasedOnNode(path.node.id);
declaration.id = id;
path.insertBefore(declaration);
path.node.declaration = hotExpression(t, id);
}
}
}
};
};
function hotExpression(t, id) {
return t.CallExpression(t.CallExpression(t.Identifier("hot"), [t.Identifier("module")]), [id]);
}