ExportsInfoDependency.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. switch (property) {
  48. case "canMangle": {
  49. const exportsInfo = moduleGraph.getExportsInfo(module);
  50. const exportInfo = exportsInfo.getExportInfo(
  51. /** @type {string} */ (exportName)
  52. );
  53. if (exportInfo) return exportInfo.canMangle;
  54. return exportsInfo.otherExportsInfo.canMangle;
  55. }
  56. case "used":
  57. return (
  58. moduleGraph
  59. .getExportsInfo(module)
  60. .getUsed(/** @type {string} */ (exportName), runtime) !==
  61. UsageState.Unused
  62. );
  63. case "useInfo": {
  64. const state = moduleGraph
  65. .getExportsInfo(module)
  66. .getUsed(/** @type {string} */ (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 undefined;
  75. case UsageState.Unknown:
  76. return null;
  77. default:
  78. throw new Error(`Unexpected UsageState ${state}`);
  79. }
  80. }
  81. case "provideInfo":
  82. return moduleGraph
  83. .getExportsInfo(module)
  84. .isExportProvided(/** @type {string} */ (exportName));
  85. }
  86. return undefined;
  87. };
  88. class ExportsInfoDependency extends NullDependency {
  89. /**
  90. * @param {Range} range range
  91. * @param {TODO} exportName export name
  92. * @param {string | null} property property
  93. */
  94. constructor(range, exportName, property) {
  95. super();
  96. this.range = range;
  97. this.exportName = exportName;
  98. this.property = property;
  99. }
  100. /**
  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. * @param {ObjectDeserializerContext} context context
  112. * @returns {ExportsInfoDependency} ExportsInfoDependency
  113. */
  114. static deserialize(context) {
  115. const obj = new ExportsInfoDependency(
  116. context.read(),
  117. context.read(),
  118. context.read()
  119. );
  120. obj.deserialize(context);
  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. * @param {Dependency} dependency the dependency for which the template should be applied
  133. * @param {ReplaceSource} source the current replace source which can be modified
  134. * @param {DependencyTemplateContext} templateContext the context object
  135. * @returns {void}
  136. */
  137. apply(dependency, source, { module, moduleGraph, runtime }) {
  138. const dep = /** @type {ExportsInfoDependency} */ (dependency);
  139. const value = getProperty(
  140. moduleGraph,
  141. module,
  142. dep.exportName,
  143. dep.property,
  144. runtime
  145. );
  146. source.replace(
  147. dep.range[0],
  148. dep.range[1] - 1,
  149. value === undefined ? "undefined" : JSON.stringify(value)
  150. );
  151. }
  152. };
  153. module.exports = ExportsInfoDependency;