-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathindex.js
48 lines (45 loc) · 1.71 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
48
const { findExportedComponents } = require('extract-react-types');
module.exports = babel => {
let t = babel.types;
return {
visitor: {
Program(programPath, state) {
let typeSystem = state.file.opts.parserOpts.plugins
.map(plugin => (Array.isArray(plugin) ? plugin[0] : plugin))
.find(plugin => plugin === 'flow' || plugin === 'typescript');
if (!typeSystem) return;
try {
findExportedComponents(programPath, typeSystem, state.file.filename).forEach(
({ name, component }) => {
// TODO: handle when name is null
// it will only happen when it's a default and anonymous export
// generate something like this
// export default (var someName = function() {}, someName.___types = theTypes, someName)
if (name !== null) {
programPath.node.body.push(
t.expressionStatement(
t.assignmentExpression(
'=',
t.memberExpression(t.identifier(name), t.identifier('___types')),
babel.parse(`(${JSON.stringify(component)})`).program.body[0].expression
)
)
);
programPath.node.body.push(
t.expressionStatement(
t.assignmentExpression(
'=',
t.memberExpression(t.identifier(name), t.identifier('___displayName')),
t.stringLiteral(name)
)
)
);
}
}
);
/* eslint-disable no-empty */
} catch (e) {}
}
}
};
};