index.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. function getCoreJSPureBase(useProposalBase) {
  50. return useBabelRuntime ? useProposalBase ? `${useBabelRuntime}/core-js` : `${useBabelRuntime}/core-js-stable` : useProposalBase ? "core-js-pure/features" : "core-js-pure/stable";
  51. }
  52. function maybeInjectGlobalImpl(name, utils) {
  53. if (shouldInjectPolyfill(name)) {
  54. debug(name);
  55. utils.injectGlobalImport((0, _utils.coreJSModule)(name));
  56. return true;
  57. }
  58. return false;
  59. }
  60. function maybeInjectGlobal(names, utils, fallback = true) {
  61. for (const name of names) {
  62. if (fallback) {
  63. esnextFallback(name, name => maybeInjectGlobalImpl(name, utils));
  64. } else {
  65. maybeInjectGlobalImpl(name, utils);
  66. }
  67. }
  68. }
  69. function maybeInjectPure(desc, hint, utils, object) {
  70. if (desc.pure && !(object && desc.exclude && desc.exclude.includes(object)) && esnextFallback(desc.name, shouldInjectPolyfill)) {
  71. const {
  72. name
  73. } = desc;
  74. let useProposalBase = false;
  75. if (proposals || shippedProposals && name.startsWith("esnext.")) {
  76. useProposalBase = true;
  77. } else if (name.startsWith("es.") && !available.has(name)) {
  78. useProposalBase = true;
  79. }
  80. const coreJSPureBase = getCoreJSPureBase(useProposalBase);
  81. return utils.injectDefaultImport( // $FlowIgnore, we already guard desc.pure
  82. `${coreJSPureBase}/${desc.pure}${ext}`, hint);
  83. }
  84. }
  85. function isFeatureStable(name) {
  86. if (name.startsWith("esnext.")) {
  87. const esName = `es.${name.slice(7)}`; // If its imaginative esName is not in latest compat data, it means
  88. // the proposal is not stage 4
  89. return esName in _data.default;
  90. }
  91. return true;
  92. }
  93. return {
  94. name: "corejs3",
  95. polyfills: _data.default,
  96. filterPolyfills(name) {
  97. if (!available.has(name)) return false;
  98. if (proposals || method === "entry-global") return true;
  99. if (shippedProposals && _shippedProposals.default.has(name)) {
  100. return true;
  101. }
  102. return isFeatureStable(name);
  103. },
  104. entryGlobal(meta, utils, path) {
  105. if (meta.kind !== "import") return;
  106. const modules = (0, _utils.isCoreJSSource)(meta.source);
  107. if (!modules) return;
  108. if (modules.length === 1 && meta.source === (0, _utils.coreJSModule)(modules[0]) && shouldInjectPolyfill(modules[0])) {
  109. // Avoid infinite loop: do not replace imports with a new copy of
  110. // themselves.
  111. debug(null);
  112. return;
  113. }
  114. maybeInjectGlobal(modules, utils, false);
  115. path.remove();
  116. },
  117. usageGlobal(meta, utils) {
  118. const resolved = resolve(meta);
  119. if (!resolved) return;
  120. let deps = resolved.desc.global;
  121. if (resolved.kind !== "global" && meta.object && meta.placement === "prototype") {
  122. const low = meta.object.toLowerCase();
  123. deps = deps.filter(m => m.includes(low) || _builtInDefinitions.CommonInstanceDependencies.has(m));
  124. }
  125. maybeInjectGlobal(deps, utils);
  126. },
  127. usagePure(meta, utils, path) {
  128. if (meta.kind === "in") {
  129. if (meta.key === "Symbol.iterator") {
  130. path.replaceWith(t.callExpression(utils.injectDefaultImport((0, _utils.coreJSPureHelper)("is-iterable", useBabelRuntime, ext), "isIterable"), [path.node.right]));
  131. }
  132. return;
  133. }
  134. if (path.parentPath.isUnaryExpression({
  135. operator: "delete"
  136. })) return;
  137. let isCall;
  138. if (meta.kind === "property") {
  139. // We can't compile destructuring.
  140. if (!path.isMemberExpression()) return;
  141. if (!path.isReferenced()) return;
  142. isCall = path.parentPath.isCallExpression({
  143. callee: path.node
  144. });
  145. if (meta.key === "Symbol.iterator") {
  146. if (!shouldInjectPolyfill("es.symbol.iterator")) return;
  147. if (isCall) {
  148. if (path.parent.arguments.length === 0) {
  149. path.parentPath.replaceWith(t.callExpression(utils.injectDefaultImport((0, _utils.coreJSPureHelper)("get-iterator", useBabelRuntime, ext), "getIterator"), [path.node.object]));
  150. path.skip();
  151. } else {
  152. (0, _utils.callMethod)(path, utils.injectDefaultImport((0, _utils.coreJSPureHelper)("get-iterator-method", useBabelRuntime, ext), "getIteratorMethod"));
  153. }
  154. } else {
  155. path.replaceWith(t.callExpression(utils.injectDefaultImport((0, _utils.coreJSPureHelper)("get-iterator-method", useBabelRuntime, ext), "getIteratorMethod"), [path.node.object]));
  156. }
  157. return;
  158. }
  159. }
  160. let resolved = resolve(meta);
  161. if (!resolved) return;
  162. if (useBabelRuntime && resolved.desc.pure && resolved.desc.pure.slice(-6) === "/index") {
  163. // Remove /index, since it doesn't exist in @babel/runtime-corejs3s
  164. resolved = _extends({}, resolved, {
  165. desc: _extends({}, resolved.desc, {
  166. pure: resolved.desc.pure.slice(0, -6)
  167. })
  168. });
  169. }
  170. if (resolved.kind === "global") {
  171. const id = maybeInjectPure(resolved.desc, resolved.name, utils);
  172. if (id) path.replaceWith(id);
  173. } else if (resolved.kind === "static") {
  174. const id = maybeInjectPure(resolved.desc, resolved.name, utils, // $FlowIgnore
  175. meta.object);
  176. if (id) path.replaceWith(id);
  177. } else if (resolved.kind === "instance") {
  178. const id = maybeInjectPure(resolved.desc, `${resolved.name}InstanceProperty`, utils, // $FlowIgnore
  179. meta.object);
  180. if (!id) return;
  181. if (isCall) {
  182. (0, _utils.callMethod)(path, id);
  183. } else {
  184. path.replaceWith(t.callExpression(id, [path.node.object]));
  185. }
  186. }
  187. },
  188. visitor: method === "usage-global" && {
  189. // import("foo")
  190. CallExpression(path) {
  191. if (path.get("callee").isImport()) {
  192. const utils = getUtils(path);
  193. if (isWebpack) {
  194. // Webpack uses Promise.all to handle dynamic import.
  195. maybeInjectGlobal(_builtInDefinitions.PromiseDependenciesWithIterators, utils);
  196. } else {
  197. maybeInjectGlobal(_builtInDefinitions.PromiseDependencies, utils);
  198. }
  199. }
  200. },
  201. // (async function () { }).finally(...)
  202. Function(path) {
  203. if (path.node.async) {
  204. maybeInjectGlobal(_builtInDefinitions.PromiseDependencies, getUtils(path));
  205. }
  206. },
  207. // for-of, [a, b] = c
  208. "ForOfStatement|ArrayPattern"(path) {
  209. maybeInjectGlobal(_builtInDefinitions.CommonIterators, getUtils(path));
  210. },
  211. // [...spread]
  212. SpreadElement(path) {
  213. if (!path.parentPath.isObjectExpression()) {
  214. maybeInjectGlobal(_builtInDefinitions.CommonIterators, getUtils(path));
  215. }
  216. },
  217. // yield*
  218. YieldExpression(path) {
  219. if (path.node.delegate) {
  220. maybeInjectGlobal(_builtInDefinitions.CommonIterators, getUtils(path));
  221. }
  222. }
  223. }
  224. };
  225. });
  226. exports.default = _default;