WebpackIsIncludedDependency.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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("../javascript/JavascriptParser").Range} Range */
  16. /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
  17. class WebpackIsIncludedDependency extends ModuleDependency {
  18. /**
  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 {(string[] | ReferencedExport)[]} 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. * @param {Dependency} dependency the dependency for which the template should be applied
  50. * @param {ReplaceSource} source the current replace source which can be modified
  51. * @param {DependencyTemplateContext} templateContext the context object
  52. * @returns {void}
  53. */
  54. apply(dependency, source, { runtimeTemplate, chunkGraph, moduleGraph }) {
  55. const dep = /** @type {WebpackIsIncludedDependency} */ (dependency);
  56. const connection = moduleGraph.getConnection(dep);
  57. const included = connection
  58. ? chunkGraph.getNumberOfModuleChunks(connection.module) > 0
  59. : false;
  60. const comment = runtimeTemplate.outputOptions.pathinfo
  61. ? Template.toComment(
  62. `__webpack_is_included__ ${runtimeTemplate.requestShortener.shorten(
  63. dep.request
  64. )}`
  65. )
  66. : "";
  67. source.replace(
  68. dep.range[0],
  69. dep.range[1] - 1,
  70. `${comment}${JSON.stringify(included)}`
  71. );
  72. }
  73. };
  74. module.exports = WebpackIsIncludedDependency;