UseFilePlugin.js 1.3 KB

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