CloneBasenamePlugin.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const basename = require("./getPaths").basename;
  7. /** @typedef {import("./Resolver")} Resolver */
  8. /** @typedef {import("./Resolver").ResolveRequest} ResolveRequest */
  9. /** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */
  10. module.exports = class CloneBasenamePlugin {
  11. /**
  12. * @param {string | ResolveStepHook} source source
  13. * @param {string | ResolveStepHook} target target
  14. */
  15. constructor(source, target) {
  16. this.source = source;
  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("CloneBasenamePlugin", (request, resolveContext, callback) => {
  28. const requestPath = /** @type {string} */ (request.path);
  29. const filename = /** @type {string} */ (basename(requestPath));
  30. const filePath = resolver.join(requestPath, filename);
  31. /** @type {ResolveRequest} */
  32. const obj = {
  33. ...request,
  34. path: filePath,
  35. relativePath:
  36. request.relativePath &&
  37. resolver.join(request.relativePath, filename)
  38. };
  39. resolver.doResolve(
  40. target,
  41. obj,
  42. "using path: " + filePath,
  43. resolveContext,
  44. callback
  45. );
  46. });
  47. }
  48. };