index.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = void 0;
  4. var _data = _interopRequireDefault(require("../core-js-compat/data.js"));
  5. var _shippedProposals = _interopRequireDefault(require("./shipped-proposals"));
  6. var _getModulesListForTargetVersion = _interopRequireDefault(require("../core-js-compat/get-modules-list-for-target-version.js"));
  7. var _builtInDefinitions = require("./built-in-definitions");
  8. var BabelRuntimePaths = _interopRequireWildcard(require("./babel-runtime-corejs3-paths"));
  9. var _usageFilters = _interopRequireDefault(require("./usage-filters"));
  10. var _babel = _interopRequireWildcard(require("@babel/core"));
  11. var _utils = require("./utils");
  12. var _helperDefinePolyfillProvider = _interopRequireDefault(require("@babel/helper-define-polyfill-provider"));
  13. 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); }
  14. 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; }
  15. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  16. 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); }
  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 = (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. });
  310. exports.default = _default;