JoinRequestPlugin.js 1.2 KB

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