ExportsInfoDependency.js 3.9 KB

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