-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathindex.js
More file actions
38 lines (36 loc) · 1.38 KB
/
index.js
File metadata and controls
38 lines (36 loc) · 1.38 KB
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
const { findExportedComponents } = require('@extract-types/core');
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) {
try {
let components = findExportedComponents(programPath, typeSystem, state.file.filename);
components.forEach(({ name, component }) => {
// TODO: handle when name is null
// it will only happen when it's the default 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
)
)
);
}
});
/* eslint-disable no-empty */
} catch (e) {}
}
}
}
};
};