utils.js 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.BABEL_RUNTIME = void 0;
  4. exports.callMethod = callMethod;
  5. exports.coreJSModule = coreJSModule;
  6. exports.coreJSPureHelper = coreJSPureHelper;
  7. exports.extractOptionalCheck = extractOptionalCheck;
  8. exports.isCoreJSSource = isCoreJSSource;
  9. exports.maybeMemoizeContext = maybeMemoizeContext;
  10. var _babel = _interopRequireWildcard(require("@babel/core"));
  11. var _entries = _interopRequireDefault(require("../core-js-compat/entries.js"));
  12. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  13. 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); }
  14. 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; }
  15. const {
  16. types: t
  17. } = _babel.default || _babel;
  18. const BABEL_RUNTIME = "@babel/runtime-corejs3";
  19. exports.BABEL_RUNTIME = BABEL_RUNTIME;
  20. function callMethod(path, id, optionalCall, wrapCallee) {
  21. const [context1, context2] = maybeMemoizeContext(path.node, path.scope);
  22. let callee = t.callExpression(id, [context1]);
  23. if (wrapCallee) callee = wrapCallee(callee);
  24. const call = t.identifier("call");
  25. path.replaceWith(optionalCall ? t.optionalMemberExpression(callee, call, false, true) : t.memberExpression(callee, call));
  26. path.parentPath.unshiftContainer("arguments", context2);
  27. }
  28. function maybeMemoizeContext(node, scope) {
  29. const {
  30. object
  31. } = node;
  32. let context1, context2;
  33. if (t.isIdentifier(object)) {
  34. context2 = object;
  35. context1 = t.cloneNode(object);
  36. } else {
  37. context2 = scope.generateDeclaredUidIdentifier("context");
  38. context1 = t.assignmentExpression("=", t.cloneNode(context2), object);
  39. }
  40. return [context1, context2];
  41. }
  42. function extractOptionalCheck(scope, node) {
  43. let optionalNode = node;
  44. while (!optionalNode.optional && t.isOptionalMemberExpression(optionalNode.object)) {
  45. optionalNode = optionalNode.object;
  46. }
  47. optionalNode.optional = false;
  48. const ctx = scope.generateDeclaredUidIdentifier("context");
  49. const assign = t.assignmentExpression("=", ctx, optionalNode.object);
  50. optionalNode.object = t.cloneNode(ctx);
  51. return ifNotNullish => t.conditionalExpression(t.binaryExpression("==", assign, t.nullLiteral()), t.unaryExpression("void", t.numericLiteral(0)), ifNotNullish);
  52. }
  53. function isCoreJSSource(source) {
  54. if (typeof source === "string") {
  55. source = source.replace(/\\/g, "/").replace(/(\/(index)?)?(\.js)?$/i, "").toLowerCase();
  56. }
  57. return Object.prototype.hasOwnProperty.call(_entries.default, source) && _entries.default[source];
  58. }
  59. function coreJSModule(name) {
  60. return `core-js/modules/${name}.js`;
  61. }
  62. function coreJSPureHelper(name, useBabelRuntime, ext) {
  63. return useBabelRuntime ? `${BABEL_RUNTIME}/core-js/${name}${ext}` : `core-js-pure/features/${name}.js`;
  64. }