ModuleDependencyTemplateAsRequireId.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const ModuleDependency = require("./ModuleDependency");
  7. /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
  8. /** @typedef {import("../Dependency")} Dependency */
  9. /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
  10. class ModuleDependencyTemplateAsRequireId extends ModuleDependency.Template {
  11. /**
  12. * @param {Dependency} dependency the dependency for which the template should be applied
  13. * @param {ReplaceSource} source the current replace source which can be modified
  14. * @param {DependencyTemplateContext} templateContext the context object
  15. * @returns {void}
  16. */
  17. apply(
  18. dependency,
  19. source,
  20. { runtimeTemplate, moduleGraph, chunkGraph, runtimeRequirements }
  21. ) {
  22. const dep = /** @type {ModuleDependency} */ (dependency);
  23. if (!dep.range) return;
  24. const content = runtimeTemplate.moduleExports({
  25. module: moduleGraph.getModule(dep),
  26. chunkGraph,
  27. request: dep.request,
  28. weak: dep.weak,
  29. runtimeRequirements
  30. });
  31. source.replace(dep.range[0], dep.range[1] - 1, content);
  32. }
  33. }
  34. module.exports = ModuleDependencyTemplateAsRequireId;