[79a0317] | 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 _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); }
|
---|
| 15 | 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; }
|
---|
| 16 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
---|
| 17 | function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
---|
| 18 | const {
|
---|
| 19 | types: t
|
---|
| 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 = (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 |
|
---|
| 157 | return;
|
---|
| 158 | }
|
---|
| 159 | if (path.parentPath.isUnaryExpression({
|
---|
| 160 | operator: "delete"
|
---|
| 161 | })) return;
|
---|
| 162 | if (meta.kind === "property") {
|
---|
| 163 | // We can't compile destructuring and updateExpression.
|
---|
| 164 | if (!path.isMemberExpression()) return;
|
---|
| 165 | if (!path.isReferenced()) return;
|
---|
| 166 | if (path.parentPath.isUpdateExpression()) return;
|
---|
| 167 | if (t.isSuper(path.node.object)) {
|
---|
| 168 | return;
|
---|
| 169 | }
|
---|
| 170 | if (meta.key === "Symbol.iterator") {
|
---|
| 171 | if (!shouldInjectPolyfill("es.symbol.iterator")) return;
|
---|
| 172 | const {
|
---|
| 173 | parent,
|
---|
| 174 | node
|
---|
| 175 | } = path;
|
---|
| 176 | if (t.isCallExpression(parent, {
|
---|
| 177 | callee: node
|
---|
| 178 | })) {
|
---|
| 179 | if (parent.arguments.length === 0) {
|
---|
| 180 | path.parentPath.replaceWith(t.callExpression(utils.injectDefaultImport((0, _utils.coreJSPureHelper)("get-iterator", useBabelRuntime, ext), "getIterator"), [node.object]));
|
---|
| 181 | path.skip();
|
---|
| 182 | } else {
|
---|
| 183 | (0, _utils.callMethod)(path, utils.injectDefaultImport((0, _utils.coreJSPureHelper)("get-iterator-method", useBabelRuntime, ext), "getIteratorMethod"));
|
---|
| 184 | }
|
---|
| 185 | } else {
|
---|
| 186 | path.replaceWith(t.callExpression(utils.injectDefaultImport((0, _utils.coreJSPureHelper)("get-iterator-method", useBabelRuntime, ext), "getIteratorMethod"), [path.node.object]));
|
---|
| 187 | }
|
---|
| 188 | return;
|
---|
| 189 | }
|
---|
| 190 | }
|
---|
| 191 | let resolved = resolve(meta);
|
---|
| 192 | if (!resolved) return;
|
---|
| 193 | if ((0, _usageFilters.default)(resolved.desc, path)) return;
|
---|
| 194 | if (useBabelRuntime && resolved.desc.pure && resolved.desc.pure.slice(-6) === "/index") {
|
---|
| 195 | // Remove /index, since it doesn't exist in @babel/runtime-corejs3s
|
---|
| 196 | resolved = _extends({}, resolved, {
|
---|
| 197 | desc: _extends({}, resolved.desc, {
|
---|
| 198 | pure: resolved.desc.pure.slice(0, -6)
|
---|
| 199 | })
|
---|
| 200 | });
|
---|
| 201 | }
|
---|
| 202 | if (resolved.kind === "global") {
|
---|
| 203 | const id = maybeInjectPure(resolved.desc, resolved.name, utils);
|
---|
| 204 | if (id) path.replaceWith(id);
|
---|
| 205 | } else if (resolved.kind === "static") {
|
---|
| 206 | const id = maybeInjectPure(resolved.desc, resolved.name, utils,
|
---|
| 207 | // @ts-expect-error
|
---|
| 208 | meta.object);
|
---|
| 209 | if (id) path.replaceWith(id);
|
---|
| 210 | } else if (resolved.kind === "instance") {
|
---|
| 211 | const id = maybeInjectPure(resolved.desc, `${resolved.name}InstanceProperty`, utils,
|
---|
| 212 | // @ts-expect-error
|
---|
| 213 | meta.object);
|
---|
| 214 | if (!id) return;
|
---|
| 215 | const {
|
---|
| 216 | node
|
---|
| 217 | } = path;
|
---|
| 218 | if (t.isCallExpression(path.parent, {
|
---|
| 219 | callee: node
|
---|
| 220 | })) {
|
---|
| 221 | (0, _utils.callMethod)(path, id);
|
---|
| 222 | } else {
|
---|
| 223 | path.replaceWith(t.callExpression(id, [node.object]));
|
---|
| 224 | }
|
---|
| 225 | }
|
---|
| 226 | },
|
---|
| 227 | visitor: method === "usage-global" && {
|
---|
| 228 | // import("foo")
|
---|
| 229 | CallExpression(path) {
|
---|
| 230 | if (path.get("callee").isImport()) {
|
---|
| 231 | const utils = getUtils(path);
|
---|
| 232 | if (isWebpack) {
|
---|
| 233 | // Webpack uses Promise.all to handle dynamic import.
|
---|
| 234 | maybeInjectGlobal(_builtInDefinitions.PromiseDependenciesWithIterators, utils);
|
---|
| 235 | } else {
|
---|
| 236 | maybeInjectGlobal(_builtInDefinitions.PromiseDependencies, utils);
|
---|
| 237 | }
|
---|
| 238 | }
|
---|
| 239 | },
|
---|
| 240 | // (async function () { }).finally(...)
|
---|
| 241 | Function(path) {
|
---|
| 242 | if (path.node.async) {
|
---|
| 243 | maybeInjectGlobal(_builtInDefinitions.PromiseDependencies, getUtils(path));
|
---|
| 244 | }
|
---|
| 245 | },
|
---|
| 246 | // for-of, [a, b] = c
|
---|
| 247 | "ForOfStatement|ArrayPattern"(path) {
|
---|
| 248 | maybeInjectGlobal(_builtInDefinitions.CommonIterators, getUtils(path));
|
---|
| 249 | },
|
---|
| 250 | // [...spread]
|
---|
| 251 | SpreadElement(path) {
|
---|
| 252 | if (!path.parentPath.isObjectExpression()) {
|
---|
| 253 | maybeInjectGlobal(_builtInDefinitions.CommonIterators, getUtils(path));
|
---|
| 254 | }
|
---|
| 255 | },
|
---|
| 256 | // yield*
|
---|
| 257 | YieldExpression(path) {
|
---|
| 258 | if (path.node.delegate) {
|
---|
| 259 | maybeInjectGlobal(_builtInDefinitions.CommonIterators, getUtils(path));
|
---|
| 260 | }
|
---|
| 261 | },
|
---|
| 262 | // Decorators metadata
|
---|
| 263 | Class(path) {
|
---|
| 264 | var _path$node$decorators;
|
---|
| 265 | const hasDecorators = ((_path$node$decorators = path.node.decorators) == null ? void 0 : _path$node$decorators.length) || path.node.body.body.some(el => {
|
---|
| 266 | var _decorators;
|
---|
| 267 | return (_decorators = el.decorators) == null ? void 0 : _decorators.length;
|
---|
| 268 | });
|
---|
| 269 | if (hasDecorators) {
|
---|
| 270 | maybeInjectGlobal(_builtInDefinitions.DecoratorMetadataDependencies, getUtils(path));
|
---|
| 271 | }
|
---|
| 272 | }
|
---|
| 273 | }
|
---|
| 274 | };
|
---|
| 275 | });
|
---|
| 276 | exports.default = _default; |
---|