-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
31 lines (28 loc) · 1009 Bytes
/
index.js
File metadata and controls
31 lines (28 loc) · 1009 Bytes
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
const { createMacro } = require('babel-plugin-macros');
const getImportSource = (callExpressionPath) => {
try {
return path = callExpressionPath.get('arguments')[0].evaluate().value
} catch (error) {
throw new Error(
`There was a problem evaluating the value of the argument for the code: ${callExpressionPath.getSource()}. ` +
`If the value is dynamic, please make sure that its value is statically deterministic.`,
)
}
}
const macro = ({
references, state, babel
}) => {
references.default.forEach(referencePath => {
const { types: t } = babel;
const source = getImportSource(referencePath.parentPath);
const id = referencePath.scope.generateUidIdentifier(source);
const importNode = t.importDeclaration(
[t.importDefaultSpecifier(id)],
t.stringLiteral(source),
);
const program = state.file.path;
program.node.body.unshift(importNode);
referencePath.parentPath.replaceWith(id);
});
};
module.exports = createMacro(macro);