-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsvgrc-template.js
80 lines (69 loc) · 1.66 KB
/
svgrc-template.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const memoImportAST = {
source: { type: "StringLiteral", value: "react" },
specifiers: [
{
imported: {
name: "memo",
type: "Identifier",
},
local: {
name: "memo",
type: "Identifier",
},
type: "ImportSpecifier",
},
],
type: "ImportDeclaration",
};
const memoCallAST = {
callee: {
name: "memo",
type: "Identifier",
},
type: "CallExpression",
};
function template(
{ template },
opts,
{ componentName, exports, imports, interfaces, jsx, props }
) {
const plugins = ["jsx"];
if (opts.typescript) {
plugins.push("typescript");
}
const templatingEngine = template.smart({ plugins });
// Filter out React import, since we use automatic jsx runtime
imports = (imports || []).filter(
({ source }) => (source?.value ?? "").toLowerCase() !== "react"
);
if (opts.memo) {
imports.push(memoImportAST);
}
exports = (exports || []).map((exportAST) =>
exportAST.type === "VariableDeclaration"
? {
...exportAST,
declarations: exportAST.declarations.map((declaration) => {
if (
declaration.init.callee.object.name === "React" &&
declaration.init.callee.property.name === "memo"
) {
return {
...declaration,
init: { ...declaration.init, ...memoCallAST },
};
}
return declaration;
}),
}
: exportAST
);
const templatedComponent = templatingEngine.ast`${imports}
const ${componentName} = (${props}) =>
${jsx}
;
${exports}
`;
return templatedComponent;
}
exports.default = template;