| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | exports.__esModule = true;
|
|---|
| 4 | exports.default = definePolyfillProvider;
|
|---|
| 5 | var _helperPluginUtils = require("@babel/helper-plugin-utils");
|
|---|
| 6 | var _helperCompilationTargets = _interopRequireWildcard(require("@babel/helper-compilation-targets"));
|
|---|
| 7 | var _utils = require("./utils");
|
|---|
| 8 | var _importsInjector = _interopRequireDefault(require("./imports-injector"));
|
|---|
| 9 | var _debugUtils = require("./debug-utils");
|
|---|
| 10 | var _normalizeOptions = require("./normalize-options");
|
|---|
| 11 | var v = _interopRequireWildcard(require("./visitors"));
|
|---|
| 12 | var deps = _interopRequireWildcard(require("./node/dependencies"));
|
|---|
| 13 | var _metaResolver = _interopRequireDefault(require("./meta-resolver"));
|
|---|
| 14 | const _excluded = ["method", "targets", "ignoreBrowserslistConfig", "configPath", "debug", "shouldInjectPolyfill", "absoluteImports"];
|
|---|
| 15 | function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|---|
| 16 | function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
|
|---|
| 17 | function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
|
|---|
| 18 | const getTargets = _helperCompilationTargets.default.default || _helperCompilationTargets.default;
|
|---|
| 19 | function resolveOptions(options, babelApi) {
|
|---|
| 20 | const {
|
|---|
| 21 | method,
|
|---|
| 22 | targets: targetsOption,
|
|---|
| 23 | ignoreBrowserslistConfig,
|
|---|
| 24 | configPath,
|
|---|
| 25 | debug,
|
|---|
| 26 | shouldInjectPolyfill,
|
|---|
| 27 | absoluteImports
|
|---|
| 28 | } = options,
|
|---|
| 29 | providerOptions = _objectWithoutPropertiesLoose(options, _excluded);
|
|---|
| 30 | if (isEmpty(options)) {
|
|---|
| 31 | throw new Error(`\
|
|---|
| 32 | This plugin requires options, for example:
|
|---|
| 33 | {
|
|---|
| 34 | "plugins": [
|
|---|
| 35 | ["<plugin name>", { method: "usage-pure" }]
|
|---|
| 36 | ]
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | See more options at https://github.com/babel/babel-polyfills/blob/main/docs/usage.md`);
|
|---|
| 40 | }
|
|---|
| 41 | let methodName;
|
|---|
| 42 | 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") {
|
|---|
| 43 | throw new Error(".method must be a string");
|
|---|
| 44 | } else {
|
|---|
| 45 | throw new Error(`.method must be one of "entry-global", "usage-global"` + ` or "usage-pure" (received ${JSON.stringify(method)})`);
|
|---|
| 46 | }
|
|---|
| 47 | if (typeof shouldInjectPolyfill === "function") {
|
|---|
| 48 | if (options.include || options.exclude) {
|
|---|
| 49 | throw new Error(`.include and .exclude are not supported when using the` + ` .shouldInjectPolyfill function.`);
|
|---|
| 50 | }
|
|---|
| 51 | } else if (shouldInjectPolyfill != null) {
|
|---|
| 52 | throw new Error(`.shouldInjectPolyfill must be a function, or undefined` + ` (received ${JSON.stringify(shouldInjectPolyfill)})`);
|
|---|
| 53 | }
|
|---|
| 54 | if (absoluteImports != null && typeof absoluteImports !== "boolean" && typeof absoluteImports !== "string") {
|
|---|
| 55 | throw new Error(`.absoluteImports must be a boolean, a string, or undefined` + ` (received ${JSON.stringify(absoluteImports)})`);
|
|---|
| 56 | }
|
|---|
| 57 | let targets;
|
|---|
| 58 | if (
|
|---|
| 59 | // If any browserslist-related option is specified, fallback to the old
|
|---|
| 60 | // behavior of not using the targets specified in the top-level options.
|
|---|
| 61 | targetsOption || configPath || ignoreBrowserslistConfig) {
|
|---|
| 62 | const targetsObj = typeof targetsOption === "string" || Array.isArray(targetsOption) ? {
|
|---|
| 63 | browsers: targetsOption
|
|---|
| 64 | } : targetsOption;
|
|---|
| 65 | targets = getTargets(targetsObj, {
|
|---|
| 66 | ignoreBrowserslistConfig,
|
|---|
| 67 | configPath
|
|---|
| 68 | });
|
|---|
| 69 | } else {
|
|---|
| 70 | targets = babelApi.targets();
|
|---|
| 71 | }
|
|---|
| 72 | return {
|
|---|
| 73 | method,
|
|---|
| 74 | methodName,
|
|---|
| 75 | targets,
|
|---|
| 76 | absoluteImports: absoluteImports != null ? absoluteImports : false,
|
|---|
| 77 | shouldInjectPolyfill,
|
|---|
| 78 | debug: !!debug,
|
|---|
| 79 | providerOptions: providerOptions
|
|---|
| 80 | };
|
|---|
| 81 | }
|
|---|
| 82 | function instantiateProvider(factory, options, missingDependencies, dirname, debugLog, babelApi) {
|
|---|
| 83 | const {
|
|---|
| 84 | method,
|
|---|
| 85 | methodName,
|
|---|
| 86 | targets,
|
|---|
| 87 | debug,
|
|---|
| 88 | shouldInjectPolyfill,
|
|---|
| 89 | providerOptions,
|
|---|
| 90 | absoluteImports
|
|---|
| 91 | } = resolveOptions(options, babelApi);
|
|---|
| 92 |
|
|---|
| 93 | // eslint-disable-next-line prefer-const
|
|---|
| 94 | let include, exclude;
|
|---|
| 95 | let polyfillsSupport;
|
|---|
| 96 | let polyfillsNames;
|
|---|
| 97 | let filterPolyfills;
|
|---|
| 98 | const getUtils = (0, _utils.createUtilsGetter)(new _importsInjector.default(moduleName => deps.resolve(dirname, moduleName, absoluteImports), name => {
|
|---|
| 99 | var _polyfillsNames$get, _polyfillsNames;
|
|---|
| 100 | return (_polyfillsNames$get = (_polyfillsNames = polyfillsNames) == null ? void 0 : _polyfillsNames.get(name)) != null ? _polyfillsNames$get : Infinity;
|
|---|
| 101 | }));
|
|---|
| 102 | const depsCache = new Map();
|
|---|
| 103 | const api = {
|
|---|
| 104 | babel: babelApi,
|
|---|
| 105 | getUtils,
|
|---|
| 106 | method: options.method,
|
|---|
| 107 | targets,
|
|---|
| 108 | createMetaResolver: _metaResolver.default,
|
|---|
| 109 | shouldInjectPolyfill(name) {
|
|---|
| 110 | if (polyfillsNames === undefined) {
|
|---|
| 111 | throw new Error(`Internal error in the ${factory.name} provider: ` + `shouldInjectPolyfill() can't be called during initialization.`);
|
|---|
| 112 | }
|
|---|
| 113 | if (!polyfillsNames.has(name)) {
|
|---|
| 114 | console.warn(`Internal error in the ${providerName} provider: ` + `unknown polyfill "${name}".`);
|
|---|
| 115 | }
|
|---|
| 116 | if (filterPolyfills && !filterPolyfills(name)) return false;
|
|---|
| 117 | let shouldInject = (0, _helperCompilationTargets.isRequired)(name, targets, {
|
|---|
| 118 | compatData: polyfillsSupport,
|
|---|
| 119 | includes: include,
|
|---|
| 120 | excludes: exclude
|
|---|
| 121 | });
|
|---|
| 122 | if (shouldInjectPolyfill) {
|
|---|
| 123 | shouldInject = shouldInjectPolyfill(name, shouldInject);
|
|---|
| 124 | if (typeof shouldInject !== "boolean") {
|
|---|
| 125 | throw new Error(`.shouldInjectPolyfill must return a boolean.`);
|
|---|
| 126 | }
|
|---|
| 127 | }
|
|---|
| 128 | return shouldInject;
|
|---|
| 129 | },
|
|---|
| 130 | debug(name) {
|
|---|
| 131 | var _debugLog, _debugLog$polyfillsSu;
|
|---|
| 132 | debugLog().found = true;
|
|---|
| 133 | if (!debug || !name) return;
|
|---|
| 134 | if (debugLog().polyfills.has(providerName)) return;
|
|---|
| 135 | debugLog().polyfills.add(name);
|
|---|
| 136 | (_debugLog$polyfillsSu = (_debugLog = debugLog()).polyfillsSupport) != null ? _debugLog$polyfillsSu : _debugLog.polyfillsSupport = polyfillsSupport;
|
|---|
| 137 | },
|
|---|
| 138 | assertDependency(name, version = "*") {
|
|---|
| 139 | if (missingDependencies === false) return;
|
|---|
| 140 | if (absoluteImports) {
|
|---|
| 141 | // If absoluteImports is not false, we will try resolving
|
|---|
| 142 | // the dependency and throw if it's not possible. We can
|
|---|
| 143 | // skip the check here.
|
|---|
| 144 | return;
|
|---|
| 145 | }
|
|---|
| 146 | const dep = version === "*" ? name : `${name}@^${version}`;
|
|---|
| 147 | const found = missingDependencies.all ? false : mapGetOr(depsCache, `${name} :: ${dirname}`, () => deps.has(dirname, name));
|
|---|
| 148 | if (!found) {
|
|---|
| 149 | debugLog().missingDeps.add(dep);
|
|---|
| 150 | }
|
|---|
| 151 | }
|
|---|
| 152 | };
|
|---|
| 153 | const provider = factory(api, providerOptions, dirname);
|
|---|
| 154 | const providerName = provider.name || factory.name;
|
|---|
| 155 | if (typeof provider[methodName] !== "function") {
|
|---|
| 156 | throw new Error(`The "${providerName}" provider doesn't support the "${method}" polyfilling method.`);
|
|---|
| 157 | }
|
|---|
| 158 | if (Array.isArray(provider.polyfills)) {
|
|---|
| 159 | polyfillsNames = new Map(provider.polyfills.map((name, index) => [name, index]));
|
|---|
| 160 | filterPolyfills = provider.filterPolyfills;
|
|---|
| 161 | } else if (provider.polyfills) {
|
|---|
| 162 | polyfillsNames = new Map(Object.keys(provider.polyfills).map((name, index) => [name, index]));
|
|---|
| 163 | polyfillsSupport = provider.polyfills;
|
|---|
| 164 | filterPolyfills = provider.filterPolyfills;
|
|---|
| 165 | } else {
|
|---|
| 166 | polyfillsNames = new Map();
|
|---|
| 167 | }
|
|---|
| 168 | ({
|
|---|
| 169 | include,
|
|---|
| 170 | exclude
|
|---|
| 171 | } = (0, _normalizeOptions.validateIncludeExclude)(providerName, polyfillsNames, providerOptions.include || [], providerOptions.exclude || []));
|
|---|
| 172 | let callProvider;
|
|---|
| 173 | if (methodName === "usageGlobal") {
|
|---|
| 174 | callProvider = (payload, path) => {
|
|---|
| 175 | var _ref;
|
|---|
| 176 | const utils = getUtils(path);
|
|---|
| 177 | return (_ref = provider[methodName](payload, utils, path)) != null ? _ref : false;
|
|---|
| 178 | };
|
|---|
| 179 | } else {
|
|---|
| 180 | callProvider = (payload, path) => {
|
|---|
| 181 | const utils = getUtils(path);
|
|---|
| 182 | provider[methodName](payload, utils, path);
|
|---|
| 183 | return false;
|
|---|
| 184 | };
|
|---|
| 185 | }
|
|---|
| 186 | return {
|
|---|
| 187 | debug,
|
|---|
| 188 | method,
|
|---|
| 189 | targets,
|
|---|
| 190 | provider,
|
|---|
| 191 | providerName,
|
|---|
| 192 | callProvider
|
|---|
| 193 | };
|
|---|
| 194 | }
|
|---|
| 195 | function definePolyfillProvider(factory) {
|
|---|
| 196 | return (0, _helperPluginUtils.declare)((babelApi, options, dirname) => {
|
|---|
| 197 | babelApi.assertVersion("^7.0.0 || ^8.0.0-alpha.0");
|
|---|
| 198 | const {
|
|---|
| 199 | traverse
|
|---|
| 200 | } = babelApi;
|
|---|
| 201 | let debugLog;
|
|---|
| 202 | const missingDependencies = (0, _normalizeOptions.applyMissingDependenciesDefaults)(options, babelApi);
|
|---|
| 203 | const {
|
|---|
| 204 | debug,
|
|---|
| 205 | method,
|
|---|
| 206 | targets,
|
|---|
| 207 | provider,
|
|---|
| 208 | providerName,
|
|---|
| 209 | callProvider
|
|---|
| 210 | } = instantiateProvider(factory, options, missingDependencies, dirname, () => debugLog, babelApi);
|
|---|
| 211 | const createVisitor = method === "entry-global" ? v.entry : v.usage;
|
|---|
| 212 | const visitor = provider.visitor ? traverse.visitors.merge([createVisitor(callProvider), provider.visitor]) : createVisitor(callProvider);
|
|---|
| 213 | if (debug && debug !== _debugUtils.presetEnvSilentDebugHeader) {
|
|---|
| 214 | console.log(`${providerName}: \`DEBUG\` option`);
|
|---|
| 215 | console.log(`\nUsing targets: ${(0, _debugUtils.stringifyTargetsMultiline)(targets)}`);
|
|---|
| 216 | console.log(`\nUsing polyfills with \`${method}\` method:`);
|
|---|
| 217 | }
|
|---|
| 218 | const {
|
|---|
| 219 | runtimeName
|
|---|
| 220 | } = provider;
|
|---|
| 221 | return {
|
|---|
| 222 | name: "inject-polyfills",
|
|---|
| 223 | visitor,
|
|---|
| 224 | pre(file) {
|
|---|
| 225 | var _provider$pre;
|
|---|
| 226 | if (runtimeName) {
|
|---|
| 227 | if (file.get("runtimeHelpersModuleName") && file.get("runtimeHelpersModuleName") !== runtimeName) {
|
|---|
| 228 | 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.`);
|
|---|
| 229 | } else {
|
|---|
| 230 | file.set("runtimeHelpersModuleName", runtimeName);
|
|---|
| 231 | file.set("runtimeHelpersModuleProvider", providerName);
|
|---|
| 232 | }
|
|---|
| 233 | }
|
|---|
| 234 | debugLog = {
|
|---|
| 235 | polyfills: new Set(),
|
|---|
| 236 | polyfillsSupport: undefined,
|
|---|
| 237 | found: false,
|
|---|
| 238 | providers: new Set(),
|
|---|
| 239 | missingDeps: new Set()
|
|---|
| 240 | };
|
|---|
| 241 | (_provider$pre = provider.pre) == null || _provider$pre.apply(this, arguments);
|
|---|
| 242 | },
|
|---|
| 243 | post() {
|
|---|
| 244 | var _provider$post;
|
|---|
| 245 | (_provider$post = provider.post) == null || _provider$post.apply(this, arguments);
|
|---|
| 246 | if (missingDependencies !== false) {
|
|---|
| 247 | if (missingDependencies.log === "per-file") {
|
|---|
| 248 | deps.logMissing(debugLog.missingDeps);
|
|---|
| 249 | } else {
|
|---|
| 250 | deps.laterLogMissing(debugLog.missingDeps);
|
|---|
| 251 | }
|
|---|
| 252 | }
|
|---|
| 253 | if (!debug) return;
|
|---|
| 254 | if (this.filename) console.log(`\n[${this.filename}]`);
|
|---|
| 255 | if (debugLog.polyfills.size === 0) {
|
|---|
| 256 | 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.`);
|
|---|
| 257 | return;
|
|---|
| 258 | }
|
|---|
| 259 | if (method === "entry-global") {
|
|---|
| 260 | console.log(`The ${providerName} polyfill entry has been replaced with ` + `the following polyfills:`);
|
|---|
| 261 | } else {
|
|---|
| 262 | console.log(`The ${providerName} polyfill added the following polyfills:`);
|
|---|
| 263 | }
|
|---|
| 264 | for (const name of debugLog.polyfills) {
|
|---|
| 265 | var _debugLog$polyfillsSu2;
|
|---|
| 266 | if ((_debugLog$polyfillsSu2 = debugLog.polyfillsSupport) != null && _debugLog$polyfillsSu2[name]) {
|
|---|
| 267 | const filteredTargets = (0, _helperCompilationTargets.getInclusionReasons)(name, targets, debugLog.polyfillsSupport);
|
|---|
| 268 | const formattedTargets = JSON.stringify(filteredTargets).replace(/,/g, ", ").replace(/^\{"/, '{ "').replace(/"\}$/, '" }');
|
|---|
| 269 | console.log(` ${name} ${formattedTargets}`);
|
|---|
| 270 | } else {
|
|---|
| 271 | console.log(` ${name}`);
|
|---|
| 272 | }
|
|---|
| 273 | }
|
|---|
| 274 | }
|
|---|
| 275 | };
|
|---|
| 276 | });
|
|---|
| 277 | }
|
|---|
| 278 | function mapGetOr(map, key, getDefault) {
|
|---|
| 279 | let val = map.get(key);
|
|---|
| 280 | if (val === undefined) {
|
|---|
| 281 | val = getDefault();
|
|---|
| 282 | map.set(key, val);
|
|---|
| 283 | }
|
|---|
| 284 | return val;
|
|---|
| 285 | }
|
|---|
| 286 | function isEmpty(obj) {
|
|---|
| 287 | return Object.keys(obj).length === 0;
|
|---|
| 288 | } |
|---|