ExportsInfoDependency.js 5.0 KB

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