-
Notifications
You must be signed in to change notification settings - Fork 2
/
macro.js
60 lines (50 loc) · 1.68 KB
/
macro.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
55
56
57
58
59
60
const path = require("path");
const fs = require("fs");
const { createMacro } = require("babel-plugin-macros");
export default createMacro(filesMacros);
function filesMacros({ references, state, babel }) {
references.default.forEach(referencePath => {
if (referencePath.parentPath.type === "CallExpression") {
addFiles({ referencePath, state, babel });
} else {
throw new Error(
`This is not supported: \`${referencePath
.findParent(babel.types.isExpression)
.getSource()}\`. Please see the files.macro documentation`
);
}
});
}
function addFiles({ referencePath, state, babel }) {
// console.log('referencePath', referencePath);
// console.log('state', state);
// console.log('babel', babel);
const filename = state.file.opts.filename;
const dirname = path.dirname(filename);
let dirPath;
const callExpressionPath = referencePath.parentPath;
try {
dirPath = callExpressionPath.get("arguments")[0].evaluate().value;
} catch (err) {
// swallow error, print better error below
}
if (dirPath === undefined) {
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 fullPath = path.join(dirname, dirPath);
let content = "[]";
if (fs.existsSync(fullPath)) {
const files = fs.readdirSync(fullPath);
content = `[ ${files
.map(i => `"${i}"`)
.toString()
.trim()} ]`;
}
const t = babel.types;
referencePath.parentPath.replaceWith(
t.expressionStatement(t.stringLiteral(content))
);
}