index.js 12 KB

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