PureExpressionDependency.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 } = 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").UpdateHashContext} UpdateHashContext */
  14. /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
  15. /** @typedef {import("../ModuleGraph")} ModuleGraph */
  16. /** @typedef {import("../ModuleGraphConnection").ConnectionState} ConnectionState */
  17. /** @typedef {import("../util/Hash")} Hash */
  18. class PureExpressionDependency extends NullDependency {
  19. /**
  20. * @param {[number, number]} range the source range
  21. */
  22. constructor(range) {
  23. super();
  24. this.range = range;
  25. /** @type {Set<string> | false} */
  26. this.usedByExports = false;
  27. this._hashUpdate = undefined;
  28. }
  29. /**
  30. * Update the hash
  31. * @param {Hash} hash hash to be updated
  32. * @param {UpdateHashContext} context context
  33. * @returns {void}
  34. */
  35. updateHash(hash, context) {
  36. if (this._hashUpdate === undefined) {
  37. this._hashUpdate = this.range + "";
  38. }
  39. hash.update(this._hashUpdate);
  40. }
  41. /**
  42. * @param {ModuleGraph} moduleGraph the module graph
  43. * @returns {ConnectionState} how this dependency connects the module to referencing modules
  44. */
  45. getModuleEvaluationSideEffectsState(moduleGraph) {
  46. return false;
  47. }
  48. serialize(context) {
  49. const { write } = context;
  50. write(this.range);
  51. write(this.usedByExports);
  52. super.serialize(context);
  53. }
  54. deserialize(context) {
  55. const { read } = context;
  56. this.range = read();
  57. this.usedByExports = read();
  58. super.deserialize(context);
  59. }
  60. }
  61. makeSerializable(
  62. PureExpressionDependency,
  63. "webpack/lib/dependencies/PureExpressionDependency"
  64. );
  65. PureExpressionDependency.Template = class PureExpressionDependencyTemplate extends (
  66. NullDependency.Template
  67. ) {
  68. /**
  69. * @param {Dependency} dependency the dependency for which the template should be applied
  70. * @param {ReplaceSource} source the current replace source which can be modified
  71. * @param {DependencyTemplateContext} templateContext the context object
  72. * @returns {void}
  73. */
  74. apply(
  75. dependency,
  76. source,
  77. { chunkGraph, moduleGraph, runtime, runtimeTemplate, runtimeRequirements }
  78. ) {
  79. const dep = /** @type {PureExpressionDependency} */ (dependency);
  80. const usedByExports = dep.usedByExports;
  81. if (usedByExports !== false) {
  82. const selfModule = moduleGraph.getParentModule(dep);
  83. const exportsInfo = moduleGraph.getExportsInfo(selfModule);
  84. const runtimeCondition = filterRuntime(runtime, runtime => {
  85. for (const exportName of usedByExports) {
  86. if (exportsInfo.getUsed(exportName, runtime) !== UsageState.Unused) {
  87. return true;
  88. }
  89. }
  90. return false;
  91. });
  92. if (runtimeCondition === true) return;
  93. if (runtimeCondition !== false) {
  94. const condition = runtimeTemplate.runtimeConditionExpression({
  95. chunkGraph,
  96. runtime,
  97. runtimeCondition,
  98. runtimeRequirements
  99. });
  100. source.insert(
  101. dep.range[0],
  102. `(/* runtime-dependent pure expression or super */ ${condition} ? (`
  103. );
  104. source.insert(dep.range[1], ") : null)");
  105. return;
  106. }
  107. }
  108. source.insert(
  109. dep.range[0],
  110. `(/* unused pure expression or super */ null && (`
  111. );
  112. source.insert(dep.range[1], "))");
  113. }
  114. };
  115. module.exports = PureExpressionDependency;