You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.1 KiB

4 years ago
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. class ContextDependencyTemplateAsId {
  7. apply(dep, source, runtime) {
  8. const moduleExports = runtime.moduleExports({
  9. module: dep.module,
  10. request: dep.request
  11. });
  12. if (dep.module) {
  13. if (dep.valueRange) {
  14. if (Array.isArray(dep.replaces)) {
  15. for (let i = 0; i < dep.replaces.length; i++) {
  16. const rep = dep.replaces[i];
  17. source.replace(rep.range[0], rep.range[1] - 1, rep.value);
  18. }
  19. }
  20. source.replace(dep.valueRange[1], dep.range[1] - 1, ")");
  21. // TODO webpack 5 remove `prepend` it's no longer used
  22. source.replace(
  23. dep.range[0],
  24. dep.valueRange[0] - 1,
  25. `${moduleExports}.resolve(${
  26. typeof dep.prepend === "string" ? JSON.stringify(dep.prepend) : ""
  27. }`
  28. );
  29. } else {
  30. source.replace(
  31. dep.range[0],
  32. dep.range[1] - 1,
  33. `${moduleExports}.resolve`
  34. );
  35. }
  36. } else {
  37. source.replace(dep.range[0], dep.range[1] - 1, moduleExports);
  38. }
  39. }
  40. }
  41. module.exports = ContextDependencyTemplateAsId;