FileExistsPlugin.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 FileExistsPlugin {
  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. const fs = resolver.fileSystem;
  24. resolver
  25. .getHook(this.source)
  26. .tapAsync("FileExistsPlugin", (request, resolveContext, callback) => {
  27. const file = request.path;
  28. if (!file) return callback();
  29. fs.stat(file, (err, stat) => {
  30. if (err || !stat) {
  31. if (resolveContext.missingDependencies)
  32. resolveContext.missingDependencies.add(file);
  33. if (resolveContext.log) resolveContext.log(file + " doesn't exist");
  34. return callback();
  35. }
  36. if (!stat.isFile()) {
  37. if (resolveContext.missingDependencies)
  38. resolveContext.missingDependencies.add(file);
  39. if (resolveContext.log) resolveContext.log(file + " is not a file");
  40. return callback();
  41. }
  42. if (resolveContext.fileDependencies)
  43. resolveContext.fileDependencies.add(file);
  44. resolver.doResolve(
  45. target,
  46. request,
  47. "existing file: " + file,
  48. resolveContext,
  49. callback
  50. );
  51. });
  52. });
  53. }
  54. };