index.js 12 KB

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