ModuleDependencyTemplateAsId.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  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 ModuleDependencyTemplateAsId 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(dependency, source, { runtimeTemplate, moduleGraph, chunkGraph }) {
  18. const dep = /** @type {ModuleDependency} */ (dependency);
  19. if (!dep.range) return;
  20. const content = runtimeTemplate.moduleId({
  21. module: moduleGraph.getModule(dep),
  22. chunkGraph,
  23. request: dep.request,
  24. weak: dep.weak
  25. });
  26. source.replace(dep.range[0], dep.range[1] - 1, content);
  27. }
  28. }
  29. module.exports = ModuleDependencyTemplateAsId;