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