CloneBasenamePlugin.js 1003 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. module.exports = class CloneBasenamePlugin {
  9. constructor(source, target) {
  10. this.source = source;
  11. this.target = target;
  12. }
  13. /**
  14. * @param {Resolver} resolver the resolver
  15. * @returns {void}
  16. */
  17. apply(resolver) {
  18. const target = resolver.ensureHook(this.target);
  19. resolver
  20. .getHook(this.source)
  21. .tapAsync("CloneBasenamePlugin", (request, resolveContext, callback) => {
  22. const filename = basename(request.path);
  23. const filePath = resolver.join(request.path, filename);
  24. const obj = {
  25. ...request,
  26. path: filePath,
  27. relativePath:
  28. request.relativePath &&
  29. resolver.join(request.relativePath, filename)
  30. };
  31. resolver.doResolve(
  32. target,
  33. obj,
  34. "using path: " + filePath,
  35. resolveContext,
  36. callback
  37. );
  38. });
  39. }
  40. };