ExportsInfoDependency.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { UsageState } = require("../ExportsInfo");
  7. const makeSerializable = require("../util/makeSerializable");
  8. const NullDependency = require("./NullDependency");
  9. /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
  10. /** @typedef {import("../ChunkGraph")} ChunkGraph */
  11. /** @typedef {import("../Dependency")} Dependency */
  12. /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
  13. /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
  14. /** @typedef {import("../Module")} Module */
  15. /** @typedef {import("../ModuleGraph")} ModuleGraph */
  16. /** @typedef {import("../javascript/JavascriptParser").Range} Range */
  17. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  18. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  19. /** @typedef {import("../util/Hash")} Hash */
  20. /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
  21. /**
  22. * @template T
  23. * @typedef {import("../util/SortableSet")<T>} SortableSet
  24. */
  25. /**
  26. * @param {ModuleGraph} moduleGraph the module graph
  27. * @param {Module} module the module
  28. * @param {string[] | null} _exportName name of the export if any
  29. * @param {string | null} property name of the requested property
  30. * @param {RuntimeSpec} runtime for which runtime
  31. * @returns {undefined | null | number | boolean | string[] | SortableSet<string>} value of the property
  32. */
  33. const getProperty = (moduleGraph, module, _exportName, property, runtime) => {
  34. if (!_exportName) {
  35. switch (property) {
  36. case "usedExports": {
  37. const usedExports = moduleGraph
  38. .getExportsInfo(module)
  39. .getUsedExports(runtime);
  40. if (
  41. typeof usedExports === "boolean" ||
  42. usedExports === undefined ||
  43. usedExports === null
  44. ) {
  45. return usedExports;
  46. }
  47. return Array.from(usedExports).sort();
  48. }
  49. }
  50. }
  51. const exportName = /** @type {string[]} */ (_exportName);
  52. switch (property) {
  53. case "canMangle": {
  54. const exportsInfo = moduleGraph.getExportsInfo(module);
  55. const exportInfo = exportsInfo.getReadOnlyExportInfoRecursive(exportName);
  56. if (exportInfo) return exportInfo.canMangle;
  57. return exportsInfo.otherExportsInfo.canMangle;
  58. }
  59. case "used":
  60. return (
  61. moduleGraph.getExportsInfo(module).getUsed(exportName, runtime) !==
  62. UsageState.Unused
  63. );
  64. case "useInfo": {
  65. const state = moduleGraph
  66. .getExportsInfo(module)
  67. .getUsed(exportName, runtime);
  68. switch (state) {
  69. case UsageState.Used:
  70. case UsageState.OnlyPropertiesUsed:
  71. return true;
  72. case UsageState.Unused:
  73. return false;
  74. case UsageState.NoInfo:
  75. return;
  76. case UsageState.Unknown:
  77. return null;
  78. default:
  79. throw new Error(`Unexpected UsageState ${state}`);
  80. }
  81. }
  82. case "provideInfo":
  83. return moduleGraph.getExportsInfo(module).isExportProvided(exportName);
  84. }
  85. };
  86. class ExportsInfoDependency extends NullDependency {
  87. /**
  88. * @param {Range} range range
  89. * @param {string[] | null} exportName export name
  90. * @param {string | null} property property
  91. */
  92. constructor(range, exportName, property) {
  93. super();
  94. this.range = range;
  95. this.exportName = exportName;
  96. this.property = property;
  97. }
  98. /**
  99. * @param {ObjectSerializerContext} context context
  100. */
  101. serialize(context) {
  102. const { write } = context;
  103. write(this.range);
  104. write(this.exportName);
  105. write(this.property);
  106. super.serialize(context);
  107. }
  108. /**
  109. * @param {ObjectDeserializerContext} context context
  110. * @returns {ExportsInfoDependency} ExportsInfoDependency
  111. */
  112. static deserialize(context) {
  113. const obj = new ExportsInfoDependency(
  114. context.read(),
  115. context.read(),
  116. context.read()
  117. );
  118. obj.deserialize(context);
  119. return obj;
  120. }
  121. }
  122. makeSerializable(
  123. ExportsInfoDependency,
  124. "webpack/lib/dependencies/ExportsInfoDependency"
  125. );
  126. ExportsInfoDependency.Template = class ExportsInfoDependencyTemplate extends (
  127. NullDependency.Template
  128. ) {
  129. /**
  130. * @param {Dependency} dependency the dependency for which the template should be applied
  131. * @param {ReplaceSource} source the current replace source which can be modified
  132. * @param {DependencyTemplateContext} templateContext the context object
  133. * @returns {void}
  134. */
  135. apply(dependency, source, { module, moduleGraph, runtime }) {
  136. const dep = /** @type {ExportsInfoDependency} */ (dependency);
  137. const value = getProperty(
  138. moduleGraph,
  139. module,
  140. dep.exportName,
  141. dep.property,
  142. runtime
  143. );
  144. source.replace(
  145. dep.range[0],
  146. dep.range[1] - 1,
  147. value === undefined ? "undefined" : JSON.stringify(value)
  148. );
  149. }
  150. };
  151. module.exports = ExportsInfoDependency;