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