ModuleAppendPlugin.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. module.exports = class ModuleAppendPlugin {
  7. constructor(source, appending, target) {
  8. this.source = source;
  9. this.appending = appending;
  10. this.target = target;
  11. }
  12. apply(resolver) {
  13. const target = resolver.ensureHook(this.target);
  14. resolver
  15. .getHook(this.source)
  16. .tapAsync("ModuleAppendPlugin", (request, resolveContext, callback) => {
  17. const i = request.request.indexOf("/"),
  18. j = request.request.indexOf("\\");
  19. const p = i < 0 ? j : j < 0 ? i : i < j ? i : j;
  20. let moduleName, remainingRequest;
  21. if (p < 0) {
  22. moduleName = request.request;
  23. remainingRequest = "";
  24. } else {
  25. moduleName = request.request.substr(0, p);
  26. remainingRequest = request.request.substr(p);
  27. }
  28. if (moduleName === "." || moduleName === "..") return callback();
  29. const moduleFinalName = moduleName + this.appending;
  30. const obj = Object.assign({}, request, {
  31. request: moduleFinalName + remainingRequest
  32. });
  33. resolver.doResolve(
  34. target,
  35. obj,
  36. "module variation " + moduleFinalName,
  37. resolveContext,
  38. callback
  39. );
  40. });
  41. }
  42. };