| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | exports.__esModule = true;
|
|---|
| 4 | exports.default = void 0;
|
|---|
| 5 | var _data = _interopRequireDefault(require("../core-js-compat/data.js"));
|
|---|
| 6 | var _shippedProposals = _interopRequireDefault(require("./shipped-proposals"));
|
|---|
| 7 | var _getModulesListForTargetVersion = _interopRequireDefault(require("../core-js-compat/get-modules-list-for-target-version.js"));
|
|---|
| 8 | var _builtInDefinitions = require("./built-in-definitions");
|
|---|
| 9 | var BabelRuntimePaths = _interopRequireWildcard(require("./babel-runtime-corejs3-paths"));
|
|---|
| 10 | var _usageFilters = _interopRequireDefault(require("./usage-filters"));
|
|---|
| 11 | var _babel = _interopRequireWildcard(require("@babel/core"));
|
|---|
| 12 | var _utils = require("./utils");
|
|---|
| 13 | var _helperDefinePolyfillProvider = _interopRequireDefault(require("@babel/helper-define-polyfill-provider"));
|
|---|
| 14 | 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); }
|
|---|
| 15 | function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|---|
| 16 | function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
|
|---|
| 17 | const {
|
|---|
| 18 | types: t,
|
|---|
| 19 | template: template
|
|---|
| 20 | } = _babel.default || _babel;
|
|---|
| 21 | const presetEnvCompat = "#__secret_key__@babel/preset-env__compatibility";
|
|---|
| 22 | const runtimeCompat = "#__secret_key__@babel/runtime__compatibility";
|
|---|
| 23 | const uniqueObjects = ["array", "string", "iterator", "async-iterator", "dom-collections"].map(v => new RegExp(`[a-z]*\\.${v}\\..*`));
|
|---|
| 24 | const esnextFallback = (name, cb) => {
|
|---|
| 25 | if (cb(name)) return true;
|
|---|
| 26 | if (!name.startsWith("es.")) return false;
|
|---|
| 27 | const fallback = `esnext.${name.slice(3)}`;
|
|---|
| 28 | if (!_data.default[fallback]) return false;
|
|---|
| 29 | return cb(fallback);
|
|---|
| 30 | };
|
|---|
| 31 | var _default = exports.default = (0, _helperDefinePolyfillProvider.default)(function ({
|
|---|
| 32 | getUtils,
|
|---|
| 33 | method,
|
|---|
| 34 | shouldInjectPolyfill,
|
|---|
| 35 | createMetaResolver,
|
|---|
| 36 | debug,
|
|---|
| 37 | babel
|
|---|
| 38 | }, {
|
|---|
| 39 | version = 3,
|
|---|
| 40 | proposals,
|
|---|
| 41 | shippedProposals,
|
|---|
| 42 | [presetEnvCompat]: {
|
|---|
| 43 | noRuntimeName = false
|
|---|
| 44 | } = {},
|
|---|
| 45 | [runtimeCompat]: {
|
|---|
| 46 | useBabelRuntime = false,
|
|---|
| 47 | ext = ".js"
|
|---|
| 48 | } = {}
|
|---|
| 49 | }) {
|
|---|
| 50 | const isWebpack = babel.caller(caller => (caller == null ? void 0 : caller.name) === "babel-loader");
|
|---|
| 51 | const resolve = createMetaResolver({
|
|---|
| 52 | global: _builtInDefinitions.BuiltIns,
|
|---|
| 53 | static: _builtInDefinitions.StaticProperties,
|
|---|
| 54 | instance: _builtInDefinitions.InstanceProperties
|
|---|
| 55 | });
|
|---|
| 56 | const available = new Set((0, _getModulesListForTargetVersion.default)(version));
|
|---|
| 57 | function getCoreJSPureBase(useProposalBase) {
|
|---|
| 58 | return useBabelRuntime ? useProposalBase ? `${_utils.BABEL_RUNTIME}/core-js` : `${_utils.BABEL_RUNTIME}/core-js-stable` : useProposalBase ? "core-js-pure/features" : "core-js-pure/stable";
|
|---|
| 59 | }
|
|---|
| 60 | function maybeInjectGlobalImpl(name, utils) {
|
|---|
| 61 | if (shouldInjectPolyfill(name)) {
|
|---|
| 62 | debug(name);
|
|---|
| 63 | utils.injectGlobalImport((0, _utils.coreJSModule)(name), name);
|
|---|
| 64 | return true;
|
|---|
| 65 | }
|
|---|
| 66 | return false;
|
|---|
| 67 | }
|
|---|
| 68 | function maybeInjectGlobal(names, utils, fallback = true) {
|
|---|
| 69 | for (const name of names) {
|
|---|
| 70 | if (fallback) {
|
|---|
| 71 | esnextFallback(name, name => maybeInjectGlobalImpl(name, utils));
|
|---|
| 72 | } else {
|
|---|
| 73 | maybeInjectGlobalImpl(name, utils);
|
|---|
| 74 | }
|
|---|
| 75 | }
|
|---|
| 76 | }
|
|---|
| 77 | function maybeInjectPure(desc, hint, utils, object) {
|
|---|
| 78 | if (desc.pure && !(object && desc.exclude && desc.exclude.includes(object)) && esnextFallback(desc.name, shouldInjectPolyfill)) {
|
|---|
| 79 | const {
|
|---|
| 80 | name
|
|---|
| 81 | } = desc;
|
|---|
| 82 | let useProposalBase = false;
|
|---|
| 83 | if (proposals || shippedProposals && name.startsWith("esnext.")) {
|
|---|
| 84 | useProposalBase = true;
|
|---|
| 85 | } else if (name.startsWith("es.") && !available.has(name)) {
|
|---|
| 86 | useProposalBase = true;
|
|---|
| 87 | }
|
|---|
| 88 | if (useBabelRuntime && !(useProposalBase ? BabelRuntimePaths.proposals : BabelRuntimePaths.stable).has(desc.pure)) {
|
|---|
| 89 | return;
|
|---|
| 90 | }
|
|---|
| 91 | const coreJSPureBase = getCoreJSPureBase(useProposalBase);
|
|---|
| 92 | return utils.injectDefaultImport(`${coreJSPureBase}/${desc.pure}${ext}`, hint);
|
|---|
| 93 | }
|
|---|
| 94 | }
|
|---|
| 95 | function isFeatureStable(name) {
|
|---|
| 96 | if (name.startsWith("esnext.")) {
|
|---|
| 97 | const esName = `es.${name.slice(7)}`;
|
|---|
| 98 | // If its imaginative esName is not in latest compat data, it means
|
|---|
| 99 | // the proposal is not stage 4
|
|---|
| 100 | return esName in _data.default;
|
|---|
| 101 | }
|
|---|
| 102 | return true;
|
|---|
| 103 | }
|
|---|
| 104 | return {
|
|---|
| 105 | name: "corejs3",
|
|---|
| 106 | runtimeName: noRuntimeName ? null : _utils.BABEL_RUNTIME,
|
|---|
| 107 | polyfills: _data.default,
|
|---|
| 108 | filterPolyfills(name) {
|
|---|
| 109 | if (!available.has(name)) return false;
|
|---|
| 110 | if (proposals || method === "entry-global") return true;
|
|---|
| 111 | if (shippedProposals && _shippedProposals.default.has(name)) {
|
|---|
| 112 | return true;
|
|---|
| 113 | }
|
|---|
| 114 | return isFeatureStable(name);
|
|---|
| 115 | },
|
|---|
| 116 | entryGlobal(meta, utils, path) {
|
|---|
| 117 | if (meta.kind !== "import") return;
|
|---|
| 118 | const modules = (0, _utils.isCoreJSSource)(meta.source);
|
|---|
| 119 | if (!modules) return;
|
|---|
| 120 | if (modules.length === 1 && meta.source === (0, _utils.coreJSModule)(modules[0]) && shouldInjectPolyfill(modules[0])) {
|
|---|
| 121 | // Avoid infinite loop: do not replace imports with a new copy of
|
|---|
| 122 | // themselves.
|
|---|
| 123 | debug(null);
|
|---|
| 124 | return;
|
|---|
| 125 | }
|
|---|
| 126 | const modulesSet = new Set(modules);
|
|---|
| 127 | const filteredModules = modules.filter(module => {
|
|---|
| 128 | if (!module.startsWith("esnext.")) return true;
|
|---|
| 129 | const stable = module.replace("esnext.", "es.");
|
|---|
| 130 | if (modulesSet.has(stable) && shouldInjectPolyfill(stable)) {
|
|---|
| 131 | return false;
|
|---|
| 132 | }
|
|---|
| 133 | return true;
|
|---|
| 134 | });
|
|---|
| 135 | maybeInjectGlobal(filteredModules, utils, false);
|
|---|
| 136 | path.remove();
|
|---|
| 137 | },
|
|---|
| 138 | usageGlobal(meta, utils, path) {
|
|---|
| 139 | const resolved = resolve(meta);
|
|---|
| 140 | if (!resolved) return;
|
|---|
| 141 | if ((0, _usageFilters.default)(resolved.desc, path)) return;
|
|---|
| 142 | let deps = resolved.desc.global;
|
|---|
| 143 | if (resolved.kind !== "global" && "object" in meta && meta.object && meta.placement === "prototype") {
|
|---|
| 144 | const low = meta.object.toLowerCase();
|
|---|
| 145 | deps = deps.filter(m => uniqueObjects.some(v => v.test(m)) ? m.includes(low) : true);
|
|---|
| 146 | }
|
|---|
| 147 | maybeInjectGlobal(deps, utils);
|
|---|
| 148 | return true;
|
|---|
| 149 | },
|
|---|
| 150 | usagePure(meta, utils, path) {
|
|---|
| 151 | if (meta.kind === "in") {
|
|---|
| 152 | if (meta.key === "Symbol.iterator") {
|
|---|
| 153 | path.replaceWith(t.callExpression(utils.injectDefaultImport((0, _utils.coreJSPureHelper)("is-iterable", useBabelRuntime, ext), "isIterable"), [path.node.right] // meta.kind === "in" narrows this
|
|---|
| 154 | ));
|
|---|
| 155 | }
|
|---|
| 156 | return;
|
|---|
| 157 | }
|
|---|
| 158 | if (path.parentPath.isUnaryExpression({
|
|---|
| 159 | operator: "delete"
|
|---|
| 160 | })) return;
|
|---|
| 161 | if (meta.kind === "property") {
|
|---|
| 162 | // We can't compile destructuring and updateExpression.
|
|---|
| 163 | if (!path.isMemberExpression() && !path.isOptionalMemberExpression()) {
|
|---|
| 164 | return;
|
|---|
| 165 | }
|
|---|
| 166 | if (!path.isReferenced()) return;
|
|---|
| 167 | if (path.parentPath.isUpdateExpression()) return;
|
|---|
| 168 | if (t.isSuper(path.node.object)) {
|
|---|
| 169 | return;
|
|---|
| 170 | }
|
|---|
| 171 | if (meta.key === "Symbol.iterator") {
|
|---|
| 172 | if (!shouldInjectPolyfill("es.symbol.iterator")) return;
|
|---|
| 173 | const {
|
|---|
| 174 | parent,
|
|---|
| 175 | node
|
|---|
| 176 | } = path;
|
|---|
| 177 | if (t.isCallExpression(parent, {
|
|---|
| 178 | callee: node
|
|---|
| 179 | })) {
|
|---|
| 180 | if (parent.arguments.length === 0) {
|
|---|
| 181 | path.parentPath.replaceWith(t.callExpression(utils.injectDefaultImport((0, _utils.coreJSPureHelper)("get-iterator", useBabelRuntime, ext), "getIterator"), [node.object]));
|
|---|
| 182 | path.skip();
|
|---|
| 183 | } else {
|
|---|
| 184 | (0, _utils.callMethod)(path, utils.injectDefaultImport((0, _utils.coreJSPureHelper)("get-iterator-method", useBabelRuntime, ext), "getIteratorMethod"));
|
|---|
| 185 | }
|
|---|
| 186 | } else {
|
|---|
| 187 | path.replaceWith(t.callExpression(utils.injectDefaultImport((0, _utils.coreJSPureHelper)("get-iterator-method", useBabelRuntime, ext), "getIteratorMethod"), [path.node.object]));
|
|---|
| 188 | }
|
|---|
| 189 | return;
|
|---|
| 190 | }
|
|---|
| 191 | }
|
|---|
| 192 | let resolved = resolve(meta);
|
|---|
| 193 | if (!resolved) return;
|
|---|
| 194 | if ((0, _usageFilters.default)(resolved.desc, path)) return;
|
|---|
| 195 | if (useBabelRuntime && resolved.desc.pure && resolved.desc.pure.slice(-6) === "/index") {
|
|---|
| 196 | // Remove /index, since it doesn't exist in @babel/runtime-corejs3s
|
|---|
| 197 | resolved = _extends({}, resolved, {
|
|---|
| 198 | desc: _extends({}, resolved.desc, {
|
|---|
| 199 | pure: resolved.desc.pure.slice(0, -6)
|
|---|
| 200 | })
|
|---|
| 201 | });
|
|---|
| 202 | }
|
|---|
| 203 | if (resolved.kind === "global") {
|
|---|
| 204 | const id = maybeInjectPure(resolved.desc, resolved.name, utils);
|
|---|
| 205 | if (id) path.replaceWith(id);
|
|---|
| 206 | } else if (resolved.kind === "static") {
|
|---|
| 207 | const id = maybeInjectPure(resolved.desc, resolved.name, utils,
|
|---|
| 208 | // @ts-expect-error
|
|---|
| 209 | meta.object);
|
|---|
| 210 | if (id) {
|
|---|
| 211 | path.replaceWith(id);
|
|---|
| 212 | let {
|
|---|
| 213 | parentPath
|
|---|
| 214 | } = path;
|
|---|
| 215 | if (parentPath.isOptionalMemberExpression() || parentPath.isOptionalCallExpression()) {
|
|---|
| 216 | do {
|
|---|
| 217 | const parentAsNotOptional = parentPath;
|
|---|
| 218 | parentAsNotOptional.type = parentAsNotOptional.node.type = parentPath.type === "OptionalMemberExpression" ? "MemberExpression" : "CallExpression";
|
|---|
| 219 | delete parentAsNotOptional.node.optional;
|
|---|
| 220 | ({
|
|---|
| 221 | parentPath
|
|---|
| 222 | } = parentPath);
|
|---|
| 223 | } while ((parentPath.isOptionalMemberExpression() || parentPath.isOptionalCallExpression()) && !parentPath.node.optional);
|
|---|
| 224 | }
|
|---|
| 225 | }
|
|---|
| 226 | } else if (resolved.kind === "instance") {
|
|---|
| 227 | const id = maybeInjectPure(resolved.desc, `${resolved.name}InstanceProperty`, utils,
|
|---|
| 228 | // @ts-expect-error
|
|---|
| 229 | meta.object);
|
|---|
| 230 | if (!id) return;
|
|---|
| 231 | const {
|
|---|
| 232 | node,
|
|---|
| 233 | parent
|
|---|
| 234 | } = path;
|
|---|
| 235 | if (t.isOptionalCallExpression(parent) && parent.callee === node) {
|
|---|
| 236 | const wasOptional = parent.optional;
|
|---|
| 237 | parent.optional = !wasOptional;
|
|---|
| 238 | if (!wasOptional) {
|
|---|
| 239 | const check = (0, _utils.extractOptionalCheck)(path.scope, node);
|
|---|
| 240 | const [thisArg, thisArg2] = (0, _utils.maybeMemoizeContext)(node, path.scope);
|
|---|
| 241 | path.replaceWith(check(template.expression.ast`
|
|---|
| 242 | Function.call.bind(${id}(${thisArg}), ${thisArg2})
|
|---|
| 243 | `));
|
|---|
| 244 | } else if (t.isOptionalMemberExpression(node)) {
|
|---|
| 245 | const check = (0, _utils.extractOptionalCheck)(path.scope, node);
|
|---|
| 246 | (0, _utils.callMethod)(path, id, true, check);
|
|---|
| 247 | } else {
|
|---|
| 248 | (0, _utils.callMethod)(path, id, true);
|
|---|
| 249 | }
|
|---|
| 250 | } else if (t.isCallExpression(parent) && parent.callee === node) {
|
|---|
| 251 | (0, _utils.callMethod)(path, id, false);
|
|---|
| 252 | } else if (t.isOptionalMemberExpression(node)) {
|
|---|
| 253 | const check = (0, _utils.extractOptionalCheck)(path.scope, node);
|
|---|
| 254 | path.replaceWith(check(t.callExpression(id, [node.object])));
|
|---|
| 255 | if (t.isOptionalMemberExpression(parent)) parent.optional = true;
|
|---|
| 256 | } else {
|
|---|
| 257 | path.replaceWith(t.callExpression(id, [node.object]));
|
|---|
| 258 | }
|
|---|
| 259 | }
|
|---|
| 260 | },
|
|---|
| 261 | visitor: method === "usage-global" && {
|
|---|
| 262 | // import("foo")
|
|---|
| 263 | CallExpression(path) {
|
|---|
| 264 | if (path.get("callee").isImport()) {
|
|---|
| 265 | const utils = getUtils(path);
|
|---|
| 266 | if (isWebpack) {
|
|---|
| 267 | // Webpack uses Promise.all to handle dynamic import.
|
|---|
| 268 | maybeInjectGlobal(_builtInDefinitions.PromiseDependenciesWithIterators, utils);
|
|---|
| 269 | } else {
|
|---|
| 270 | maybeInjectGlobal(_builtInDefinitions.PromiseDependencies, utils);
|
|---|
| 271 | }
|
|---|
| 272 | }
|
|---|
| 273 | },
|
|---|
| 274 | // (async function () { }).finally(...)
|
|---|
| 275 | Function(path) {
|
|---|
| 276 | if (path.node.async) {
|
|---|
| 277 | maybeInjectGlobal(_builtInDefinitions.PromiseDependencies, getUtils(path));
|
|---|
| 278 | }
|
|---|
| 279 | },
|
|---|
| 280 | // for-of, [a, b] = c
|
|---|
| 281 | "ForOfStatement|ArrayPattern"(path) {
|
|---|
| 282 | maybeInjectGlobal(_builtInDefinitions.CommonIterators, getUtils(path));
|
|---|
| 283 | },
|
|---|
| 284 | // [...spread]
|
|---|
| 285 | SpreadElement(path) {
|
|---|
| 286 | if (!path.parentPath.isObjectExpression()) {
|
|---|
| 287 | maybeInjectGlobal(_builtInDefinitions.CommonIterators, getUtils(path));
|
|---|
| 288 | }
|
|---|
| 289 | },
|
|---|
| 290 | // yield*
|
|---|
| 291 | YieldExpression(path) {
|
|---|
| 292 | if (path.node.delegate) {
|
|---|
| 293 | maybeInjectGlobal(_builtInDefinitions.CommonIterators, getUtils(path));
|
|---|
| 294 | }
|
|---|
| 295 | },
|
|---|
| 296 | // Decorators metadata
|
|---|
| 297 | Class(path) {
|
|---|
| 298 | var _path$node$decorators;
|
|---|
| 299 | const hasDecorators = ((_path$node$decorators = path.node.decorators) == null ? void 0 : _path$node$decorators.length) || path.node.body.body.some(el => {
|
|---|
| 300 | var _decorators;
|
|---|
| 301 | return (_decorators = el.decorators) == null ? void 0 : _decorators.length;
|
|---|
| 302 | });
|
|---|
| 303 | if (hasDecorators) {
|
|---|
| 304 | maybeInjectGlobal(_builtInDefinitions.DecoratorMetadataDependencies, getUtils(path));
|
|---|
| 305 | }
|
|---|
| 306 | }
|
|---|
| 307 | }
|
|---|
| 308 | };
|
|---|
| 309 | }); |
|---|