index.js 11 KB

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