-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmacro.js
63 lines (56 loc) · 2.14 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
61
62
63
// @flow
import fs from 'fs';
import { execSync } from 'child_process';
import { createMacro } from 'babel-plugin-macros';
import { parse } from 'babylon';
import glob from 'glob';
import type { Babel, BabelPluginPass } from 'babel-flow-types';
import importReact from './utils/importReact';
import getArguments from './utils/getArguments';
import optionToCLI from './utils/optionToCLI';
// import printAST from 'ast-pretty-print';
// console.log(printAST(referencePath.parentPath));
function svgrMacro({
references,
state,
state: { file: { opts: { filename } } },
babel: { types: t },
}: {
references: { default: Array<any> },
state: BabelPluginPass,
babel: Babel,
}): void {
const { default: toReactComponent = [] } = references;
// Node: add react on the top of file if it have not been imported.
importReact(state, t);
toReactComponent.forEach(referencePath => {
const [svgPath, options] = getArguments(referencePath, filename);
const cliArguments = optionToCLI(options);
const isFileExists = fs.existsSync(svgPath);
if (isFileExists) {
// Case 1: single file
const jsCode = execSync(`svgr ${svgPath} ${cliArguments}`).toString();
const ast = parse(jsCode, { sourceType: 'module', plugins: ['jsx'] });
const arrowFunctionExpression = ast.program.body[1].declarations[0].init;
referencePath.parentPath.replaceWith(arrowFunctionExpression);
} else {
// Case 2: glob pattern
const objectProperties = glob.sync(svgPath).map(f => {
// TODO: merge multiple files and run svgr at same time.
const jsCode = execSync(`svgr ${f} ${cliArguments}`).toString();
const ast = parse(jsCode, { sourceType: 'module', plugins: ['jsx'] });
const componentName = ast.program.body[1].declarations[0].id.name;
const arrowFunctionExpression =
ast.program.body[1].declarations[0].init;
return t.objectProperty(
t.stringLiteral(componentName),
arrowFunctionExpression,
);
});
referencePath.parentPath.replaceWith(
t.objectExpression(objectProperties),
);
}
});
}
export default createMacro(svgrMacro);