index.js 9.0 KB

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