PureExpressionDependency.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 { filterRuntime, runtimeToString } = require("../util/runtime");
  9. const NullDependency = require("./NullDependency");
  10. /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
  11. /** @typedef {import("../ChunkGraph")} ChunkGraph */
  12. /** @typedef {import("../Dependency")} Dependency */
  13. /** @typedef {import("../Dependency").RuntimeSpec} RuntimeSpec */
  14. /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
  15. /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
  16. /** @typedef {import("../Module")} Module */
  17. /** @typedef {import("../ModuleGraph")} ModuleGraph */
  18. /** @typedef {import("../ModuleGraphConnection").ConnectionState} ConnectionState */
  19. /** @typedef {import("../javascript/JavascriptParser").Range} Range */
  20. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  21. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  22. /** @typedef {import("../util/Hash")} Hash */
  23. class PureExpressionDependency extends NullDependency {
  24. /**
  25. * @param {Range} range the source range
  26. */
  27. constructor(range) {
  28. super();
  29. this.range = range;
  30. /** @type {Set<string> | false} */
  31. this.usedByExports = false;
  32. }
  33. /**
  34. * @param {ModuleGraph} moduleGraph module graph
  35. * @param {RuntimeSpec} runtime current runtimes
  36. * @returns {boolean | RuntimeSpec} runtime condition
  37. */
  38. _getRuntimeCondition(moduleGraph, runtime) {
  39. const usedByExports = this.usedByExports;
  40. if (usedByExports !== false) {
  41. const selfModule =
  42. /** @type {Module} */
  43. (moduleGraph.getParentModule(this));
  44. const exportsInfo = moduleGraph.getExportsInfo(selfModule);
  45. const runtimeCondition = filterRuntime(runtime, runtime => {
  46. for (const exportName of usedByExports) {
  47. if (exportsInfo.getUsed(exportName, runtime) !== UsageState.Unused) {
  48. return true;
  49. }
  50. }
  51. return false;
  52. });
  53. return runtimeCondition;
  54. }
  55. return false;
  56. }
  57. /**
  58. * Update the hash
  59. * @param {Hash} hash hash to be updated
  60. * @param {UpdateHashContext} context context
  61. * @returns {void}
  62. */
  63. updateHash(hash, context) {
  64. const runtimeCondition = this._getRuntimeCondition(
  65. context.chunkGraph.moduleGraph,
  66. context.runtime
  67. );
  68. if (runtimeCondition === true) {
  69. return;
  70. } else if (runtimeCondition === false) {
  71. hash.update("null");
  72. } else {
  73. hash.update(
  74. runtimeToString(runtimeCondition) +
  75. "|" +
  76. runtimeToString(context.runtime)
  77. );
  78. }
  79. hash.update(this.range + "");
  80. }
  81. /**
  82. * @param {ModuleGraph} moduleGraph the module graph
  83. * @returns {ConnectionState} how this dependency connects the module to referencing modules
  84. */
  85. getModuleEvaluationSideEffectsState(moduleGraph) {
  86. return false;
  87. }
  88. /**
  89. * @param {ObjectSerializerContext} context context
  90. */
  91. serialize(context) {
  92. const { write } = context;
  93. write(this.range);
  94. write(this.usedByExports);
  95. super.serialize(context);
  96. }
  97. /**
  98. * @param {ObjectDeserializerContext} context context
  99. */
  100. deserialize(context) {
  101. const { read } = context;
  102. this.range = read();
  103. this.usedByExports = read();
  104. super.deserialize(context);
  105. }
  106. }
  107. makeSerializable(
  108. PureExpressionDependency,
  109. "webpack/lib/dependencies/PureExpressionDependency"
  110. );
  111. PureExpressionDependency.Template = class PureExpressionDependencyTemplate extends (
  112. NullDependency.Template
  113. ) {
  114. /**
  115. * @param {Dependency} dependency the dependency for which the template should be applied
  116. * @param {ReplaceSource} source the current replace source which can be modified
  117. * @param {DependencyTemplateContext} templateContext the context object
  118. * @returns {void}
  119. */
  120. apply(
  121. dependency,
  122. source,
  123. { chunkGraph, moduleGraph, runtime, runtimeTemplate, runtimeRequirements }
  124. ) {
  125. const dep = /** @type {PureExpressionDependency} */ (dependency);
  126. const runtimeCondition = dep._getRuntimeCondition(moduleGraph, runtime);
  127. if (runtimeCondition === true) {
  128. return;
  129. } else if (runtimeCondition === false) {
  130. source.insert(
  131. dep.range[0],
  132. `(/* unused pure expression or super */ null && (`
  133. );
  134. source.insert(dep.range[1], "))");
  135. } else {
  136. const condition = runtimeTemplate.runtimeConditionExpression({
  137. chunkGraph,
  138. runtime,
  139. runtimeCondition,
  140. runtimeRequirements
  141. });
  142. source.insert(
  143. dep.range[0],
  144. `(/* runtime-dependent pure expression or super */ ${condition} ? (`
  145. );
  146. source.insert(dep.range[1], ") : null)");
  147. }
  148. }
  149. };
  150. module.exports = PureExpressionDependency;