WebpackIsIncludedDependency.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. /** @import { ReplaceSource } from "webpack-sources" */
  11. /** @import { ReferencedExports } from "../Dependency" */
  12. /** @import { DependencyTemplateContext } from "../DependencyTemplate" */
  13. /** @import ModuleGraph from "../ModuleGraph" */
  14. /** @import { Range } from "../javascript/JavascriptParser" */
  15. /** @import { RuntimeSpec } from "../util/runtime" */
  16. class WebpackIsIncludedDependency extends ModuleDependency {
  17. /**
  18. * Creates an instance of WebpackIsIncludedDependency.
  19. * @param {string} request the request string
  20. * @param {Range} range location in source code
  21. */
  22. constructor(request, range) {
  23. super(request);
  24. /** @type {boolean} */
  25. this.weak = true;
  26. this.range = range;
  27. }
  28. /**
  29. * Returns list of exports referenced by this dependency
  30. * @param {ModuleGraph} moduleGraph module graph
  31. * @param {RuntimeSpec} runtime the runtime for which the module is analysed
  32. * @returns {ReferencedExports} referenced exports
  33. */
  34. getReferencedExports(moduleGraph, runtime) {
  35. // This doesn't use any export
  36. return Dependency.NO_EXPORTS_REFERENCED;
  37. }
  38. get type() {
  39. return "__webpack_is_included__";
  40. }
  41. }
  42. makeSerializable(
  43. WebpackIsIncludedDependency,
  44. "webpack/lib/dependencies/WebpackIsIncludedDependency"
  45. );
  46. WebpackIsIncludedDependency.Template = class WebpackIsIncludedDependencyTemplate extends (
  47. ModuleDependency.Template
  48. ) {
  49. /**
  50. * Applies the plugin by registering its hooks on the compiler.
  51. * @param {Dependency} dependency the dependency for which the template should be applied
  52. * @param {ReplaceSource} source the current replace source which can be modified
  53. * @param {DependencyTemplateContext} templateContext the context object
  54. * @returns {void}
  55. */
  56. apply(dependency, source, { runtimeTemplate, chunkGraph, moduleGraph }) {
  57. const dep = /** @type {WebpackIsIncludedDependency} */ (dependency);
  58. const connection = moduleGraph.getConnection(dep);
  59. const included = connection
  60. ? chunkGraph.getNumberOfModuleChunks(connection.module) > 0
  61. : false;
  62. const comment = runtimeTemplate.outputOptions.pathinfo
  63. ? Template.toComment(
  64. `__webpack_is_included__ ${runtimeTemplate.requestShortener.shorten(
  65. dep.request
  66. )}`
  67. )
  68. : "";
  69. source.replace(
  70. dep.range[0],
  71. dep.range[1] - 1,
  72. `${comment}${JSON.stringify(included)}`
  73. );
  74. }
  75. };
  76. module.exports = WebpackIsIncludedDependency;