ModulesInHierachicDirectoriesPlugin.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const forEachBail = require("./forEachBail");
  7. const getPaths = require("./getPaths");
  8. module.exports = class ModulesInHierachicDirectoriesPlugin {
  9. constructor(source, directories, target) {
  10. this.source = source;
  11. this.directories = [].concat(directories);
  12. this.target = target;
  13. }
  14. apply(resolver) {
  15. const target = resolver.ensureHook(this.target);
  16. resolver
  17. .getHook(this.source)
  18. .tapAsync(
  19. "ModulesInHierachicDirectoriesPlugin",
  20. (request, resolveContext, callback) => {
  21. const fs = resolver.fileSystem;
  22. const addrs = getPaths(request.path)
  23. .paths.map(p => {
  24. return this.directories.map(d => resolver.join(p, d));
  25. })
  26. .reduce((array, p) => {
  27. array.push.apply(array, p);
  28. return array;
  29. }, []);
  30. forEachBail(
  31. addrs,
  32. (addr, callback) => {
  33. fs.stat(addr, (err, stat) => {
  34. if (!err && stat && stat.isDirectory()) {
  35. const obj = Object.assign({}, request, {
  36. path: addr,
  37. request: "./" + request.request
  38. });
  39. const message = "looking for modules in " + addr;
  40. return resolver.doResolve(
  41. target,
  42. obj,
  43. message,
  44. resolveContext,
  45. callback
  46. );
  47. }
  48. if (resolveContext.log)
  49. resolveContext.log(
  50. addr + " doesn't exist or is not a directory"
  51. );
  52. if (resolveContext.missing) resolveContext.missing.add(addr);
  53. return callback();
  54. });
  55. },
  56. callback
  57. );
  58. }
  59. );
  60. }
  61. };