diff options
Diffstat (limited to 'node_modules/@babel/helper-define-polyfill-provider/esm/index.node.mjs')
| -rw-r--r-- | node_modules/@babel/helper-define-polyfill-provider/esm/index.node.mjs | 199 |
1 files changed, 131 insertions, 68 deletions
diff --git a/node_modules/@babel/helper-define-polyfill-provider/esm/index.node.mjs b/node_modules/@babel/helper-define-polyfill-provider/esm/index.node.mjs index b6a4adfc..f049a734 100644 --- a/node_modules/@babel/helper-define-polyfill-provider/esm/index.node.mjs +++ b/node_modules/@babel/helper-define-polyfill-provider/esm/index.node.mjs @@ -130,13 +130,13 @@ function createUtilsGetter(cache) { return path => { const prog = path.findParent(p => p.isProgram()); return { - injectGlobalImport(url) { - cache.storeAnonymous(prog, url, (isScript, source) => { + injectGlobalImport(url, moduleName) { + cache.storeAnonymous(prog, url, moduleName, (isScript, source) => { return isScript ? template.statement.ast`require(${source})` : t$1.importDeclaration([], source); }); }, - injectNamedImport(url, name, hint = name) { - return cache.storeNamed(prog, url, name, (isScript, source, name) => { + injectNamedImport(url, name, hint = name, moduleName) { + return cache.storeNamed(prog, url, name, moduleName, (isScript, source, name) => { const id = prog.scope.generateUidIdentifier(hint); return { node: isScript ? hoist(template.statement.ast` @@ -146,8 +146,8 @@ function createUtilsGetter(cache) { }; }); }, - injectDefaultImport(url, hint = url) { - return cache.storeNamed(prog, url, "default", (isScript, source) => { + injectDefaultImport(url, hint = url, moduleName) { + return cache.storeNamed(prog, url, "default", moduleName, (isScript, source) => { const id = prog.scope.generateUidIdentifier(hint); return { node: isScript ? hoist(template.statement.ast`var ${id} = require(${source})`) : t$1.importDeclaration([t$1.importDefaultSpecifier(id)], source), @@ -162,24 +162,23 @@ function createUtilsGetter(cache) { const { types: t } = _babel.default || _babel; -class ImportsCache { - constructor(resolver) { +class ImportsCachedInjector { + constructor(resolver, getPreferredIndex) { this._imports = new WeakMap(); this._anonymousImports = new WeakMap(); this._lastImports = new WeakMap(); this._resolver = resolver; + this._getPreferredIndex = getPreferredIndex; } - storeAnonymous(programPath, url, - // eslint-disable-next-line no-undef - getVal) { + storeAnonymous(programPath, url, moduleName, getVal) { const key = this._normalizeKey(programPath, url); const imports = this._ensure(this._anonymousImports, programPath, Set); if (imports.has(key)) return; const node = getVal(programPath.node.sourceType === "script", t.stringLiteral(this._resolver(url))); imports.add(key); - this._injectImport(programPath, node); + this._injectImport(programPath, node, moduleName); } - storeNamed(programPath, url, name, getVal) { + storeNamed(programPath, url, name, moduleName, getVal) { const key = this._normalizeKey(programPath, url, name); const imports = this._ensure(this._imports, programPath, Map); if (!imports.has(key)) { @@ -188,46 +187,58 @@ class ImportsCache { name: id } = getVal(programPath.node.sourceType === "script", t.stringLiteral(this._resolver(url)), t.identifier(name)); imports.set(key, id); - this._injectImport(programPath, node); + this._injectImport(programPath, node, moduleName); } return t.identifier(imports.get(key)); } - _injectImport(programPath, node) { - const lastImport = this._lastImports.get(programPath); - let newNodes; - if (lastImport && lastImport.node && + _injectImport(programPath, node, moduleName) { + var _this$_lastImports$ge; + const newIndex = this._getPreferredIndex(moduleName); + const lastImports = (_this$_lastImports$ge = this._lastImports.get(programPath)) != null ? _this$_lastImports$ge : []; + const isPathStillValid = path => path.node && // Sometimes the AST is modified and the "last import" // we have has been replaced - lastImport.parent === programPath.node && lastImport.container === programPath.node.body) { - newNodes = lastImport.insertAfter(node); - } else { - newNodes = programPath.unshiftContainer("body", node); - } - const newNode = newNodes[newNodes.length - 1]; - this._lastImports.set(programPath, newNode); - - /* - let lastImport; - programPath.get("body").forEach(path => { - if (path.isImportDeclaration()) lastImport = path; - if ( - path.isExpressionStatement() && - isRequireCall(path.get("expression")) - ) { - lastImport = path; + path.parent === programPath.node && path.container === programPath.node.body; + let last; + if (newIndex === Infinity) { + // Fast path: we can always just insert at the end if newIndex is `Infinity` + if (lastImports.length > 0) { + last = lastImports[lastImports.length - 1].path; + if (!isPathStillValid(last)) last = undefined; } - if ( - path.isVariableDeclaration() && - path.get("declarations").length === 1 && - (isRequireCall(path.get("declarations.0.init")) || - (path.get("declarations.0.init").isMemberExpression() && - isRequireCall(path.get("declarations.0.init.object")))) - ) { - lastImport = path; + } else { + for (const [i, data] of lastImports.entries()) { + const { + path, + index + } = data; + if (isPathStillValid(path)) { + if (newIndex < index) { + const [newPath] = path.insertBefore(node); + lastImports.splice(i, 0, { + path: newPath, + index: newIndex + }); + return; + } + last = path; + } } - });*/ + } + if (last) { + const [newPath] = last.insertAfter(node); + lastImports.push({ + path: newPath, + index: newIndex + }); + } else { + const [newPath] = programPath.unshiftContainer("body", node); + this._lastImports.set(programPath, [{ + path: newPath, + index: newIndex + }]); + } } - _ensure(map, programPath, Collection) { let collection = map.get(programPath); if (!collection) { @@ -275,7 +286,7 @@ function validateIncludeExclude(provider, polyfills, includePatterns, excludePat const regexp = patternToRegExp(pattern); if (!regexp) return false; let matched = false; - for (const polyfill of polyfills) { + for (const polyfill of polyfills.keys()) { if (regexp.test(polyfill)) { matched = true; current.add(polyfill); @@ -318,6 +329,17 @@ function applyMissingDependenciesDefaults(options, babelApi) { }; } +function isRemoved(path) { + if (path.removed) return true; + if (!path.parentPath) return false; + if (path.listKey) { + var _path$parentPath$node; + 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; + } else { + if (path.parentPath.node[path.key] !== path.node) return true; + } + return isRemoved(path.parentPath); +} var usage = (callProvider => { function property(object, key, placement, path) { return callProvider({ @@ -327,31 +349,58 @@ var usage = (callProvider => { placement }, path); } + function handleReferencedIdentifier(path) { + const { + node: { + name + }, + scope + } = path; + if (scope.getBindingIdentifier(name)) return; + callProvider({ + kind: "global", + name + }, path); + } + function analyzeMemberExpression(path) { + const key = resolveKey(path.get("property"), path.node.computed); + return { + key, + handleAsMemberExpression: !!key && key !== "prototype" + }; + } return { // Symbol(), new Promise ReferencedIdentifier(path) { const { - node: { - name - }, - scope + parentPath } = path; - if (scope.getBindingIdentifier(name)) return; - callProvider({ - kind: "global", - name - }, path); + if (parentPath.isMemberExpression({ + object: path.node + }) && analyzeMemberExpression(parentPath).handleAsMemberExpression) { + return; + } + handleReferencedIdentifier(path); }, MemberExpression(path) { - const key = resolveKey(path.get("property"), path.node.computed); - if (!key || key === "prototype") return; + const { + key, + handleAsMemberExpression + } = analyzeMemberExpression(path); + if (!handleAsMemberExpression) return; const object = path.get("object"); - if (object.isIdentifier()) { + let objectIsGlobalIdentifier = object.isIdentifier(); + if (objectIsGlobalIdentifier) { const binding = object.scope.getBinding(object.node.name); - if (binding && binding.path.isImportNamespaceSpecifier()) return; + if (binding) { + if (binding.path.isImportNamespaceSpecifier()) return; + objectIsGlobalIdentifier = false; + } } const source = resolveSource(object); - return property(source.id, key, source.placement, path); + let skipObject = property(source.id, key, source.placement, path); + skipObject || (skipObject = !objectIsGlobalIdentifier || path.shouldSkip || object.shouldSkip || isRemoved(object)); + if (!skipObject) handleReferencedIdentifier(object); }, ObjectPattern(path) { const { @@ -603,13 +652,16 @@ function instantiateProvider(factory, options, missingDependencies, dirname, deb providerOptions, absoluteImports } = resolveOptions(options, babelApi); - const getUtils = createUtilsGetter(new ImportsCache(moduleName => resolve(dirname, moduleName, absoluteImports))); // eslint-disable-next-line prefer-const let include, exclude; let polyfillsSupport; let polyfillsNames; let filterPolyfills; + const getUtils = createUtilsGetter(new ImportsCachedInjector(moduleName => resolve(dirname, moduleName, absoluteImports), name => { + var _polyfillsNames$get, _polyfillsNames; + return (_polyfillsNames$get = (_polyfillsNames = polyfillsNames) == null ? void 0 : _polyfillsNames.get(name)) != null ? _polyfillsNames$get : Infinity; + })); const depsCache = new Map(); const api = { babel: babelApi, @@ -667,29 +719,40 @@ function instantiateProvider(factory, options, missingDependencies, dirname, deb throw new Error(`The "${providerName}" provider doesn't support the "${method}" polyfilling method.`); } if (Array.isArray(provider.polyfills)) { - polyfillsNames = new Set(provider.polyfills); + polyfillsNames = new Map(provider.polyfills.map((name, index) => [name, index])); filterPolyfills = provider.filterPolyfills; } else if (provider.polyfills) { - polyfillsNames = new Set(Object.keys(provider.polyfills)); + polyfillsNames = new Map(Object.keys(provider.polyfills).map((name, index) => [name, index])); polyfillsSupport = provider.polyfills; filterPolyfills = provider.filterPolyfills; } else { - polyfillsNames = new Set(); + polyfillsNames = new Map(); } ({ include, exclude } = validateIncludeExclude(providerName, polyfillsNames, providerOptions.include || [], providerOptions.exclude || [])); + let callProvider; + if (methodName === "usageGlobal") { + callProvider = (payload, path) => { + var _ref; + const utils = getUtils(path); + return (_ref = provider[methodName](payload, utils, path)) != null ? _ref : false; + }; + } else { + callProvider = (payload, path) => { + const utils = getUtils(path); + provider[methodName](payload, utils, path); + return false; + }; + } return { debug, method, targets, provider, providerName, - callProvider(payload, path) { - const utils = getUtils(path); - provider[methodName](payload, utils, path); - } + callProvider }; } function definePolyfillProvider(factory) { |
