DirectoryExistsPlugin.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. /** @typedef {import("./Resolver")} Resolver */
  7. /** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */
  8. module.exports = class DirectoryExistsPlugin {
  9. /**
  10. * @param {string | ResolveStepHook} source source
  11. * @param {string | ResolveStepHook} target target
  12. */
  13. constructor(source, target) {
  14. this.source = source;
  15. this.target = target;
  16. }
  17. /**
  18. * @param {Resolver} resolver the resolver
  19. * @returns {void}
  20. */
  21. apply(resolver) {
  22. const target = resolver.ensureHook(this.target);
  23. resolver
  24. .getHook(this.source)
  25. .tapAsync(
  26. "DirectoryExistsPlugin",
  27. (request, resolveContext, callback) => {
  28. const fs = resolver.fileSystem;
  29. const directory = request.path;
  30. if (!directory) return callback();
  31. fs.stat(directory, (err, stat) => {
  32. if (err || !stat) {
  33. if (resolveContext.missingDependencies)
  34. resolveContext.missingDependencies.add(directory);
  35. if (resolveContext.log)
  36. resolveContext.log(directory + " doesn't exist");
  37. return callback();
  38. }
  39. if (!stat.isDirectory()) {
  40. if (resolveContext.missingDependencies)
  41. resolveContext.missingDependencies.add(directory);
  42. if (resolveContext.log)
  43. resolveContext.log(directory + " is not a directory");
  44. return callback();
  45. }
  46. if (resolveContext.fileDependencies)
  47. resolveContext.fileDependencies.add(directory);
  48. resolver.doResolve(
  49. target,
  50. request,
  51. `existing directory ${directory}`,
  52. resolveContext,
  53. callback
  54. );
  55. });
  56. }
  57. );
  58. }
  59. };