You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ModulesInHierachicDirectoriesPlugin.js 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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.getHook(this.source).tapAsync("ModulesInHierachicDirectoriesPlugin", (request, resolveContext, callback) => {
  17. const fs = resolver.fileSystem;
  18. const addrs = getPaths(request.path).paths.map(p => {
  19. return this.directories.map(d => resolver.join(p, d));
  20. }).reduce((array, p) => {
  21. array.push.apply(array, p);
  22. return array;
  23. }, []);
  24. forEachBail(addrs, (addr, callback) => {
  25. fs.stat(addr, (err, stat) => {
  26. if(!err && stat && stat.isDirectory()) {
  27. const obj = Object.assign({}, request, {
  28. path: addr,
  29. request: "./" + request.request
  30. });
  31. const message = "looking for modules in " + addr;
  32. return resolver.doResolve(target, obj, message, resolveContext, callback);
  33. }
  34. if(resolveContext.log) resolveContext.log(addr + " doesn't exist or is not a directory");
  35. if(resolveContext.missing) resolveContext.missing.add(addr);
  36. return callback();
  37. });
  38. }, callback);
  39. });
  40. }
  41. };