AppendPlugin.js 1.2 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").ResolveRequest} ResolveRequest */
  8. /** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */
  9. module.exports = class AppendPlugin {
  10. /**
  11. * @param {string | ResolveStepHook} source source
  12. * @param {string} appending appending
  13. * @param {string | ResolveStepHook} target target
  14. */
  15. constructor(source, appending, target) {
  16. this.source = source;
  17. this.appending = appending;
  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("AppendPlugin", (request, resolveContext, callback) => {
  29. /** @type {ResolveRequest} */
  30. const obj = {
  31. ...request,
  32. path: request.path + this.appending,
  33. relativePath:
  34. request.relativePath && request.relativePath + this.appending
  35. };
  36. resolver.doResolve(
  37. target,
  38. obj,
  39. this.appending,
  40. resolveContext,
  41. callback
  42. );
  43. });
  44. }
  45. };