utils.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.getType = getType;
  6. exports.intersection = intersection;
  7. exports.filterStageFromList = filterStageFromList;
  8. exports.getImportSource = getImportSource;
  9. exports.getRequireSource = getRequireSource;
  10. exports.isPolyfillSource = isPolyfillSource;
  11. exports.getModulePath = getModulePath;
  12. exports.createImport = createImport;
  13. exports.isNamespaced = isNamespaced;
  14. exports.has = void 0;
  15. var t = _interopRequireWildcard(require("@babel/types"));
  16. var _helperModuleImports = require("@babel/helper-module-imports");
  17. function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
  18. 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; }
  19. const has = Object.hasOwnProperty.call.bind(Object.hasOwnProperty);
  20. exports.has = has;
  21. function getType(target) {
  22. return Object.prototype.toString.call(target).slice(8, -1).toLowerCase();
  23. }
  24. function intersection(first, second, third) {
  25. const result = new Set();
  26. for (const el of first) {
  27. if (second.has(el) && third.has(el)) result.add(el);
  28. }
  29. return result;
  30. }
  31. function filterStageFromList(list, stageList) {
  32. return Object.keys(list).reduce((result, item) => {
  33. if (!stageList[item]) {
  34. result[item] = list[item];
  35. }
  36. return result;
  37. }, {});
  38. }
  39. function getImportSource({
  40. node
  41. }) {
  42. if (node.specifiers.length === 0) return node.source.value;
  43. }
  44. function getRequireSource({
  45. node
  46. }) {
  47. if (!t.isExpressionStatement(node)) return;
  48. const {
  49. expression
  50. } = node;
  51. const isRequire = t.isCallExpression(expression) && t.isIdentifier(expression.callee) && expression.callee.name === "require" && expression.arguments.length === 1 && t.isStringLiteral(expression.arguments[0]);
  52. if (isRequire) return expression.arguments[0].value;
  53. }
  54. function isPolyfillSource(source) {
  55. return source === "@babel/polyfill" || source === "core-js";
  56. }
  57. const modulePathMap = {
  58. "regenerator-runtime": "regenerator-runtime/runtime"
  59. };
  60. function getModulePath(mod) {
  61. return modulePathMap[mod] || `core-js/modules/${mod}`;
  62. }
  63. function createImport(path, mod) {
  64. return (0, _helperModuleImports.addSideEffect)(path, getModulePath(mod));
  65. }
  66. function isNamespaced(path) {
  67. if (!path.node) return false;
  68. const binding = path.scope.getBinding(path.node.name);
  69. if (!binding) return false;
  70. return binding.path.isImportNamespaceSpecifier();
  71. }