ContextDependencyTemplateAsRequireCall.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. class ContextDependencyTemplateAsRequireCall {
  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}(${
  26. typeof dep.prepend === "string" ? JSON.stringify(dep.prepend) : ""
  27. }`
  28. );
  29. } else {
  30. source.replace(dep.range[0], dep.range[1] - 1, moduleExports);
  31. }
  32. } else {
  33. source.replace(dep.range[0], dep.range[1] - 1, moduleExports);
  34. }
  35. }
  36. }
  37. module.exports = ContextDependencyTemplateAsRequireCall;