ContextDependency.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const Dependency = require("../Dependency");
  7. const DependencyTemplate = require("../DependencyTemplate");
  8. const makeSerializable = require("../util/makeSerializable");
  9. const memoize = require("../util/memoize");
  10. /** @typedef {import("../ContextModule").ContextOptions} ContextOptions */
  11. /** @typedef {import("../Dependency").TRANSITIVE} TRANSITIVE */
  12. /** @typedef {import("../ModuleGraph")} ModuleGraph */
  13. /** @typedef {import("../WebpackError")} WebpackError */
  14. const getCriticalDependencyWarning = memoize(() =>
  15. require("./CriticalDependencyWarning")
  16. );
  17. /** @typedef {ContextOptions & { request: string }} ContextDependencyOptions */
  18. const regExpToString = r => (r ? r + "" : "");
  19. class ContextDependency extends Dependency {
  20. /**
  21. * @param {ContextDependencyOptions} options options for the context module
  22. */
  23. constructor(options) {
  24. super();
  25. this.options = options;
  26. this.userRequest = this.options && this.options.request;
  27. /** @type {false | string} */
  28. this.critical = false;
  29. this.hadGlobalOrStickyRegExp = false;
  30. if (
  31. this.options &&
  32. (this.options.regExp.global || this.options.regExp.sticky)
  33. ) {
  34. this.options = { ...this.options, regExp: null };
  35. this.hadGlobalOrStickyRegExp = true;
  36. }
  37. this.request = undefined;
  38. this.range = undefined;
  39. this.valueRange = undefined;
  40. this.inShorthand = undefined;
  41. // TODO refactor this
  42. this.replaces = undefined;
  43. }
  44. get category() {
  45. return "commonjs";
  46. }
  47. /**
  48. * @returns {boolean | TRANSITIVE} true, when changes to the referenced module could affect the referencing module; TRANSITIVE, when changes to the referenced module could affect referencing modules of the referencing module
  49. */
  50. couldAffectReferencingModule() {
  51. return true;
  52. }
  53. /**
  54. * @returns {string | null} an identifier to merge equal requests
  55. */
  56. getResourceIdentifier() {
  57. return (
  58. `context${this.options.request} ${this.options.recursive} ` +
  59. `${regExpToString(this.options.regExp)} ${regExpToString(
  60. this.options.include
  61. )} ${regExpToString(this.options.exclude)} ` +
  62. `${this.options.mode} ${this.options.chunkName} ` +
  63. `${JSON.stringify(this.options.groupOptions)}`
  64. );
  65. }
  66. /**
  67. * Returns warnings
  68. * @param {ModuleGraph} moduleGraph module graph
  69. * @returns {WebpackError[]} warnings
  70. */
  71. getWarnings(moduleGraph) {
  72. let warnings = super.getWarnings(moduleGraph);
  73. if (this.critical) {
  74. if (!warnings) warnings = [];
  75. const CriticalDependencyWarning = getCriticalDependencyWarning();
  76. warnings.push(new CriticalDependencyWarning(this.critical));
  77. }
  78. if (this.hadGlobalOrStickyRegExp) {
  79. if (!warnings) warnings = [];
  80. const CriticalDependencyWarning = getCriticalDependencyWarning();
  81. warnings.push(
  82. new CriticalDependencyWarning(
  83. "Contexts can't use RegExps with the 'g' or 'y' flags."
  84. )
  85. );
  86. }
  87. return warnings;
  88. }
  89. serialize(context) {
  90. const { write } = context;
  91. write(this.options);
  92. write(this.userRequest);
  93. write(this.critical);
  94. write(this.hadGlobalOrStickyRegExp);
  95. write(this.request);
  96. write(this.range);
  97. write(this.valueRange);
  98. write(this.prepend);
  99. write(this.replaces);
  100. super.serialize(context);
  101. }
  102. deserialize(context) {
  103. const { read } = context;
  104. this.options = read();
  105. this.userRequest = read();
  106. this.critical = read();
  107. this.hadGlobalOrStickyRegExp = read();
  108. this.request = read();
  109. this.range = read();
  110. this.valueRange = read();
  111. this.prepend = read();
  112. this.replaces = read();
  113. super.deserialize(context);
  114. }
  115. }
  116. makeSerializable(
  117. ContextDependency,
  118. "webpack/lib/dependencies/ContextDependency"
  119. );
  120. ContextDependency.Template = DependencyTemplate;
  121. module.exports = ContextDependency;