index.js
10.9 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.specifiersToProperties = specifiersToProperties;
exports.default = _default;
function _react() {
const data = _interopRequireDefault(require("react"));
_react = function _react() {
return data;
};
return data;
}
function _utils() {
const data = require("@umijs/utils");
_utils = function _utils() {
return data;
};
return data;
}
function _path() {
const data = require("path");
_path = function _path() {
return data;
};
return data;
}
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function specifiersToProperties(specifiers) {
return specifiers.reduce((memo, s) => {
if (_utils().t.isImportDefaultSpecifier(s)) {
memo.properties.push(_utils().t.objectProperty(_utils().t.identifier('default'), s.local));
} else if (_utils().t.isExportDefaultSpecifier(s)) {
memo.properties.push(_utils().t.objectProperty(_utils().t.identifier('default'), s.exported));
} else if (_utils().t.isExportSpecifier(s)) {
memo.properties.push(_utils().t.objectProperty(s.local, s.exported));
} else if (_utils().t.isImportNamespaceSpecifier(s)) {
memo.namespaceIdentifier = s.local;
} else {
memo.properties.push(_utils().t.objectProperty(s.imported, s.local));
}
return memo;
}, {
properties: [],
namespaceIdentifier: null
});
}
const RE_NODE_MODULES = /node_modules/;
const RE_UMI_LOCAL_DEV = /umi\/packages\//;
function getAlias(opts) {
for (var _i = 0, _Object$keys = Object.keys(opts.webpackAlias); _i < _Object$keys.length; _i++) {
const key = _Object$keys[_i];
const value = opts.webpackAlias[key]; // exact alias
// ref: https://webpack.js.org/configuration/resolve/#resolvealias
if (key.endsWith('$')) {
if (opts.path === key.slice(0, -1)) return value;
continue;
}
if (opts.path === key) {
return value;
}
const path = isJSFile(opts.webpackAlias[key]) ? key : addLastSlash(key);
if (opts.path.startsWith(path)) {
return value;
}
}
return null;
}
function isJSFile(path) {
return ['.js', '.jsx', '.ts', '.tsx'].includes((0, _path().extname)(path));
}
function addLastSlash(path) {
return path.endsWith('/') ? path : `${path}/`;
}
function isMatchLib(path, libs, matchAll, remoteName, alias, webpackAlias, webpackExternals) {
if (matchAll) {
if (path === 'umi' || path === 'dumi' || path === '@alipay/bigfish') return false;
if (path.startsWith(`${remoteName}/`)) return false; // don't match dynamic path
// e.g. @umijs/deps/compiled/babel/svgr-webpack.js?-svgo,+titleProp,+ref!./umi.svg
if (path.includes('babel/svgr-webpack')) return false; // don't match webpack loader
// e.g. !!dumi-raw-code-loader!/path/to/VerticalProgress/index.module.less?dumi-raw-code
if (path.startsWith('!!')) return false; // TODO: support more external types
if (typeof webpackExternals === 'object' && webpackExternals[path]) {
return false;
}
if ((0, _path().isAbsolute)(path)) {
return RE_NODE_MODULES.test(path) || RE_UMI_LOCAL_DEV.test(path);
} else if (path.charAt(0) === '.') {
return false;
} else {
const aliasPath = getAlias({
path,
webpackAlias
});
if (aliasPath) {
return RE_NODE_MODULES.test(aliasPath) || RE_UMI_LOCAL_DEV.test(aliasPath);
}
return true;
}
}
return (libs || []).concat(Object.keys(alias)).some(lib => {
if (typeof lib === 'string') {
return lib === path;
} else if (lib instanceof RegExp) {
return lib.test(path);
} else {
throw new Error('Unsupported lib format.');
}
});
}
function getPath(path, alias) {
const keys = Object.keys(alias);
for (var _i2 = 0, _keys = keys; _i2 < _keys.length; _i2++) {
const key = _keys[_i2];
if (path.startsWith(key)) {
return path.replace(key, alias[key]);
}
}
return path;
}
function _default() {
return {
visitor: {
Program: {
exit(path, {
opts
}) {
const variableDeclarations = [];
let index = path.node.body.length - 1;
while (index >= 0) {
const d = path.node.body[index];
if (_utils().t.isImportDeclaration(d)) {
var _opts$onTransformDeps;
const isMatch = isMatchLib(d.source.value, opts.libs, opts.matchAll, opts.remoteName, opts.alias || {}, opts.webpackAlias || {}, opts.webpackExternals || {});
(_opts$onTransformDeps = opts.onTransformDeps) === null || _opts$onTransformDeps === void 0 ? void 0 : _opts$onTransformDeps.call(opts, {
source: d.source.value,
// @ts-ignore
file: path.hub.file.opts.filename,
isMatch
});
if (isMatch) {
const _specifiersToProperti = specifiersToProperties(d.specifiers),
properties = _specifiersToProperti.properties,
namespaceIdentifier = _specifiersToProperti.namespaceIdentifier;
const id = _utils().t.objectPattern(properties);
const init = _utils().t.awaitExpression(_utils().t.callExpression(_utils().t.import(), [_utils().t.stringLiteral(`${opts.remoteName}/${getPath(d.source.value, opts.alias || {})}`)]));
if (namespaceIdentifier) {
if (properties.length) {
variableDeclarations.unshift(_utils().t.variableDeclaration('const', [_utils().t.variableDeclarator(id, namespaceIdentifier)]));
}
variableDeclarations.unshift(_utils().t.variableDeclaration('const', [_utils().t.variableDeclarator(namespaceIdentifier, init)]));
} else {
variableDeclarations.unshift(_utils().t.variableDeclaration('const', [_utils().t.variableDeclarator(id, init)]));
}
path.node.body.splice(index, 1);
}
} // export * from 'foo';
if (_utils().t.isExportAllDeclaration(d) && d.source) {
var _opts$onTransformDeps2, _opts$exportAllMember, _opts$exportAllMember2, _opts$exportAllMember3;
const isMatch = isMatchLib(d.source.value, opts.libs, opts.matchAll, opts.remoteName, opts.alias || {}, opts.webpackAlias || {}, opts.webpackExternals || {});
(_opts$onTransformDeps2 = opts.onTransformDeps) === null || _opts$onTransformDeps2 === void 0 ? void 0 : _opts$onTransformDeps2.call(opts, {
source: d.source.value,
// @ts-ignore
file: path.hub.file.opts.filename,
isMatch: isMatch && ((_opts$exportAllMember = opts.exportAllMembers) === null || _opts$exportAllMember === void 0 ? void 0 : (_opts$exportAllMember2 = _opts$exportAllMember[d.source.value]) === null || _opts$exportAllMember2 === void 0 ? void 0 : _opts$exportAllMember2.length),
isExportAllDeclaration: true
});
const exportAllIdentifier = _utils().t.identifier(`__all_exports_${d.source.value.replace(/(@|\/|-)/g, '_')}`);
if (isMatch && ((_opts$exportAllMember3 = opts.exportAllMembers) === null || _opts$exportAllMember3 === void 0 ? void 0 : _opts$exportAllMember3[d.source.value])) {
if (opts.exportAllMembers[d.source.value].length) {
const id = exportAllIdentifier;
const init = _utils().t.awaitExpression(_utils().t.callExpression(_utils().t.import(), [_utils().t.stringLiteral(`${opts.remoteName}/${getPath(d.source.value, opts.alias || {})}`)]));
variableDeclarations.unshift(_utils().t.variableDeclaration('const', [_utils().t.variableDeclarator(id, init)])); // replace node with export const { a, b, c } = __all_exports
// a, b, c was declared from opts.exportAllMembers
path.node.body[index] = _utils().t.exportNamedDeclaration(_utils().t.variableDeclaration('const', [_utils().t.variableDeclarator(_utils().t.objectPattern(opts.exportAllMembers[d.source.value].map(m => _utils().t.objectProperty(_utils().t.identifier(m), _utils().t.identifier(m)))), exportAllIdentifier)]));
} // 有些 export * 只是为了类型
else {
path.node.body[index] = _utils().t.expressionStatement(_utils().t.numericLiteral(1));
}
}
} // export { bar } from 'foo';
if (_utils().t.isExportNamedDeclaration(d) && d.source) {
var _opts$onTransformDeps3;
const isMatch = isMatchLib(d.source.value, opts.libs, opts.matchAll, opts.remoteName, opts.alias || {}, opts.webpackAlias || {}, opts.webpackExternals || {});
(_opts$onTransformDeps3 = opts.onTransformDeps) === null || _opts$onTransformDeps3 === void 0 ? void 0 : _opts$onTransformDeps3.call(opts, {
source: d.source.value,
// @ts-ignore
file: path.hub.file.opts.filename,
isMatch
});
if (isMatch) {
const _specifiersToProperti2 = specifiersToProperties(d.specifiers),
properties = _specifiersToProperti2.properties;
const id = _utils().t.objectPattern(properties);
const init = _utils().t.awaitExpression(_utils().t.callExpression(_utils().t.import(), [_utils().t.stringLiteral(`${opts.remoteName}/${getPath(d.source.value, opts.alias || {})}`)]));
variableDeclarations.unshift(_utils().t.variableDeclaration('const', [_utils().t.variableDeclarator(id, init)]));
d.source = null;
d.specifiers.forEach(node => {
if (_utils().t.isExportSpecifier(node)) {
// @ts-ignore
node.local = node.exported;
}
});
}
}
index -= 1;
}
path.node.body = [...variableDeclarations, ...path.node.body];
}
},
CallExpression(path, {
opts
}) {
const node = path.node;
if (_utils().t.isImport(node.callee) && node.arguments.length === 1 && node.arguments[0].type === 'StringLiteral') {
var _opts$onTransformDeps4;
const value = node.arguments[0].value;
const isMatch = isMatchLib(value, opts.libs, opts.matchAll, opts.remoteName, opts.alias || {}, opts.webpackAlias || {}, opts.webpackExternals || {});
(_opts$onTransformDeps4 = opts.onTransformDeps) === null || _opts$onTransformDeps4 === void 0 ? void 0 : _opts$onTransformDeps4.call(opts, {
source: value,
// @ts-ignore
file: path.hub.file.opts.filename,
isMatch
});
if (isMatch) {
node.arguments[0] = _utils().t.stringLiteral(`${opts.remoteName}/${getPath(value, opts.alias || {})}`);
}
}
}
}
};
}