index.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = void 0;
  4. var _corejs2BuiltIns = _interopRequireDefault(require("@babel/compat-data/corejs2-built-ins"));
  5. var _builtInDefinitions = require("./built-in-definitions");
  6. var _addPlatformSpecificPolyfills = _interopRequireDefault(require("./add-platform-specific-polyfills"));
  7. var _helpers = require("./helpers");
  8. var _helperDefinePolyfillProvider = _interopRequireDefault(require("@babel/helper-define-polyfill-provider"));
  9. var _babel = _interopRequireWildcard(require("@babel/core"));
  10. 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); }
  11. 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; }
  12. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  13. const {
  14. types: t
  15. } = _babel.default || _babel;
  16. const presetEnvCompat = "#__secret_key__@babel/preset-env__compatibility";
  17. const runtimeCompat = "#__secret_key__@babel/runtime__compatibility";
  18. const has = Function.call.bind(Object.hasOwnProperty);
  19. var _default = (0, _helperDefinePolyfillProvider.default)(function (api, {
  20. [presetEnvCompat]: {
  21. entryInjectRegenerator
  22. } = {
  23. entryInjectRegenerator: false
  24. },
  25. [runtimeCompat]: {
  26. useBabelRuntime,
  27. runtimeVersion,
  28. ext = ".js"
  29. } = {
  30. useBabelRuntime: "",
  31. runtimeVersion: ""
  32. }
  33. }) {
  34. const resolve = api.createMetaResolver({
  35. global: _builtInDefinitions.BuiltIns,
  36. static: _builtInDefinitions.StaticProperties,
  37. instance: _builtInDefinitions.InstanceProperties
  38. });
  39. const {
  40. debug,
  41. shouldInjectPolyfill,
  42. method
  43. } = api;
  44. const polyfills = (0, _addPlatformSpecificPolyfills.default)(api.targets, method, _corejs2BuiltIns.default);
  45. const coreJSBase = useBabelRuntime ? `${useBabelRuntime}/core-js` : method === "usage-pure" ? "core-js/library/fn" : "core-js/modules";
  46. function inject(name, utils) {
  47. if (typeof name === "string") {
  48. // Some polyfills aren't always available, for example
  49. // web.dom.iterable when targeting node
  50. if (has(polyfills, name) && shouldInjectPolyfill(name)) {
  51. debug(name);
  52. utils.injectGlobalImport(`${coreJSBase}/${name}.js`);
  53. }
  54. return;
  55. }
  56. name.forEach(name => inject(name, utils));
  57. }
  58. function maybeInjectPure(desc, hint, utils) {
  59. const {
  60. pure,
  61. meta,
  62. name
  63. } = desc;
  64. if (!pure || !shouldInjectPolyfill(name)) return;
  65. if (runtimeVersion && meta && meta.minRuntimeVersion && !(0, _helpers.hasMinVersion)(meta && meta.minRuntimeVersion, runtimeVersion)) {
  66. return;
  67. }
  68. return utils.injectDefaultImport(`${coreJSBase}/${pure}${ext}`, hint);
  69. }
  70. return {
  71. name: "corejs2",
  72. polyfills,
  73. entryGlobal(meta, utils, path) {
  74. if (meta.kind === "import" && meta.source === "core-js") {
  75. debug(null);
  76. inject(Object.keys(polyfills), utils);
  77. if (entryInjectRegenerator) {
  78. utils.injectGlobalImport("regenerator-runtime/runtime.js");
  79. }
  80. path.remove();
  81. }
  82. },
  83. usageGlobal(meta, utils) {
  84. const resolved = resolve(meta);
  85. if (!resolved) return;
  86. let deps = resolved.desc.global;
  87. if (resolved.kind !== "global" && "object" in meta && meta.object && meta.placement === "prototype") {
  88. const low = meta.object.toLowerCase();
  89. deps = deps.filter(m => m.includes(low));
  90. }
  91. inject(deps, utils);
  92. },
  93. usagePure(meta, utils, path) {
  94. if (meta.kind === "in") {
  95. if (meta.key === "Symbol.iterator") {
  96. path.replaceWith(t.callExpression(utils.injectDefaultImport(`${coreJSBase}/is-iterable${ext}`, "isIterable"), [path.node.right] // meta.kind === "in" narrows this
  97. ));
  98. }
  99. return;
  100. }
  101. if (path.parentPath.isUnaryExpression({
  102. operator: "delete"
  103. })) return;
  104. if (meta.kind === "property") {
  105. // We can't compile destructuring.
  106. if (!path.isMemberExpression()) return;
  107. if (!path.isReferenced()) return;
  108. if (meta.key === "Symbol.iterator" && shouldInjectPolyfill("es6.symbol") && path.parentPath.isCallExpression({
  109. callee: path.node
  110. }) && path.parentPath.node.arguments.length === 0) {
  111. path.parentPath.replaceWith(t.callExpression(utils.injectDefaultImport(`${coreJSBase}/get-iterator${ext}`, "getIterator"), [path.node.object]));
  112. path.skip();
  113. return;
  114. }
  115. }
  116. const resolved = resolve(meta);
  117. if (!resolved) return;
  118. const id = maybeInjectPure(resolved.desc, resolved.name, utils);
  119. if (id) path.replaceWith(id);
  120. },
  121. visitor: method === "usage-global" && {
  122. // yield*
  123. YieldExpression(path) {
  124. if (path.node.delegate) {
  125. inject("web.dom.iterable", api.getUtils(path));
  126. }
  127. },
  128. // for-of, [a, b] = c
  129. "ForOfStatement|ArrayPattern"(path) {
  130. _builtInDefinitions.CommonIterators.forEach(name => inject(name, api.getUtils(path)));
  131. }
  132. }
  133. };
  134. });
  135. exports.default = _default;