ChainResolver.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import runResolver from './utils/runResolver.js';
  2. var ChainingLogic;
  3. (function (ChainingLogic) {
  4. ChainingLogic[ChainingLogic["ALL"] = 0] = "ALL";
  5. ChainingLogic[ChainingLogic["FIRST_FOUND"] = 1] = "FIRST_FOUND";
  6. })(ChainingLogic || (ChainingLogic = {}));
  7. class ChainResolver {
  8. constructor(resolvers, options) {
  9. this.resolvers = resolvers;
  10. this.options = options;
  11. }
  12. resolveFirstOnly(file) {
  13. for (const resolver of this.resolvers) {
  14. const components = runResolver(resolver, file);
  15. if (components.length > 0) {
  16. return components;
  17. }
  18. }
  19. return [];
  20. }
  21. resolveAll(file) {
  22. const allComponents = new Set();
  23. for (const resolver of this.resolvers) {
  24. const components = runResolver(resolver, file);
  25. components.forEach((component) => {
  26. allComponents.add(component);
  27. });
  28. }
  29. return Array.from(allComponents);
  30. }
  31. resolve(file) {
  32. if (this.options.chainingLogic === ChainingLogic.FIRST_FOUND) {
  33. return this.resolveFirstOnly(file);
  34. }
  35. return this.resolveAll(file);
  36. }
  37. }
  38. ChainResolver.Logic = ChainingLogic;
  39. export default ChainResolver;