index.js 5.6 KB

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