interopRequireWildcard.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import _typeof from "./typeof.js";
  2. function _getRequireWildcardCache(nodeInterop) {
  3. if (typeof WeakMap !== "function") return null;
  4. var cacheBabelInterop = new WeakMap();
  5. var cacheNodeInterop = new WeakMap();
  6. return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) {
  7. return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
  8. })(nodeInterop);
  9. }
  10. export default function _interopRequireWildcard(obj, nodeInterop) {
  11. if (!nodeInterop && obj && obj.__esModule) {
  12. return obj;
  13. }
  14. if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") {
  15. return {
  16. "default": obj
  17. };
  18. }
  19. var cache = _getRequireWildcardCache(nodeInterop);
  20. if (cache && cache.has(obj)) {
  21. return cache.get(obj);
  22. }
  23. var newObj = {};
  24. var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
  25. for (var key in obj) {
  26. if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
  27. var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
  28. if (desc && (desc.get || desc.set)) {
  29. Object.defineProperty(newObj, key, desc);
  30. } else {
  31. newObj[key] = obj[key];
  32. }
  33. }
  34. }
  35. newObj["default"] = obj;
  36. if (cache) {
  37. cache.set(obj, newObj);
  38. }
  39. return newObj;
  40. }