ModuleDependency.js 3.0 KB

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