index.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = void 0;
  4. var _data = _interopRequireDefault(require("core-js-compat/data"));
  5. var _shippedProposals = _interopRequireDefault(require("./shipped-proposals"));
  6. var _getModulesListForTargetVersion = _interopRequireDefault(require("core-js-compat/get-modules-list-for-target-version"));
  7. var _builtInDefinitions = require("./built-in-definitions");
  8. var _core = require("@babel/core");
  9. var _utils = require("./utils");
  10. var _helperDefinePolyfillProvider = _interopRequireDefault(require("@babel/helper-define-polyfill-provider"));
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12. function _extends() { _extends = Object.assign || 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); }
  13. const runtimeCompat = "#__secret_key__@babel/runtime__compatibility";
  14. const esnextFallback = (name, cb) => {
  15. if (cb(name)) return true;
  16. if (!name.startsWith("es.")) return false;
  17. const fallback = `esnext.${name.slice(3)}`;
  18. if (!_data.default[fallback]) return false;
  19. return cb(fallback);
  20. };
  21. var _default = (0, _helperDefinePolyfillProvider.default)(function ({
  22. getUtils,
  23. method,
  24. shouldInjectPolyfill,
  25. createMetaResolver,
  26. debug,
  27. babel
  28. }, {
  29. version = 3,
  30. proposals,
  31. shippedProposals,
  32. [runtimeCompat]: {
  33. useBabelRuntime,
  34. ext = ".js"
  35. } = {}
  36. }) {
  37. const isWebpack = babel.caller(caller => (caller == null ? void 0 : caller.name) === "babel-loader");
  38. const resolve = createMetaResolver({
  39. global: _builtInDefinitions.BuiltIns,
  40. static: _builtInDefinitions.StaticProperties,
  41. instance: _builtInDefinitions.InstanceProperties
  42. });
  43. const available = new Set((0, _getModulesListForTargetVersion.default)(version));
  44. const coreJSPureBase = useBabelRuntime ? proposals ? `${useBabelRuntime}/core-js` : `${useBabelRuntime}/core-js-stable` : proposals ? "core-js-pure/features" : "core-js-pure/stable";
  45. function maybeInjectGlobalImpl(name, utils) {
  46. if (shouldInjectPolyfill(name)) {
  47. debug(name);
  48. utils.injectGlobalImport((0, _utils.coreJSModule)(name));
  49. return true;
  50. }
  51. return false;
  52. }
  53. function maybeInjectGlobal(names, utils, fallback = true) {
  54. for (const name of names) {
  55. if (fallback) {
  56. esnextFallback(name, name => maybeInjectGlobalImpl(name, utils));
  57. } else {
  58. maybeInjectGlobalImpl(name, utils);
  59. }
  60. }
  61. }
  62. function maybeInjectPure(desc, hint, utils, object) {
  63. if (desc.pure && !(object && desc.exclude && desc.exclude.includes(object)) && esnextFallback(desc.name, shouldInjectPolyfill)) {
  64. return utils.injectDefaultImport(`${coreJSPureBase}/${desc.pure}${ext}`, hint);
  65. }
  66. }
  67. return {
  68. name: "corejs3",
  69. polyfills: _data.default,
  70. filterPolyfills(name) {
  71. if (!available.has(name)) return false;
  72. if (proposals || method === "entry-global") return true;
  73. if (shippedProposals && _shippedProposals.default.has(name)) {
  74. return true;
  75. }
  76. return !name.startsWith("esnext.");
  77. },
  78. entryGlobal(meta, utils, path) {
  79. if (meta.kind !== "import") return;
  80. const modules = (0, _utils.isCoreJSSource)(meta.source);
  81. if (!modules) return;
  82. if (modules.length === 1 && meta.source === (0, _utils.coreJSModule)(modules[0]) && shouldInjectPolyfill(modules[0])) {
  83. // Avoid infinite loop: do not replace imports with a new copy of
  84. // themselves.
  85. debug(null);
  86. return;
  87. }
  88. maybeInjectGlobal(modules, utils, false);
  89. path.remove();
  90. },
  91. usageGlobal(meta, utils) {
  92. const resolved = resolve(meta);
  93. if (!resolved) return;
  94. let deps = resolved.desc.global;
  95. if (resolved.kind !== "global" && meta.object && meta.placement === "prototype") {
  96. const low = meta.object.toLowerCase();
  97. deps = deps.filter(m => m.includes(low) || _builtInDefinitions.CommonInstanceDependencies.has(m));
  98. }
  99. maybeInjectGlobal(deps, utils);
  100. },
  101. usagePure(meta, utils, path) {
  102. if (meta.kind === "in") {
  103. if (meta.key === "Symbol.iterator") {
  104. path.replaceWith(_core.types.callExpression(utils.injectDefaultImport((0, _utils.coreJSPureHelper)("is-iterable", useBabelRuntime, ext), "isIterable"), [path.node.right]));
  105. }
  106. return;
  107. }
  108. if (path.parentPath.isUnaryExpression({
  109. operator: "delete"
  110. })) return;
  111. let isCall;
  112. if (meta.kind === "property") {
  113. // We can't compile destructuring.
  114. if (!path.isMemberExpression()) return;
  115. if (!path.isReferenced()) return;
  116. isCall = path.parentPath.isCallExpression({
  117. callee: path.node
  118. });
  119. if (meta.key === "Symbol.iterator") {
  120. if (!shouldInjectPolyfill("es.symbol.iterator")) return;
  121. if (isCall) {
  122. if (path.parent.arguments.length === 0) {
  123. path.parentPath.replaceWith(_core.types.callExpression(utils.injectDefaultImport((0, _utils.coreJSPureHelper)("get-iterator", useBabelRuntime, ext), "getIterator"), [path.node.object]));
  124. path.skip();
  125. } else {
  126. (0, _utils.callMethod)(path, utils.injectDefaultImport((0, _utils.coreJSPureHelper)("get-iterator-method", useBabelRuntime, ext), "getIteratorMethod"));
  127. }
  128. } else {
  129. path.replaceWith(_core.types.callExpression(utils.injectDefaultImport((0, _utils.coreJSPureHelper)("get-iterator-method", useBabelRuntime, ext), "getIteratorMethod"), [path.node.object]));
  130. }
  131. return;
  132. }
  133. }
  134. let resolved = resolve(meta);
  135. if (!resolved) return;
  136. if (useBabelRuntime && resolved.desc.pure && resolved.desc.pure.slice(-6) === "/index") {
  137. // Remove /index, since it doesn't exist in @babel/runtime-corejs3s
  138. resolved = _extends({}, resolved, {
  139. desc: _extends({}, resolved.desc, {
  140. pure: resolved.desc.pure.slice(0, -6)
  141. })
  142. });
  143. }
  144. if (resolved.kind === "global") {
  145. const id = maybeInjectPure(resolved.desc, resolved.name, utils);
  146. if (id) path.replaceWith(id);
  147. } else if (resolved.kind === "static") {
  148. const id = maybeInjectPure(resolved.desc, resolved.name, utils, // $FlowIgnore
  149. meta.object);
  150. if (id) path.replaceWith(id);
  151. } else if (resolved.kind === "instance") {
  152. const id = maybeInjectPure(resolved.desc, `${resolved.name}InstanceProperty`, utils, // $FlowIgnore
  153. meta.object);
  154. if (!id) return;
  155. if (isCall) {
  156. (0, _utils.callMethod)(path, id);
  157. } else {
  158. path.replaceWith(_core.types.callExpression(id, [path.node.object]));
  159. }
  160. }
  161. },
  162. visitor: method === "usage-global" && {
  163. // import("foo")
  164. CallExpression(path) {
  165. if (path.get("callee").isImport()) {
  166. const utils = getUtils(path);
  167. if (isWebpack) {
  168. // Webpack uses Promise.all to handle dynamic import.
  169. maybeInjectGlobal(_builtInDefinitions.PromiseDependenciesWithIterators, utils);
  170. } else {
  171. maybeInjectGlobal(_builtInDefinitions.PromiseDependencies, utils);
  172. }
  173. }
  174. },
  175. // (async function () { }).finally(...)
  176. Function(path) {
  177. if (path.node.async) {
  178. maybeInjectGlobal(_builtInDefinitions.PromiseDependencies, getUtils(path));
  179. }
  180. },
  181. // for-of, [a, b] = c
  182. "ForOfStatement|ArrayPattern"(path) {
  183. maybeInjectGlobal(_builtInDefinitions.CommonIterators, getUtils(path));
  184. },
  185. // [...spread]
  186. SpreadElement(path) {
  187. if (!path.parentPath.isObjectExpression()) {
  188. maybeInjectGlobal(_builtInDefinitions.CommonIterators, getUtils(path));
  189. }
  190. },
  191. // yield*
  192. YieldExpression(path) {
  193. if (path.node.delegate) {
  194. maybeInjectGlobal(_builtInDefinitions.CommonIterators, getUtils(path));
  195. }
  196. }
  197. }
  198. };
  199. });
  200. exports.default = _default;