index.js 6.1 KB

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