ModuleDependency.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. /** @import { TRANSITIVE } from "../Dependency" */
  9. /** @import Module from "../Module" */
  10. /** @import { Range } from "../javascript/JavascriptParser" */
  11. /**
  12. * @import {
  13. * ObjectDeserializerContext,
  14. * ObjectSerializerContext
  15. * } from "../serialization/ObjectMiddleware"
  16. */
  17. class ModuleDependency extends Dependency {
  18. /**
  19. * Creates an instance of ModuleDependency.
  20. * @param {string} request request path which needs resolving
  21. * @param {number=} sourceOrder source order
  22. */
  23. constructor(request, sourceOrder) {
  24. super();
  25. /** @type {string} */
  26. this.request = request;
  27. /** @type {string} */
  28. this.userRequest = request;
  29. /** @type {number | undefined} */
  30. this.sourceOrder = sourceOrder;
  31. /** @type {Range | undefined} */
  32. this.range = undefined;
  33. /** @type {undefined | string} */
  34. this._context = undefined;
  35. /** @type {boolean} */
  36. this.weak = false;
  37. }
  38. /**
  39. * Returns a request context.
  40. * @returns {string | undefined} a request context
  41. */
  42. getContext() {
  43. return this._context;
  44. }
  45. /**
  46. * Returns an identifier to merge equal requests.
  47. * @returns {string | null} an identifier to merge equal requests
  48. */
  49. getResourceIdentifier() {
  50. return `context${this._context || ""}|module${this.request}`;
  51. }
  52. /**
  53. * Could affect referencing module.
  54. * @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
  55. */
  56. couldAffectReferencingModule() {
  57. return true;
  58. }
  59. /**
  60. * Creates an ignored module.
  61. * @param {string} context context directory
  62. * @returns {Module} ignored module
  63. */
  64. createIgnoredModule(context) {
  65. const RawModule = require("../RawModule");
  66. const module = new RawModule(
  67. "/* (ignored) */",
  68. `ignored|${context}|${this.request}`,
  69. `${this.request} (ignored)`
  70. );
  71. module.factoryMeta = { sideEffectFree: true };
  72. return module;
  73. }
  74. /**
  75. * Serializes this instance into the provided serializer context.
  76. * @param {ObjectSerializerContext} context context
  77. */
  78. serialize(context) {
  79. const { write } = context;
  80. write(this.request);
  81. write(this.userRequest);
  82. write(this._context);
  83. write(this.range);
  84. write(this.sourceOrder);
  85. write(this.weak);
  86. super.serialize(context);
  87. }
  88. /**
  89. * Restores this instance from the provided deserializer context.
  90. * @param {ObjectDeserializerContext} context context
  91. */
  92. deserialize(context) {
  93. const { read } = context;
  94. this.request = read();
  95. this.userRequest = read();
  96. this._context = read();
  97. this.range = read();
  98. this.sourceOrder = read();
  99. this.weak = read();
  100. super.deserialize(context);
  101. }
  102. }
  103. ModuleDependency.Template = DependencyTemplate;
  104. module.exports = ModuleDependency;