WebpackIsIncludedDependency.js 2.7 KB

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