ExportsInfoDependency.js 4.6 KB

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