FileExistsPlugin.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. module.exports = class FileExistsPlugin {
  7. constructor(source, target) {
  8. this.source = source;
  9. this.target = target;
  10. }
  11. apply(resolver) {
  12. const target = resolver.ensureHook(this.target);
  13. const fs = resolver.fileSystem;
  14. resolver
  15. .getHook(this.source)
  16. .tapAsync("FileExistsPlugin", (request, resolveContext, callback) => {
  17. const file = request.path;
  18. fs.stat(file, (err, stat) => {
  19. if (err || !stat) {
  20. if (resolveContext.missing) resolveContext.missing.add(file);
  21. if (resolveContext.log) resolveContext.log(file + " doesn't exist");
  22. return callback();
  23. }
  24. if (!stat.isFile()) {
  25. if (resolveContext.missing) resolveContext.missing.add(file);
  26. if (resolveContext.log) resolveContext.log(file + " is not a file");
  27. return callback();
  28. }
  29. resolver.doResolve(
  30. target,
  31. request,
  32. "existing file: " + file,
  33. resolveContext,
  34. callback
  35. );
  36. });
  37. });
  38. }
  39. };