-
Notifications
You must be signed in to change notification settings - Fork 1
/
macro.js
155 lines (143 loc) · 4.57 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
const { createMacro } = require('babel-plugin-macros');
const template = require('@babel/template');
const t = require('@babel/types');
const _ = require('lodash');
function Error(path, msg) {
throw path.buildCodeFrameError('\nMacro error: \x1b[31m' + msg + '\x1b[0m');
}
function hasImported(state, path, source, importConfig) {
if (importConfig[1].transformSource) {
source = importConfig[1].transformSource(source);
}
return state.file.path.get('body').find(path => {
if (path.isImportDeclaration()) {
return source === path.get('source.value').node;
} else {
return false;
}
});
}
function addImport(
{ state, config },
{ importConfig, path, source, filename, options }
) {
if (importConfig[1].transformSource) {
source = importConfig[1].transformSource(source);
}
options = { isDefaultExport: true, ...options };
if (!hasImported(state, path, source, importConfig)) {
const importNode = t.importDeclaration(
[
options.isDefaultExport
? t.importDefaultSpecifier(t.identifier(filename))
: t.importSpecifier(t.identifier(filename), t.identifier(filename))
],
t.stringLiteral(source)
);
state.file.path.unshiftContainer('body', importNode);
}
}
function replaceNode(context, { path, importConfig, filename, args = [] }) {
let options = importConfig[1];
const { config, state } = context;
let iconNode;
if (filename.startsWith('@')) {
options = {
...options,
...((args[2] && args[2].evaluate().value) || {})
};
let props = args[1];
if (props && t.isObjectExpression(props)) {
props = props.node.properties.map(({ key, value }) =>
t.jsxAttribute(
t.jsxIdentifier(key.name),
t.jsxExpressionContainer(value)
)
);
}
filename = filename.slice(1);
iconNode = t.jsxElement(
t.jsxOpeningElement(t.jsxIdentifier(filename), props || [], true),
null,
[],
true
);
} else {
options = {
...options,
...((args[1] && args[1].evaluate().value) || {})
};
iconNode = t.identifier(filename);
}
const source = options.prefix + '/' + filename;
addImport(context, {
path,
source,
filename,
options,
importConfig
});
path.parentPath.replaceWith(iconNode);
}
module.exports = createMacro(
context => {
const { references, state, config } = context;
const programPath = state.file.path;
references.default &&
references.default.forEach(path => {
if (path.parentPath.isCallExpression()) {
const args = path.parentPath.get('arguments');
const options = {
...(config.defaultImport || {}),
...((args[1] && args[1].evaluate().value) || {})
};
let source = args[0].evaluate().value;
source =
config.defaultImport && config.defaultImport.prefix
? config.defaultImport.prefix + '/' + source
: source;
const importConfig = [null, config.defaultImport || {}];
const matchedImport = hasImported(state, path, source, importConfig);
let filename;
if (matchedImport) {
filename = matchedImport.node.specifiers[0].local.name;
} else {
filename = path.parentPath.scope.generateUid(source);
addImport(context, {
path,
source,
filename,
options,
importConfig
});
}
path.parentPath.replaceWith(t.identifier(filename));
}
});
config.imports.forEach(importConfig => {
if (!Array.isArray(importConfig)) {
Error(programPath, "importHelper.imports's item must be array");
} else {
if (!importConfig[1] || !importConfig[1].prefix) {
Error(
programPath,
"importHelper.imports's item option must have filed [prefix]"
);
}
}
references[importConfig[0]] &&
references[importConfig[0]].forEach(path => {
if (path.parentPath.isTaggedTemplateExpression()) {
const quasiPath = path.parentPath.get('quasi.quasis');
let filename = quasiPath[0].node.value.cooked;
replaceNode(context, { path, importConfig, filename });
} else if (path.parentPath.isCallExpression()) {
const args = path.parentPath.get('arguments');
const filename = args[0].evaluate().value;
replaceNode(context, { path, importConfig, filename, args });
}
});
});
},
{ configName: 'importHelper' }
);