ModuleDependencyTemplateAsId.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  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. /** @typedef {import("../Module")} Module */
  11. class ModuleDependencyTemplateAsId extends ModuleDependency.Template {
  12. /**
  13. * @param {Dependency} dependency the dependency for which the template should be applied
  14. * @param {ReplaceSource} source the current replace source which can be modified
  15. * @param {DependencyTemplateContext} templateContext the context object
  16. * @returns {void}
  17. */
  18. apply(dependency, source, { runtimeTemplate, moduleGraph, chunkGraph }) {
  19. const dep = /** @type {ModuleDependency} */ (dependency);
  20. if (!dep.range) return;
  21. const content = runtimeTemplate.moduleId({
  22. module: /** @type {Module} */ (moduleGraph.getModule(dep)),
  23. chunkGraph,
  24. request: dep.request,
  25. weak: dep.weak
  26. });
  27. source.replace(dep.range[0], dep.range[1] - 1, content);
  28. }
  29. }
  30. module.exports = ModuleDependencyTemplateAsId;