index.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. const modulesSet = new Set(modules);
  115. const filteredModules = modules.filter(module => {
  116. if (!module.startsWith("esnext.")) return true;
  117. const stable = module.replace("esnext.", "es.");
  118. if (modulesSet.has(stable) && shouldInjectPolyfill(stable)) {
  119. return false;
  120. }
  121. return true;
  122. });
  123. maybeInjectGlobal(filteredModules, utils, false);
  124. path.remove();
  125. },
  126. usageGlobal(meta, utils) {
  127. const resolved = resolve(meta);
  128. if (!resolved) return;
  129. let deps = resolved.desc.global;
  130. if (resolved.kind !== "global" && meta.object && meta.placement === "prototype") {
  131. const low = meta.object.toLowerCase();
  132. deps = deps.filter(m => m.includes(low) || _builtInDefinitions.CommonInstanceDependencies.has(m));
  133. }
  134. maybeInjectGlobal(deps, utils);
  135. },
  136. usagePure(meta, utils, path) {
  137. if (meta.kind === "in") {
  138. if (meta.key === "Symbol.iterator") {
  139. path.replaceWith(t.callExpression(utils.injectDefaultImport((0, _utils.coreJSPureHelper)("is-iterable", useBabelRuntime, ext), "isIterable"), [path.node.right]));
  140. }
  141. return;
  142. }
  143. if (path.parentPath.isUnaryExpression({
  144. operator: "delete"
  145. })) return;
  146. let isCall;
  147. if (meta.kind === "property") {
  148. // We can't compile destructuring.
  149. if (!path.isMemberExpression()) return;
  150. if (!path.isReferenced()) return;
  151. isCall = path.parentPath.isCallExpression({
  152. callee: path.node
  153. });
  154. if (meta.key === "Symbol.iterator") {
  155. if (!shouldInjectPolyfill("es.symbol.iterator")) return;
  156. if (isCall) {
  157. if (path.parent.arguments.length === 0) {
  158. path.parentPath.replaceWith(t.callExpression(utils.injectDefaultImport((0, _utils.coreJSPureHelper)("get-iterator", useBabelRuntime, ext), "getIterator"), [path.node.object]));
  159. path.skip();
  160. } else {
  161. (0, _utils.callMethod)(path, utils.injectDefaultImport((0, _utils.coreJSPureHelper)("get-iterator-method", useBabelRuntime, ext), "getIteratorMethod"));
  162. }
  163. } else {
  164. path.replaceWith(t.callExpression(utils.injectDefaultImport((0, _utils.coreJSPureHelper)("get-iterator-method", useBabelRuntime, ext), "getIteratorMethod"), [path.node.object]));
  165. }
  166. return;
  167. }
  168. }
  169. let resolved = resolve(meta);
  170. if (!resolved) return;
  171. if (useBabelRuntime && resolved.desc.pure && resolved.desc.pure.slice(-6) === "/index") {
  172. // Remove /index, since it doesn't exist in @babel/runtime-corejs3s
  173. resolved = _extends({}, resolved, {
  174. desc: _extends({}, resolved.desc, {
  175. pure: resolved.desc.pure.slice(0, -6)
  176. })
  177. });
  178. }
  179. if (resolved.kind === "global") {
  180. const id = maybeInjectPure(resolved.desc, resolved.name, utils);
  181. if (id) path.replaceWith(id);
  182. } else if (resolved.kind === "static") {
  183. const id = maybeInjectPure(resolved.desc, resolved.name, utils, // $FlowIgnore
  184. meta.object);
  185. if (id) path.replaceWith(id);
  186. } else if (resolved.kind === "instance") {
  187. const id = maybeInjectPure(resolved.desc, `${resolved.name}InstanceProperty`, utils, // $FlowIgnore
  188. meta.object);
  189. if (!id) return;
  190. if (isCall) {
  191. (0, _utils.callMethod)(path, id);
  192. } else {
  193. path.replaceWith(t.callExpression(id, [path.node.object]));
  194. }
  195. }
  196. },
  197. visitor: method === "usage-global" && {
  198. // import("foo")
  199. CallExpression(path) {
  200. if (path.get("callee").isImport()) {
  201. const utils = getUtils(path);
  202. if (isWebpack) {
  203. // Webpack uses Promise.all to handle dynamic import.
  204. maybeInjectGlobal(_builtInDefinitions.PromiseDependenciesWithIterators, utils);
  205. } else {
  206. maybeInjectGlobal(_builtInDefinitions.PromiseDependencies, utils);
  207. }
  208. }
  209. },
  210. // (async function () { }).finally(...)
  211. Function(path) {
  212. if (path.node.async) {
  213. maybeInjectGlobal(_builtInDefinitions.PromiseDependencies, getUtils(path));
  214. }
  215. },
  216. // for-of, [a, b] = c
  217. "ForOfStatement|ArrayPattern"(path) {
  218. maybeInjectGlobal(_builtInDefinitions.CommonIterators, getUtils(path));
  219. },
  220. // [...spread]
  221. SpreadElement(path) {
  222. if (!path.parentPath.isObjectExpression()) {
  223. maybeInjectGlobal(_builtInDefinitions.CommonIterators, getUtils(path));
  224. }
  225. },
  226. // yield*
  227. YieldExpression(path) {
  228. if (path.node.delegate) {
  229. maybeInjectGlobal(_builtInDefinitions.CommonIterators, getUtils(path));
  230. }
  231. }
  232. }
  233. };
  234. });
  235. exports.default = _default;