WebpackIsIncludedDependency.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Ivan Kopeykin @vankop
  4. */
  5. "use strict";
  6. const Dependency = require("../Dependency");
  7. const Template = require("../Template");
  8. const makeSerializable = require("../util/makeSerializable");
  9. const ModuleDependency = require("./ModuleDependency");
  10. /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
  11. /** @typedef {import("../Compilation")} Compilation */
  12. /** @typedef {import("../Dependency").ReferencedExport} ReferencedExport */
  13. /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
  14. /** @typedef {import("../ModuleGraph")} ModuleGraph */
  15. /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
  16. class WebpackIsIncludedDependency extends ModuleDependency {
  17. constructor(request, range) {
  18. super(request);
  19. this.weak = true;
  20. this.range = range;
  21. }
  22. /**
  23. * Returns list of exports referenced by this dependency
  24. * @param {ModuleGraph} moduleGraph module graph
  25. * @param {RuntimeSpec} runtime the runtime for which the module is analysed
  26. * @returns {(string[] | ReferencedExport)[]} referenced exports
  27. */
  28. getReferencedExports(moduleGraph, runtime) {
  29. // This doesn't use any export
  30. return Dependency.NO_EXPORTS_REFERENCED;
  31. }
  32. get type() {
  33. return "__webpack_is_included__";
  34. }
  35. }
  36. makeSerializable(
  37. WebpackIsIncludedDependency,
  38. "webpack/lib/dependencies/WebpackIsIncludedDependency"
  39. );
  40. WebpackIsIncludedDependency.Template = class WebpackIsIncludedDependencyTemplate extends (
  41. ModuleDependency.Template
  42. ) {
  43. /**
  44. * @param {Dependency} dependency the dependency for which the template should be applied
  45. * @param {ReplaceSource} source the current replace source which can be modified
  46. * @param {DependencyTemplateContext} templateContext the context object
  47. * @returns {void}
  48. */
  49. apply(dependency, source, { runtimeTemplate, chunkGraph, moduleGraph }) {
  50. const dep = /** @type {WebpackIsIncludedDependency} */ (dependency);
  51. const connection = moduleGraph.getConnection(dep);
  52. const included = connection
  53. ? chunkGraph.getNumberOfModuleChunks(connection.module) > 0
  54. : false;
  55. const comment = runtimeTemplate.outputOptions.pathinfo
  56. ? Template.toComment(
  57. `__webpack_is_included__ ${runtimeTemplate.requestShortener.shorten(
  58. dep.request
  59. )}`
  60. )
  61. : "";
  62. source.replace(
  63. dep.range[0],
  64. dep.range[1] - 1,
  65. `${comment}${JSON.stringify(included)}`
  66. );
  67. }
  68. };
  69. module.exports = WebpackIsIncludedDependency;