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