UseFilePlugin.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 UseFilePlugin {
  9. /**
  10. * @param {string | ResolveStepHook} source source
  11. * @param {string} filename filename
  12. * @param {string | ResolveStepHook} target target
  13. */
  14. constructor(source, filename, target) {
  15. this.source = source;
  16. this.filename = filename;
  17. this.target = target;
  18. }
  19. /**
  20. * @param {Resolver} resolver the resolver
  21. * @returns {void}
  22. */
  23. apply(resolver) {
  24. const target = resolver.ensureHook(this.target);
  25. resolver
  26. .getHook(this.source)
  27. .tapAsync("UseFilePlugin", (request, resolveContext, callback) => {
  28. const filePath = resolver.join(request.path, this.filename);
  29. const obj = {
  30. ...request,
  31. path: filePath,
  32. relativePath:
  33. request.relativePath &&
  34. resolver.join(request.relativePath, this.filename)
  35. };
  36. resolver.doResolve(
  37. target,
  38. obj,
  39. "using path: " + filePath,
  40. resolveContext,
  41. callback
  42. );
  43. });
  44. }
  45. };