JoinRequestPartPlugin.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. const namespaceStartCharCode = "@".charCodeAt(0);
  9. module.exports = class JoinRequestPartPlugin {
  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(
  27. "JoinRequestPartPlugin",
  28. (request, resolveContext, callback) => {
  29. const req = request.request || "";
  30. let i = req.indexOf("/", 3);
  31. if (i >= 0 && req.charCodeAt(2) === namespaceStartCharCode) {
  32. i = req.indexOf("/", i + 1);
  33. }
  34. let moduleName, remainingRequest, fullySpecified;
  35. if (i < 0) {
  36. moduleName = req;
  37. remainingRequest = ".";
  38. fullySpecified = false;
  39. } else {
  40. moduleName = req.slice(0, i);
  41. remainingRequest = "." + req.slice(i);
  42. fullySpecified = request.fullySpecified;
  43. }
  44. const obj = {
  45. ...request,
  46. path: resolver.join(request.path, moduleName),
  47. relativePath:
  48. request.relativePath &&
  49. resolver.join(request.relativePath, moduleName),
  50. request: remainingRequest,
  51. fullySpecified
  52. };
  53. resolver.doResolve(target, obj, null, resolveContext, callback);
  54. }
  55. );
  56. }
  57. };