ModuleDependency.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 memoize = require("../util/memoize");
  9. /** @typedef {import("../Dependency").TRANSITIVE} TRANSITIVE */
  10. /** @typedef {import("../Module")} Module */
  11. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  12. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  13. const getRawModule = memoize(() => require("../RawModule"));
  14. class ModuleDependency extends Dependency {
  15. /**
  16. * @param {string} request request path which needs resolving
  17. */
  18. constructor(request) {
  19. super();
  20. this.request = request;
  21. this.userRequest = request;
  22. this.range = undefined;
  23. // assertions must be serialized by subclasses that use it
  24. /** @type {Record<string, any> | undefined} */
  25. this.assertions = undefined;
  26. this._context = undefined;
  27. }
  28. /**
  29. * @returns {string | undefined} a request context
  30. */
  31. getContext() {
  32. return this._context;
  33. }
  34. /**
  35. * @returns {string | null} an identifier to merge equal requests
  36. */
  37. getResourceIdentifier() {
  38. let str = `context${this._context || ""}|module${this.request}`;
  39. if (this.assertions !== undefined) {
  40. str += JSON.stringify(this.assertions);
  41. }
  42. return str;
  43. }
  44. /**
  45. * @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
  46. */
  47. couldAffectReferencingModule() {
  48. return true;
  49. }
  50. /**
  51. * @param {string} context context directory
  52. * @returns {Module | null} a module
  53. */
  54. createIgnoredModule(context) {
  55. const RawModule = getRawModule();
  56. return new RawModule(
  57. "/* (ignored) */",
  58. `ignored|${context}|${this.request}`,
  59. `${this.request} (ignored)`
  60. );
  61. }
  62. /**
  63. * @param {ObjectSerializerContext} context context
  64. */
  65. serialize(context) {
  66. const { write } = context;
  67. write(this.request);
  68. write(this.userRequest);
  69. write(this._context);
  70. write(this.range);
  71. super.serialize(context);
  72. }
  73. /**
  74. * @param {ObjectDeserializerContext} context context
  75. */
  76. deserialize(context) {
  77. const { read } = context;
  78. this.request = read();
  79. this.userRequest = read();
  80. this._context = read();
  81. this.range = read();
  82. super.deserialize(context);
  83. }
  84. }
  85. ModuleDependency.Template = DependencyTemplate;
  86. module.exports = ModuleDependency;