1 | import { declare } from '@babel/helper-plugin-utils';
|
---|
2 | import _getTargets, { prettifyTargets, getInclusionReasons, isRequired } from '@babel/helper-compilation-targets';
|
---|
3 | import * as babel from '@babel/core';
|
---|
4 | import path from 'path';
|
---|
5 | import debounce from 'lodash.debounce';
|
---|
6 | import requireResolve from 'resolve';
|
---|
7 |
|
---|
8 | const {
|
---|
9 | types: t$1,
|
---|
10 | template
|
---|
11 | } = babel.default || babel;
|
---|
12 | function intersection(a, b) {
|
---|
13 | const result = new Set();
|
---|
14 | a.forEach(v => b.has(v) && result.add(v));
|
---|
15 | return result;
|
---|
16 | }
|
---|
17 | function has$1(object, key) {
|
---|
18 | return Object.prototype.hasOwnProperty.call(object, key);
|
---|
19 | }
|
---|
20 |
|
---|
21 | function getType(target) {
|
---|
22 | return Object.prototype.toString.call(target).slice(8, -1);
|
---|
23 | }
|
---|
24 |
|
---|
25 | function resolveId(path) {
|
---|
26 | if (path.isIdentifier() && !path.scope.hasBinding(path.node.name,
|
---|
27 | /* noGlobals */
|
---|
28 | true)) {
|
---|
29 | return path.node.name;
|
---|
30 | }
|
---|
31 |
|
---|
32 | const {
|
---|
33 | deopt
|
---|
34 | } = path.evaluate();
|
---|
35 |
|
---|
36 | if (deopt && deopt.isIdentifier()) {
|
---|
37 | return deopt.node.name;
|
---|
38 | }
|
---|
39 | }
|
---|
40 |
|
---|
41 | function resolveKey(path, computed = false) {
|
---|
42 | const {
|
---|
43 | node,
|
---|
44 | parent,
|
---|
45 | scope
|
---|
46 | } = path;
|
---|
47 | if (path.isStringLiteral()) return node.value;
|
---|
48 | const {
|
---|
49 | name
|
---|
50 | } = node;
|
---|
51 | const isIdentifier = path.isIdentifier();
|
---|
52 | if (isIdentifier && !(computed || parent.computed)) return name;
|
---|
53 |
|
---|
54 | if (computed && path.isMemberExpression() && path.get("object").isIdentifier({
|
---|
55 | name: "Symbol"
|
---|
56 | }) && !scope.hasBinding("Symbol",
|
---|
57 | /* noGlobals */
|
---|
58 | true)) {
|
---|
59 | const sym = resolveKey(path.get("property"), path.node.computed);
|
---|
60 | if (sym) return "Symbol." + sym;
|
---|
61 | }
|
---|
62 |
|
---|
63 | if (!isIdentifier || scope.hasBinding(name,
|
---|
64 | /* noGlobals */
|
---|
65 | true)) {
|
---|
66 | const {
|
---|
67 | value
|
---|
68 | } = path.evaluate();
|
---|
69 | if (typeof value === "string") return value;
|
---|
70 | }
|
---|
71 | }
|
---|
72 | function resolveSource(obj) {
|
---|
73 | if (obj.isMemberExpression() && obj.get("property").isIdentifier({
|
---|
74 | name: "prototype"
|
---|
75 | })) {
|
---|
76 | const id = resolveId(obj.get("object"));
|
---|
77 |
|
---|
78 | if (id) {
|
---|
79 | return {
|
---|
80 | id,
|
---|
81 | placement: "prototype"
|
---|
82 | };
|
---|
83 | }
|
---|
84 |
|
---|
85 | return {
|
---|
86 | id: null,
|
---|
87 | placement: null
|
---|
88 | };
|
---|
89 | }
|
---|
90 |
|
---|
91 | const id = resolveId(obj);
|
---|
92 |
|
---|
93 | if (id) {
|
---|
94 | return {
|
---|
95 | id,
|
---|
96 | placement: "static"
|
---|
97 | };
|
---|
98 | }
|
---|
99 |
|
---|
100 | const {
|
---|
101 | value
|
---|
102 | } = obj.evaluate();
|
---|
103 |
|
---|
104 | if (value !== undefined) {
|
---|
105 | return {
|
---|
106 | id: getType(value),
|
---|
107 | placement: "prototype"
|
---|
108 | };
|
---|
109 | } else if (obj.isRegExpLiteral()) {
|
---|
110 | return {
|
---|
111 | id: "RegExp",
|
---|
112 | placement: "prototype"
|
---|
113 | };
|
---|
114 | } else if (obj.isFunction()) {
|
---|
115 | return {
|
---|
116 | id: "Function",
|
---|
117 | placement: "prototype"
|
---|
118 | };
|
---|
119 | }
|
---|
120 |
|
---|
121 | return {
|
---|
122 | id: null,
|
---|
123 | placement: null
|
---|
124 | };
|
---|
125 | }
|
---|
126 | function getImportSource({
|
---|
127 | node
|
---|
128 | }) {
|
---|
129 | if (node.specifiers.length === 0) return node.source.value;
|
---|
130 | }
|
---|
131 | function getRequireSource({
|
---|
132 | node
|
---|
133 | }) {
|
---|
134 | if (!t$1.isExpressionStatement(node)) return;
|
---|
135 | const {
|
---|
136 | expression
|
---|
137 | } = node;
|
---|
138 | const isRequire = t$1.isCallExpression(expression) && t$1.isIdentifier(expression.callee) && expression.callee.name === "require" && expression.arguments.length === 1 && t$1.isStringLiteral(expression.arguments[0]);
|
---|
139 | if (isRequire) return expression.arguments[0].value;
|
---|
140 | }
|
---|
141 |
|
---|
142 | function hoist(node) {
|
---|
143 | node._blockHoist = 3;
|
---|
144 | return node;
|
---|
145 | }
|
---|
146 |
|
---|
147 | function createUtilsGetter(cache) {
|
---|
148 | return path => {
|
---|
149 | const prog = path.findParent(p => p.isProgram());
|
---|
150 | return {
|
---|
151 | injectGlobalImport(url) {
|
---|
152 | cache.storeAnonymous(prog, url, (isScript, source) => {
|
---|
153 | return isScript ? template.statement.ast`require(${source})` : t$1.importDeclaration([], source);
|
---|
154 | });
|
---|
155 | },
|
---|
156 |
|
---|
157 | injectNamedImport(url, name, hint = name) {
|
---|
158 | return cache.storeNamed(prog, url, name, (isScript, source, name) => {
|
---|
159 | const id = prog.scope.generateUidIdentifier(hint);
|
---|
160 | return {
|
---|
161 | node: isScript ? hoist(template.statement.ast`
|
---|
162 | var ${id} = require(${source}).${name}
|
---|
163 | `) : t$1.importDeclaration([t$1.importSpecifier(id, name)], source),
|
---|
164 | name: id.name
|
---|
165 | };
|
---|
166 | });
|
---|
167 | },
|
---|
168 |
|
---|
169 | injectDefaultImport(url, hint = url) {
|
---|
170 | return cache.storeNamed(prog, url, "default", (isScript, source) => {
|
---|
171 | const id = prog.scope.generateUidIdentifier(hint);
|
---|
172 | return {
|
---|
173 | node: isScript ? hoist(template.statement.ast`var ${id} = require(${source})`) : t$1.importDeclaration([t$1.importDefaultSpecifier(id)], source),
|
---|
174 | name: id.name
|
---|
175 | };
|
---|
176 | });
|
---|
177 | }
|
---|
178 |
|
---|
179 | };
|
---|
180 | };
|
---|
181 | }
|
---|
182 |
|
---|
183 | const {
|
---|
184 | types: t
|
---|
185 | } = babel.default || babel;
|
---|
186 | class ImportsCache {
|
---|
187 | constructor(resolver) {
|
---|
188 | this._imports = new WeakMap();
|
---|
189 | this._anonymousImports = new WeakMap();
|
---|
190 | this._lastImports = new WeakMap();
|
---|
191 | this._resolver = resolver;
|
---|
192 | }
|
---|
193 |
|
---|
194 | storeAnonymous(programPath, url, // eslint-disable-next-line no-undef
|
---|
195 | getVal) {
|
---|
196 | const key = this._normalizeKey(programPath, url);
|
---|
197 |
|
---|
198 | const imports = this._ensure(this._anonymousImports, programPath, Set);
|
---|
199 |
|
---|
200 | if (imports.has(key)) return;
|
---|
201 | const node = getVal(programPath.node.sourceType === "script", t.stringLiteral(this._resolver(url)));
|
---|
202 | imports.add(key);
|
---|
203 |
|
---|
204 | this._injectImport(programPath, node);
|
---|
205 | }
|
---|
206 |
|
---|
207 | storeNamed(programPath, url, name, getVal) {
|
---|
208 | const key = this._normalizeKey(programPath, url, name);
|
---|
209 |
|
---|
210 | const imports = this._ensure(this._imports, programPath, Map);
|
---|
211 |
|
---|
212 | if (!imports.has(key)) {
|
---|
213 | const {
|
---|
214 | node,
|
---|
215 | name: id
|
---|
216 | } = getVal(programPath.node.sourceType === "script", t.stringLiteral(this._resolver(url)), t.identifier(name));
|
---|
217 | imports.set(key, id);
|
---|
218 |
|
---|
219 | this._injectImport(programPath, node);
|
---|
220 | }
|
---|
221 |
|
---|
222 | return t.identifier(imports.get(key));
|
---|
223 | }
|
---|
224 |
|
---|
225 | _injectImport(programPath, node) {
|
---|
226 | let lastImport = this._lastImports.get(programPath);
|
---|
227 |
|
---|
228 | if (lastImport && lastImport.node && // Sometimes the AST is modified and the "last import"
|
---|
229 | // we have has been replaced
|
---|
230 | lastImport.parent === programPath.node && lastImport.container === programPath.node.body) {
|
---|
231 | lastImport = lastImport.insertAfter(node);
|
---|
232 | } else {
|
---|
233 | lastImport = programPath.unshiftContainer("body", node);
|
---|
234 | }
|
---|
235 |
|
---|
236 | lastImport = lastImport[lastImport.length - 1];
|
---|
237 |
|
---|
238 | this._lastImports.set(programPath, lastImport);
|
---|
239 | /*
|
---|
240 | let lastImport;
|
---|
241 | programPath.get("body").forEach(path => {
|
---|
242 | if (path.isImportDeclaration()) lastImport = path;
|
---|
243 | if (
|
---|
244 | path.isExpressionStatement() &&
|
---|
245 | isRequireCall(path.get("expression"))
|
---|
246 | ) {
|
---|
247 | lastImport = path;
|
---|
248 | }
|
---|
249 | if (
|
---|
250 | path.isVariableDeclaration() &&
|
---|
251 | path.get("declarations").length === 1 &&
|
---|
252 | (isRequireCall(path.get("declarations.0.init")) ||
|
---|
253 | (path.get("declarations.0.init").isMemberExpression() &&
|
---|
254 | isRequireCall(path.get("declarations.0.init.object"))))
|
---|
255 | ) {
|
---|
256 | lastImport = path;
|
---|
257 | }
|
---|
258 | });*/
|
---|
259 |
|
---|
260 | }
|
---|
261 |
|
---|
262 | _ensure(map, programPath, Collection) {
|
---|
263 | let collection = map.get(programPath);
|
---|
264 |
|
---|
265 | if (!collection) {
|
---|
266 | collection = new Collection();
|
---|
267 | map.set(programPath, collection);
|
---|
268 | }
|
---|
269 |
|
---|
270 | return collection;
|
---|
271 | }
|
---|
272 |
|
---|
273 | _normalizeKey(programPath, url, name = "") {
|
---|
274 | const {
|
---|
275 | sourceType
|
---|
276 | } = programPath.node; // If we rely on the imported binding (the "name" parameter), we also need to cache
|
---|
277 | // based on the sourceType. This is because the module transforms change the names
|
---|
278 | // of the import variables.
|
---|
279 |
|
---|
280 | return `${name && sourceType}::${url}::${name}`;
|
---|
281 | }
|
---|
282 |
|
---|
283 | }
|
---|
284 |
|
---|
285 | const presetEnvSilentDebugHeader = "#__secret_key__@babel/preset-env__don't_log_debug_header_and_resolved_targets";
|
---|
286 | function stringifyTargetsMultiline(targets) {
|
---|
287 | return JSON.stringify(prettifyTargets(targets), null, 2);
|
---|
288 | }
|
---|
289 |
|
---|
290 | function patternToRegExp(pattern) {
|
---|
291 | if (pattern instanceof RegExp) return pattern;
|
---|
292 |
|
---|
293 | try {
|
---|
294 | return new RegExp(`^${pattern}$`);
|
---|
295 | } catch {
|
---|
296 | return null;
|
---|
297 | }
|
---|
298 | }
|
---|
299 |
|
---|
300 | function buildUnusedError(label, unused) {
|
---|
301 | if (!unused.length) return "";
|
---|
302 | return ` - The following "${label}" patterns didn't match any polyfill:\n` + unused.map(original => ` ${String(original)}\n`).join("");
|
---|
303 | }
|
---|
304 |
|
---|
305 | function buldDuplicatesError(duplicates) {
|
---|
306 | if (!duplicates.size) return "";
|
---|
307 | return ` - The following polyfills were matched both by "include" and "exclude" patterns:\n` + Array.from(duplicates, name => ` ${name}\n`).join("");
|
---|
308 | }
|
---|
309 |
|
---|
310 | function validateIncludeExclude(provider, polyfills, includePatterns, excludePatterns) {
|
---|
311 | let current;
|
---|
312 |
|
---|
313 | const filter = pattern => {
|
---|
314 | const regexp = patternToRegExp(pattern);
|
---|
315 | if (!regexp) return false;
|
---|
316 | let matched = false;
|
---|
317 |
|
---|
318 | for (const polyfill of polyfills) {
|
---|
319 | if (regexp.test(polyfill)) {
|
---|
320 | matched = true;
|
---|
321 | current.add(polyfill);
|
---|
322 | }
|
---|
323 | }
|
---|
324 |
|
---|
325 | return !matched;
|
---|
326 | }; // prettier-ignore
|
---|
327 |
|
---|
328 |
|
---|
329 | const include = current = new Set();
|
---|
330 | const unusedInclude = Array.from(includePatterns).filter(filter); // prettier-ignore
|
---|
331 |
|
---|
332 | const exclude = current = new Set();
|
---|
333 | const unusedExclude = Array.from(excludePatterns).filter(filter);
|
---|
334 | const duplicates = intersection(include, exclude);
|
---|
335 |
|
---|
336 | if (duplicates.size > 0 || unusedInclude.length > 0 || unusedExclude.length > 0) {
|
---|
337 | throw new Error(`Error while validating the "${provider}" provider options:\n` + buildUnusedError("include", unusedInclude) + buildUnusedError("exclude", unusedExclude) + buldDuplicatesError(duplicates));
|
---|
338 | }
|
---|
339 |
|
---|
340 | return {
|
---|
341 | include,
|
---|
342 | exclude
|
---|
343 | };
|
---|
344 | }
|
---|
345 | function applyMissingDependenciesDefaults(options, babelApi) {
|
---|
346 | const {
|
---|
347 | missingDependencies = {}
|
---|
348 | } = options;
|
---|
349 | if (missingDependencies === false) return false;
|
---|
350 | const caller = babelApi.caller(caller => caller == null ? void 0 : caller.name);
|
---|
351 | const {
|
---|
352 | log = "deferred",
|
---|
353 | inject = caller === "rollup-plugin-babel" ? "throw" : "import",
|
---|
354 | all = false
|
---|
355 | } = missingDependencies;
|
---|
356 | return {
|
---|
357 | log,
|
---|
358 | inject,
|
---|
359 | all
|
---|
360 | };
|
---|
361 | }
|
---|
362 |
|
---|
363 | var usage = (callProvider => {
|
---|
364 | function property(object, key, placement, path) {
|
---|
365 | return callProvider({
|
---|
366 | kind: "property",
|
---|
367 | object,
|
---|
368 | key,
|
---|
369 | placement
|
---|
370 | }, path);
|
---|
371 | }
|
---|
372 |
|
---|
373 | return {
|
---|
374 | // Symbol(), new Promise
|
---|
375 | ReferencedIdentifier(path) {
|
---|
376 | const {
|
---|
377 | node: {
|
---|
378 | name
|
---|
379 | },
|
---|
380 | scope
|
---|
381 | } = path;
|
---|
382 | if (scope.getBindingIdentifier(name)) return;
|
---|
383 | callProvider({
|
---|
384 | kind: "global",
|
---|
385 | name
|
---|
386 | }, path);
|
---|
387 | },
|
---|
388 |
|
---|
389 | MemberExpression(path) {
|
---|
390 | const key = resolveKey(path.get("property"), path.node.computed);
|
---|
391 | if (!key || key === "prototype") return;
|
---|
392 | const object = path.get("object");
|
---|
393 | const binding = object.scope.getBinding(object.node.name);
|
---|
394 | if (binding && binding.path.isImportNamespaceSpecifier()) return;
|
---|
395 | const source = resolveSource(object);
|
---|
396 | return property(source.id, key, source.placement, path);
|
---|
397 | },
|
---|
398 |
|
---|
399 | ObjectPattern(path) {
|
---|
400 | const {
|
---|
401 | parentPath,
|
---|
402 | parent
|
---|
403 | } = path;
|
---|
404 | let obj; // const { keys, values } = Object
|
---|
405 |
|
---|
406 | if (parentPath.isVariableDeclarator()) {
|
---|
407 | obj = parentPath.get("init"); // ({ keys, values } = Object)
|
---|
408 | } else if (parentPath.isAssignmentExpression()) {
|
---|
409 | obj = parentPath.get("right"); // !function ({ keys, values }) {...} (Object)
|
---|
410 | // resolution does not work after properties transform :-(
|
---|
411 | } else if (parentPath.isFunction()) {
|
---|
412 | const grand = parentPath.parentPath;
|
---|
413 |
|
---|
414 | if (grand.isCallExpression() || grand.isNewExpression()) {
|
---|
415 | if (grand.node.callee === parent) {
|
---|
416 | obj = grand.get("arguments")[path.key];
|
---|
417 | }
|
---|
418 | }
|
---|
419 | }
|
---|
420 |
|
---|
421 | let id = null;
|
---|
422 | let placement = null;
|
---|
423 | if (obj) ({
|
---|
424 | id,
|
---|
425 | placement
|
---|
426 | } = resolveSource(obj));
|
---|
427 |
|
---|
428 | for (const prop of path.get("properties")) {
|
---|
429 | if (prop.isObjectProperty()) {
|
---|
430 | const key = resolveKey(prop.get("key"));
|
---|
431 | if (key) property(id, key, placement, prop);
|
---|
432 | }
|
---|
433 | }
|
---|
434 | },
|
---|
435 |
|
---|
436 | BinaryExpression(path) {
|
---|
437 | if (path.node.operator !== "in") return;
|
---|
438 | const source = resolveSource(path.get("right"));
|
---|
439 | const key = resolveKey(path.get("left"), true);
|
---|
440 | if (!key) return;
|
---|
441 | callProvider({
|
---|
442 | kind: "in",
|
---|
443 | object: source.id,
|
---|
444 | key,
|
---|
445 | placement: source.placement
|
---|
446 | }, path);
|
---|
447 | }
|
---|
448 |
|
---|
449 | };
|
---|
450 | });
|
---|
451 |
|
---|
452 | var entry = (callProvider => ({
|
---|
453 | ImportDeclaration(path) {
|
---|
454 | const source = getImportSource(path);
|
---|
455 | if (!source) return;
|
---|
456 | callProvider({
|
---|
457 | kind: "import",
|
---|
458 | source
|
---|
459 | }, path);
|
---|
460 | },
|
---|
461 |
|
---|
462 | Program(path) {
|
---|
463 | path.get("body").forEach(bodyPath => {
|
---|
464 | const source = getRequireSource(bodyPath);
|
---|
465 | if (!source) return;
|
---|
466 | callProvider({
|
---|
467 | kind: "import",
|
---|
468 | source
|
---|
469 | }, bodyPath);
|
---|
470 | });
|
---|
471 | }
|
---|
472 |
|
---|
473 | }));
|
---|
474 |
|
---|
475 | const nativeRequireResolve = parseFloat(process.versions.node) >= 8.9;
|
---|
476 | function resolve(dirname, moduleName, absoluteImports) {
|
---|
477 | if (absoluteImports === false) return moduleName;
|
---|
478 | let basedir = dirname;
|
---|
479 |
|
---|
480 | if (typeof absoluteImports === "string") {
|
---|
481 | basedir = path.resolve(basedir, absoluteImports);
|
---|
482 | }
|
---|
483 |
|
---|
484 | let modulePackage, moduleNestedPath;
|
---|
485 | let slash = moduleName.indexOf("/");
|
---|
486 |
|
---|
487 | if (moduleName[0] === "@") {
|
---|
488 | slash = moduleName.indexOf("/", slash + 1);
|
---|
489 | }
|
---|
490 |
|
---|
491 | if (slash === -1) {
|
---|
492 | modulePackage = moduleName;
|
---|
493 | moduleNestedPath = "";
|
---|
494 | } else {
|
---|
495 | modulePackage = moduleName.slice(0, slash);
|
---|
496 | moduleNestedPath = moduleName.slice(slash);
|
---|
497 | }
|
---|
498 |
|
---|
499 | try {
|
---|
500 | let pkg;
|
---|
501 |
|
---|
502 | if (nativeRequireResolve) {
|
---|
503 | // $FlowIgnore
|
---|
504 | pkg = require.resolve(`${modulePackage}/package.json`, {
|
---|
505 | paths: [basedir]
|
---|
506 | });
|
---|
507 | } else {
|
---|
508 | pkg = requireResolve.sync(`${modulePackage}/package.json`, {
|
---|
509 | basedir
|
---|
510 | });
|
---|
511 | }
|
---|
512 |
|
---|
513 | return path.dirname(pkg) + moduleNestedPath;
|
---|
514 | } catch (err) {
|
---|
515 | if (err.code !== "MODULE_NOT_FOUND") throw err; // $FlowIgnore
|
---|
516 |
|
---|
517 | throw Object.assign(new Error(`Failed to resolve "${moduleName}" relative to "${dirname}"`), {
|
---|
518 | code: "BABEL_POLYFILL_NOT_FOUND",
|
---|
519 | polyfill: moduleName,
|
---|
520 | dirname
|
---|
521 | });
|
---|
522 | }
|
---|
523 | }
|
---|
524 | function has(basedir, name) {
|
---|
525 | try {
|
---|
526 | if (nativeRequireResolve) {
|
---|
527 | // $FlowIgnore
|
---|
528 | require.resolve(name, {
|
---|
529 | paths: [basedir]
|
---|
530 | });
|
---|
531 | } else {
|
---|
532 | requireResolve.sync(name, {
|
---|
533 | basedir
|
---|
534 | });
|
---|
535 | }
|
---|
536 |
|
---|
537 | return true;
|
---|
538 | } catch {
|
---|
539 | return false;
|
---|
540 | }
|
---|
541 | }
|
---|
542 | function logMissing(missingDeps) {
|
---|
543 | if (missingDeps.size === 0) return;
|
---|
544 | const deps = Array.from(missingDeps).sort().join(" ");
|
---|
545 | console.warn("\nSome polyfills have been added but are not present in your dependencies.\n" + "Please run one of the following commands:\n" + `\tnpm install --save ${deps}\n` + `\tyarn add ${deps}\n`);
|
---|
546 | process.exitCode = 1;
|
---|
547 | }
|
---|
548 | let allMissingDeps = new Set();
|
---|
549 | const laterLogMissingDependencies = debounce(() => {
|
---|
550 | logMissing(allMissingDeps);
|
---|
551 | allMissingDeps = new Set();
|
---|
552 | }, 100);
|
---|
553 | function laterLogMissing(missingDeps) {
|
---|
554 | if (missingDeps.size === 0) return;
|
---|
555 | missingDeps.forEach(name => allMissingDeps.add(name));
|
---|
556 | laterLogMissingDependencies();
|
---|
557 | }
|
---|
558 |
|
---|
559 | const PossibleGlobalObjects = new Set(["global", "globalThis", "self", "window"]);
|
---|
560 | function createMetaResolver(polyfills) {
|
---|
561 | const {
|
---|
562 | static: staticP,
|
---|
563 | instance: instanceP,
|
---|
564 | global: globalP
|
---|
565 | } = polyfills;
|
---|
566 | return meta => {
|
---|
567 | if (meta.kind === "global" && globalP && has$1(globalP, meta.name)) {
|
---|
568 | return {
|
---|
569 | kind: "global",
|
---|
570 | desc: globalP[meta.name],
|
---|
571 | name: meta.name
|
---|
572 | };
|
---|
573 | }
|
---|
574 |
|
---|
575 | if (meta.kind === "property" || meta.kind === "in") {
|
---|
576 | const {
|
---|
577 | placement,
|
---|
578 | object,
|
---|
579 | key
|
---|
580 | } = meta;
|
---|
581 |
|
---|
582 | if (object && placement === "static") {
|
---|
583 | if (globalP && PossibleGlobalObjects.has(object) && has$1(globalP, key)) {
|
---|
584 | return {
|
---|
585 | kind: "global",
|
---|
586 | desc: globalP[key],
|
---|
587 | name: key
|
---|
588 | };
|
---|
589 | }
|
---|
590 |
|
---|
591 | if (staticP && has$1(staticP, object) && has$1(staticP[object], key)) {
|
---|
592 | return {
|
---|
593 | kind: "static",
|
---|
594 | desc: staticP[object][key],
|
---|
595 | name: `${object}$${key}`
|
---|
596 | };
|
---|
597 | }
|
---|
598 | }
|
---|
599 |
|
---|
600 | if (instanceP && has$1(instanceP, key)) {
|
---|
601 | return {
|
---|
602 | kind: "instance",
|
---|
603 | desc: instanceP[key],
|
---|
604 | name: `${key}`
|
---|
605 | };
|
---|
606 | }
|
---|
607 | }
|
---|
608 | };
|
---|
609 | }
|
---|
610 |
|
---|
611 | const getTargets = _getTargets.default || _getTargets;
|
---|
612 |
|
---|
613 | function resolveOptions(options, babelApi) {
|
---|
614 | const {
|
---|
615 | method,
|
---|
616 | targets: targetsOption,
|
---|
617 | ignoreBrowserslistConfig,
|
---|
618 | configPath,
|
---|
619 | debug,
|
---|
620 | shouldInjectPolyfill,
|
---|
621 | absoluteImports,
|
---|
622 | ...providerOptions
|
---|
623 | } = options;
|
---|
624 | let methodName;
|
---|
625 | if (method === "usage-global") methodName = "usageGlobal";else if (method === "entry-global") methodName = "entryGlobal";else if (method === "usage-pure") methodName = "usagePure";else if (typeof method !== "string") {
|
---|
626 | throw new Error(".method must be a string");
|
---|
627 | } else {
|
---|
628 | throw new Error(`.method must be one of "entry-global", "usage-global"` + ` or "usage-pure" (received ${JSON.stringify(method)})`);
|
---|
629 | }
|
---|
630 |
|
---|
631 | if (typeof shouldInjectPolyfill === "function") {
|
---|
632 | if (options.include || options.exclude) {
|
---|
633 | throw new Error(`.include and .exclude are not supported when using the` + ` .shouldInjectPolyfill function.`);
|
---|
634 | }
|
---|
635 | } else if (shouldInjectPolyfill != null) {
|
---|
636 | throw new Error(`.shouldInjectPolyfill must be a function, or undefined` + ` (received ${JSON.stringify(shouldInjectPolyfill)})`);
|
---|
637 | }
|
---|
638 |
|
---|
639 | if (absoluteImports != null && typeof absoluteImports !== "boolean" && typeof absoluteImports !== "string") {
|
---|
640 | throw new Error(`.absoluteImports must be a boolean, a string, or undefined` + ` (received ${JSON.stringify(absoluteImports)})`);
|
---|
641 | }
|
---|
642 |
|
---|
643 | let targets;
|
---|
644 |
|
---|
645 | if ( // If any browserslist-related option is specified, fallback to the old
|
---|
646 | // behavior of not using the targets specified in the top-level options.
|
---|
647 | targetsOption || configPath || ignoreBrowserslistConfig) {
|
---|
648 | const targetsObj = typeof targetsOption === "string" || Array.isArray(targetsOption) ? {
|
---|
649 | browsers: targetsOption
|
---|
650 | } : targetsOption;
|
---|
651 | targets = getTargets(targetsObj, {
|
---|
652 | ignoreBrowserslistConfig,
|
---|
653 | configPath
|
---|
654 | });
|
---|
655 | } else {
|
---|
656 | targets = babelApi.targets();
|
---|
657 | }
|
---|
658 |
|
---|
659 | return {
|
---|
660 | method,
|
---|
661 | methodName,
|
---|
662 | targets,
|
---|
663 | absoluteImports: absoluteImports != null ? absoluteImports : false,
|
---|
664 | shouldInjectPolyfill,
|
---|
665 | debug: !!debug,
|
---|
666 | providerOptions: providerOptions
|
---|
667 | };
|
---|
668 | }
|
---|
669 |
|
---|
670 | function instantiateProvider(factory, options, missingDependencies, dirname, debugLog, babelApi) {
|
---|
671 | const {
|
---|
672 | method,
|
---|
673 | methodName,
|
---|
674 | targets,
|
---|
675 | debug,
|
---|
676 | shouldInjectPolyfill,
|
---|
677 | providerOptions,
|
---|
678 | absoluteImports
|
---|
679 | } = resolveOptions(options, babelApi);
|
---|
680 | const getUtils = createUtilsGetter(new ImportsCache(moduleName => resolve(dirname, moduleName, absoluteImports))); // eslint-disable-next-line prefer-const
|
---|
681 |
|
---|
682 | let include, exclude;
|
---|
683 | let polyfillsSupport;
|
---|
684 | let polyfillsNames;
|
---|
685 | let filterPolyfills;
|
---|
686 | const depsCache = new Map();
|
---|
687 | const api = {
|
---|
688 | babel: babelApi,
|
---|
689 | getUtils,
|
---|
690 | method: options.method,
|
---|
691 | targets,
|
---|
692 | createMetaResolver,
|
---|
693 |
|
---|
694 | shouldInjectPolyfill(name) {
|
---|
695 | if (polyfillsNames === undefined) {
|
---|
696 | throw new Error(`Internal error in the ${factory.name} provider: ` + `shouldInjectPolyfill() can't be called during initialization.`);
|
---|
697 | }
|
---|
698 |
|
---|
699 | if (!polyfillsNames.has(name)) {
|
---|
700 | console.warn(`Internal error in the ${provider.name} provider: ` + `unknown polyfill "${name}".`);
|
---|
701 | }
|
---|
702 |
|
---|
703 | if (filterPolyfills && !filterPolyfills(name)) return false;
|
---|
704 | let shouldInject = isRequired(name, targets, {
|
---|
705 | compatData: polyfillsSupport,
|
---|
706 | includes: include,
|
---|
707 | excludes: exclude
|
---|
708 | });
|
---|
709 |
|
---|
710 | if (shouldInjectPolyfill) {
|
---|
711 | shouldInject = shouldInjectPolyfill(name, shouldInject);
|
---|
712 |
|
---|
713 | if (typeof shouldInject !== "boolean") {
|
---|
714 | throw new Error(`.shouldInjectPolyfill must return a boolean.`);
|
---|
715 | }
|
---|
716 | }
|
---|
717 |
|
---|
718 | return shouldInject;
|
---|
719 | },
|
---|
720 |
|
---|
721 | debug(name) {
|
---|
722 | debugLog().found = true;
|
---|
723 | if (!debug || !name) return;
|
---|
724 | if (debugLog().polyfills.has(provider.name)) return;
|
---|
725 | debugLog().polyfills.set(name, polyfillsSupport && name && polyfillsSupport[name]);
|
---|
726 | },
|
---|
727 |
|
---|
728 | assertDependency(name, version = "*") {
|
---|
729 | if (missingDependencies === false) return;
|
---|
730 |
|
---|
731 | if (absoluteImports) {
|
---|
732 | // If absoluteImports is not false, we will try resolving
|
---|
733 | // the dependency and throw if it's not possible. We can
|
---|
734 | // skip the check here.
|
---|
735 | return;
|
---|
736 | }
|
---|
737 |
|
---|
738 | const dep = version === "*" ? name : `${name}@^${version}`;
|
---|
739 | const found = missingDependencies.all ? false : mapGetOr(depsCache, `${name} :: ${dirname}`, () => has(dirname, name));
|
---|
740 |
|
---|
741 | if (!found) {
|
---|
742 | debugLog().missingDeps.add(dep);
|
---|
743 | }
|
---|
744 | }
|
---|
745 |
|
---|
746 | };
|
---|
747 | const provider = factory(api, providerOptions, dirname);
|
---|
748 |
|
---|
749 | if (typeof provider[methodName] !== "function") {
|
---|
750 | throw new Error(`The "${provider.name || factory.name}" provider doesn't ` + `support the "${method}" polyfilling method.`);
|
---|
751 | }
|
---|
752 |
|
---|
753 | if (Array.isArray(provider.polyfills)) {
|
---|
754 | polyfillsNames = new Set(provider.polyfills);
|
---|
755 | filterPolyfills = provider.filterPolyfills;
|
---|
756 | } else if (provider.polyfills) {
|
---|
757 | polyfillsNames = new Set(Object.keys(provider.polyfills));
|
---|
758 | polyfillsSupport = provider.polyfills;
|
---|
759 | filterPolyfills = provider.filterPolyfills;
|
---|
760 | } else {
|
---|
761 | polyfillsNames = new Set();
|
---|
762 | }
|
---|
763 |
|
---|
764 | ({
|
---|
765 | include,
|
---|
766 | exclude
|
---|
767 | } = validateIncludeExclude(provider.name || factory.name, polyfillsNames, providerOptions.include || [], providerOptions.exclude || []));
|
---|
768 | return {
|
---|
769 | debug,
|
---|
770 | method,
|
---|
771 | targets,
|
---|
772 | provider,
|
---|
773 |
|
---|
774 | callProvider(payload, path) {
|
---|
775 | const utils = getUtils(path); // $FlowIgnore
|
---|
776 |
|
---|
777 | provider[methodName](payload, utils, path);
|
---|
778 | }
|
---|
779 |
|
---|
780 | };
|
---|
781 | }
|
---|
782 |
|
---|
783 | function definePolyfillProvider(factory) {
|
---|
784 | return declare((babelApi, options, dirname) => {
|
---|
785 | babelApi.assertVersion(7);
|
---|
786 | const {
|
---|
787 | traverse
|
---|
788 | } = babelApi;
|
---|
789 | let debugLog;
|
---|
790 | const missingDependencies = applyMissingDependenciesDefaults(options, babelApi);
|
---|
791 | const {
|
---|
792 | debug,
|
---|
793 | method,
|
---|
794 | targets,
|
---|
795 | provider,
|
---|
796 | callProvider
|
---|
797 | } = instantiateProvider(factory, options, missingDependencies, dirname, () => debugLog, babelApi);
|
---|
798 | const createVisitor = method === "entry-global" ? entry : usage;
|
---|
799 | const visitor = provider.visitor ? traverse.visitors.merge([createVisitor(callProvider), provider.visitor]) : createVisitor(callProvider);
|
---|
800 |
|
---|
801 | if (debug && debug !== presetEnvSilentDebugHeader) {
|
---|
802 | console.log(`${provider.name}: \`DEBUG\` option`);
|
---|
803 | console.log(`\nUsing targets: ${stringifyTargetsMultiline(targets)}`);
|
---|
804 | console.log(`\nUsing polyfills with \`${method}\` method:`);
|
---|
805 | }
|
---|
806 |
|
---|
807 | return {
|
---|
808 | name: "inject-polyfills",
|
---|
809 | visitor,
|
---|
810 |
|
---|
811 | pre() {
|
---|
812 | var _provider$pre;
|
---|
813 |
|
---|
814 | debugLog = {
|
---|
815 | polyfills: new Map(),
|
---|
816 | found: false,
|
---|
817 | providers: new Set(),
|
---|
818 | missingDeps: new Set()
|
---|
819 | }; // $FlowIgnore - Flow doesn't support optional calls
|
---|
820 |
|
---|
821 | (_provider$pre = provider.pre) == null ? void 0 : _provider$pre.apply(this, arguments);
|
---|
822 | },
|
---|
823 |
|
---|
824 | post() {
|
---|
825 | var _provider$post;
|
---|
826 |
|
---|
827 | // $FlowIgnore - Flow doesn't support optional calls
|
---|
828 | (_provider$post = provider.post) == null ? void 0 : _provider$post.apply(this, arguments);
|
---|
829 |
|
---|
830 | if (missingDependencies !== false) {
|
---|
831 | if (missingDependencies.log === "per-file") {
|
---|
832 | logMissing(debugLog.missingDeps);
|
---|
833 | } else {
|
---|
834 | laterLogMissing(debugLog.missingDeps);
|
---|
835 | }
|
---|
836 | }
|
---|
837 |
|
---|
838 | if (!debug) return;
|
---|
839 | if (this.filename) console.log(`\n[${this.filename}]`);
|
---|
840 |
|
---|
841 | if (debugLog.polyfills.size === 0) {
|
---|
842 | console.log(method === "entry-global" ? debugLog.found ? `Based on your targets, the ${provider.name} polyfill did not add any polyfill.` : `The entry point for the ${provider.name} polyfill has not been found.` : `Based on your code and targets, the ${provider.name} polyfill did not add any polyfill.`);
|
---|
843 | return;
|
---|
844 | }
|
---|
845 |
|
---|
846 | if (method === "entry-global") {
|
---|
847 | console.log(`The ${provider.name} polyfill entry has been replaced with ` + `the following polyfills:`);
|
---|
848 | } else {
|
---|
849 | console.log(`The ${provider.name} polyfill added the following polyfills:`);
|
---|
850 | }
|
---|
851 |
|
---|
852 | for (const [name, support] of debugLog.polyfills) {
|
---|
853 | if (support) {
|
---|
854 | const filteredTargets = getInclusionReasons(name, targets, support);
|
---|
855 | const formattedTargets = JSON.stringify(filteredTargets).replace(/,/g, ", ").replace(/^\{"/, '{ "').replace(/"\}$/, '" }');
|
---|
856 | console.log(` ${name} ${formattedTargets}`);
|
---|
857 | } else {
|
---|
858 | console.log(` ${name}`);
|
---|
859 | }
|
---|
860 | }
|
---|
861 | }
|
---|
862 |
|
---|
863 | };
|
---|
864 | });
|
---|
865 | }
|
---|
866 |
|
---|
867 | function mapGetOr(map, key, getDefault) {
|
---|
868 | let val = map.get(key);
|
---|
869 |
|
---|
870 | if (val === undefined) {
|
---|
871 | val = getDefault();
|
---|
872 | map.set(key, val);
|
---|
873 | }
|
---|
874 |
|
---|
875 | return val;
|
---|
876 | }
|
---|
877 |
|
---|
878 | export default definePolyfillProvider;
|
---|
879 | //# sourceMappingURL=index.node.mjs.map
|
---|