index.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
  11. function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
  12. const {
  13. types: t
  14. } = _babel.default || _babel;
  15. const BABEL_RUNTIME = "@babel/runtime-corejs2";
  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 = exports.default = (0, _helperDefinePolyfillProvider.default)(function (api, {
  20. [presetEnvCompat]: {
  21. entryInjectRegenerator = false,
  22. noRuntimeName = false
  23. } = {},
  24. [runtimeCompat]: {
  25. useBabelRuntime = false,
  26. runtimeVersion = "",
  27. ext = ".js"
  28. } = {}
  29. }) {
  30. const resolve = api.createMetaResolver({
  31. global: _builtInDefinitions.BuiltIns,
  32. static: _builtInDefinitions.StaticProperties,
  33. instance: _builtInDefinitions.InstanceProperties
  34. });
  35. const {
  36. debug,
  37. shouldInjectPolyfill,
  38. method
  39. } = api;
  40. const polyfills = (0, _addPlatformSpecificPolyfills.default)(api.targets, method, _corejs2BuiltIns.default);
  41. const coreJSBase = useBabelRuntime ? `${BABEL_RUNTIME}/core-js` : method === "usage-pure" ? "core-js/library/fn" : "core-js/modules";
  42. function inject(name, utils) {
  43. if (typeof name === "string") {
  44. // Some polyfills aren't always available, for example
  45. // web.dom.iterable when targeting node
  46. if (has(polyfills, name) && shouldInjectPolyfill(name)) {
  47. debug(name);
  48. utils.injectGlobalImport(`${coreJSBase}/${name}.js`);
  49. }
  50. return;
  51. }
  52. name.forEach(name => inject(name, utils));
  53. }
  54. function maybeInjectPure(desc, hint, utils) {
  55. let {
  56. pure,
  57. meta,
  58. name
  59. } = desc;
  60. if (!pure || !shouldInjectPolyfill(name)) return;
  61. if (runtimeVersion && meta && meta.minRuntimeVersion && !(0, _helpers.hasMinVersion)(meta && meta.minRuntimeVersion, runtimeVersion)) {
  62. return;
  63. }
  64. // Unfortunately core-js and @babel/runtime-corejs2 don't have the same
  65. // directory structure, so we need to special case this.
  66. if (useBabelRuntime && pure === "symbol/index") pure = "symbol";
  67. return utils.injectDefaultImport(`${coreJSBase}/${pure}${ext}`, hint);
  68. }
  69. return {
  70. name: "corejs2",
  71. runtimeName: noRuntimeName ? null : BABEL_RUNTIME,
  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. });