ExportsInfoDependency.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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("../Dependency")} Dependency */
  11. /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
  12. /** @typedef {import("../Module")} Module */
  13. /** @typedef {import("../ModuleGraph")} ModuleGraph */
  14. /** @typedef {import("../ExportsInfo").ExportInfoName} ExportInfoName */
  15. /** @typedef {import("../javascript/JavascriptParser").Range} Range */
  16. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  17. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  18. /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
  19. /**
  20. * Defines the sortable set type used by this module.
  21. * @template T
  22. * @typedef {import("../util/SortableSet")<T>} SortableSet
  23. */
  24. /**
  25. * Returns value of the property.
  26. * @param {ModuleGraph} moduleGraph the module graph
  27. * @param {Module} module the module
  28. * @param {ExportInfoName[] | 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 | boolean | ExportInfoName[]} 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 [...usedExports].sort();
  48. }
  49. }
  50. }
  51. const exportName = /** @type {ExportInfoName[]} */ (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. * Creates an instance of ExportsInfoDependency.
  89. * @param {Range} range range
  90. * @param {ExportInfoName[] | null} exportName export name
  91. * @param {string | null} property property
  92. */
  93. constructor(range, exportName, property) {
  94. super();
  95. this.range = range;
  96. this.exportName = exportName;
  97. this.property = property;
  98. }
  99. /**
  100. * Serializes this instance into the provided serializer context.
  101. * @param {ObjectSerializerContext} context context
  102. */
  103. serialize(context) {
  104. const { write } = context;
  105. write(this.range);
  106. write(this.exportName);
  107. write(this.property);
  108. super.serialize(context);
  109. }
  110. /**
  111. * Restores this instance from the provided deserializer context.
  112. * @param {ObjectDeserializerContext} context context
  113. * @returns {ExportsInfoDependency} ExportsInfoDependency
  114. */
  115. static deserialize(context) {
  116. const obj = new ExportsInfoDependency(
  117. context.read(),
  118. context.read(),
  119. context.read()
  120. );
  121. obj.deserialize(context);
  122. return obj;
  123. }
  124. }
  125. makeSerializable(
  126. ExportsInfoDependency,
  127. "webpack/lib/dependencies/ExportsInfoDependency"
  128. );
  129. ExportsInfoDependency.Template = class ExportsInfoDependencyTemplate extends (
  130. NullDependency.Template
  131. ) {
  132. /**
  133. * Applies the plugin by registering its hooks on the compiler.
  134. * @param {Dependency} dependency the dependency for which the template should be applied
  135. * @param {ReplaceSource} source the current replace source which can be modified
  136. * @param {DependencyTemplateContext} templateContext the context object
  137. * @returns {void}
  138. */
  139. apply(dependency, source, { module, moduleGraph, runtime }) {
  140. const dep = /** @type {ExportsInfoDependency} */ (dependency);
  141. const value = getProperty(
  142. moduleGraph,
  143. module,
  144. dep.exportName,
  145. dep.property,
  146. runtime
  147. );
  148. source.replace(
  149. dep.range[0],
  150. dep.range[1] - 1,
  151. value === undefined ? "undefined" : JSON.stringify(value)
  152. );
  153. }
  154. };
  155. module.exports = ExportsInfoDependency;