| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | exports.__esModule = true;
|
|---|
| 4 | exports.default = void 0;
|
|---|
| 5 | var _corejs2BuiltIns = _interopRequireDefault(require("@babel/compat-data/corejs2-built-ins"));
|
|---|
| 6 | var _builtInDefinitions = require("./built-in-definitions");
|
|---|
| 7 | var _addPlatformSpecificPolyfills = _interopRequireDefault(require("./add-platform-specific-polyfills"));
|
|---|
| 8 | var _helpers = require("./helpers");
|
|---|
| 9 | var _helperDefinePolyfillProvider = _interopRequireDefault(require("@babel/helper-define-polyfill-provider"));
|
|---|
| 10 | var _babel = _interopRequireWildcard(require("@babel/core"));
|
|---|
| 11 | 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); }
|
|---|
| 12 | function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|---|
| 13 | const {
|
|---|
| 14 | types: t
|
|---|
| 15 | } = _babel.default || _babel;
|
|---|
| 16 | const BABEL_RUNTIME = "@babel/runtime-corejs2";
|
|---|
| 17 | const presetEnvCompat = "#__secret_key__@babel/preset-env__compatibility";
|
|---|
| 18 | const runtimeCompat = "#__secret_key__@babel/runtime__compatibility";
|
|---|
| 19 | const has = Function.call.bind(Object.hasOwnProperty);
|
|---|
| 20 | var _default = exports.default = (0, _helperDefinePolyfillProvider.default)(function (api, {
|
|---|
| 21 | [presetEnvCompat]: {
|
|---|
| 22 | entryInjectRegenerator = false,
|
|---|
| 23 | noRuntimeName = false
|
|---|
| 24 | } = {},
|
|---|
| 25 | [runtimeCompat]: {
|
|---|
| 26 | useBabelRuntime = false,
|
|---|
| 27 | runtimeVersion = "",
|
|---|
| 28 | ext = ".js"
|
|---|
| 29 | } = {}
|
|---|
| 30 | }) {
|
|---|
| 31 | const resolve = api.createMetaResolver({
|
|---|
| 32 | global: _builtInDefinitions.BuiltIns,
|
|---|
| 33 | static: _builtInDefinitions.StaticProperties,
|
|---|
| 34 | instance: _builtInDefinitions.InstanceProperties
|
|---|
| 35 | });
|
|---|
| 36 | const {
|
|---|
| 37 | debug,
|
|---|
| 38 | shouldInjectPolyfill,
|
|---|
| 39 | method
|
|---|
| 40 | } = api;
|
|---|
| 41 | const polyfills = (0, _addPlatformSpecificPolyfills.default)(api.targets, method, _corejs2BuiltIns.default);
|
|---|
| 42 | const coreJSBase = useBabelRuntime ? `${BABEL_RUNTIME}/core-js` : method === "usage-pure" ? "core-js/library/fn" : "core-js/modules";
|
|---|
| 43 | function inject(name, utils) {
|
|---|
| 44 | if (typeof name === "string") {
|
|---|
| 45 | // Some polyfills aren't always available, for example
|
|---|
| 46 | // web.dom.iterable when targeting node
|
|---|
| 47 | if (has(polyfills, name) && shouldInjectPolyfill(name)) {
|
|---|
| 48 | debug(name);
|
|---|
| 49 | utils.injectGlobalImport(`${coreJSBase}/${name}.js`);
|
|---|
| 50 | }
|
|---|
| 51 | return;
|
|---|
| 52 | }
|
|---|
| 53 | name.forEach(name => inject(name, utils));
|
|---|
| 54 | }
|
|---|
| 55 | function maybeInjectPure(desc, hint, utils) {
|
|---|
| 56 | let {
|
|---|
| 57 | pure,
|
|---|
| 58 | meta,
|
|---|
| 59 | name
|
|---|
| 60 | } = desc;
|
|---|
| 61 | if (!pure || !shouldInjectPolyfill(name)) return;
|
|---|
| 62 | if (runtimeVersion && meta && meta.minRuntimeVersion && !(0, _helpers.hasMinVersion)(meta && meta.minRuntimeVersion, runtimeVersion)) {
|
|---|
| 63 | return;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | // Unfortunately core-js and @babel/runtime-corejs2 don't have the same
|
|---|
| 67 | // directory structure, so we need to special case this.
|
|---|
| 68 | if (useBabelRuntime && pure === "symbol/index") pure = "symbol";
|
|---|
| 69 | return utils.injectDefaultImport(`${coreJSBase}/${pure}${ext}`, hint);
|
|---|
| 70 | }
|
|---|
| 71 | return {
|
|---|
| 72 | name: "corejs2",
|
|---|
| 73 | runtimeName: noRuntimeName ? null : BABEL_RUNTIME,
|
|---|
| 74 | polyfills,
|
|---|
| 75 | entryGlobal(meta, utils, path) {
|
|---|
| 76 | if (meta.kind === "import" && meta.source === "core-js") {
|
|---|
| 77 | debug(null);
|
|---|
| 78 | inject(Object.keys(polyfills), utils);
|
|---|
| 79 | if (entryInjectRegenerator) {
|
|---|
| 80 | utils.injectGlobalImport("regenerator-runtime/runtime.js");
|
|---|
| 81 | }
|
|---|
| 82 | path.remove();
|
|---|
| 83 | }
|
|---|
| 84 | },
|
|---|
| 85 | usageGlobal(meta, utils) {
|
|---|
| 86 | const resolved = resolve(meta);
|
|---|
| 87 | if (!resolved) return;
|
|---|
| 88 | let deps = resolved.desc.global;
|
|---|
| 89 | if (resolved.kind !== "global" && "object" in meta && meta.object && meta.placement === "prototype") {
|
|---|
| 90 | const low = meta.object.toLowerCase();
|
|---|
| 91 | deps = deps.filter(m => m.includes(low));
|
|---|
| 92 | }
|
|---|
| 93 | inject(deps, utils);
|
|---|
| 94 | },
|
|---|
| 95 | usagePure(meta, utils, path) {
|
|---|
| 96 | if (meta.kind === "in") {
|
|---|
| 97 | if (meta.key === "Symbol.iterator") {
|
|---|
| 98 | path.replaceWith(t.callExpression(utils.injectDefaultImport(`${coreJSBase}/is-iterable${ext}`, "isIterable"), [path.node.right] // meta.kind === "in" narrows this
|
|---|
| 99 | ));
|
|---|
| 100 | }
|
|---|
| 101 | return;
|
|---|
| 102 | }
|
|---|
| 103 | if (path.parentPath.isUnaryExpression({
|
|---|
| 104 | operator: "delete"
|
|---|
| 105 | })) return;
|
|---|
| 106 | if (meta.kind === "property") {
|
|---|
| 107 | // We can't compile destructuring.
|
|---|
| 108 | if (!path.isMemberExpression()) return;
|
|---|
| 109 | if (!path.isReferenced()) return;
|
|---|
| 110 | if (meta.key === "Symbol.iterator" && shouldInjectPolyfill("es6.symbol") && path.parentPath.isCallExpression({
|
|---|
| 111 | callee: path.node
|
|---|
| 112 | }) && path.parentPath.node.arguments.length === 0) {
|
|---|
| 113 | path.parentPath.replaceWith(t.callExpression(utils.injectDefaultImport(`${coreJSBase}/get-iterator${ext}`, "getIterator"), [path.node.object]));
|
|---|
| 114 | path.skip();
|
|---|
| 115 | return;
|
|---|
| 116 | }
|
|---|
| 117 | }
|
|---|
| 118 | const resolved = resolve(meta);
|
|---|
| 119 | if (!resolved) return;
|
|---|
| 120 | const id = maybeInjectPure(resolved.desc, resolved.name, utils);
|
|---|
| 121 | if (id) path.replaceWith(id);
|
|---|
| 122 | },
|
|---|
| 123 | visitor: method === "usage-global" && {
|
|---|
| 124 | // yield*
|
|---|
| 125 | YieldExpression(path) {
|
|---|
| 126 | if (path.node.delegate) {
|
|---|
| 127 | inject("web.dom.iterable", api.getUtils(path));
|
|---|
| 128 | }
|
|---|
| 129 | },
|
|---|
| 130 | // for-of, [a, b] = c
|
|---|
| 131 | "ForOfStatement|ArrayPattern"(path) {
|
|---|
| 132 | _builtInDefinitions.CommonIterators.forEach(name => inject(name, api.getUtils(path)));
|
|---|
| 133 | }
|
|---|
| 134 | }
|
|---|
| 135 | };
|
|---|
| 136 | }); |
|---|