ModuleDependency.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. * @param {string} request request path which needs resolving
  17. * @param {number=} sourceOrder source order
  18. */
  19. constructor(request, sourceOrder) {
  20. super();
  21. this.request = request;
  22. this.userRequest = request;
  23. this.sourceOrder = sourceOrder;
  24. /** @type {Range | undefined} */
  25. this.range = undefined;
  26. /** @type {undefined | string} */
  27. this._context = undefined;
  28. }
  29. /**
  30. * @returns {string | undefined} a request context
  31. */
  32. getContext() {
  33. return this._context;
  34. }
  35. /**
  36. * @returns {string | null} an identifier to merge equal requests
  37. */
  38. getResourceIdentifier() {
  39. return `context${this._context || ""}|module${this.request}`;
  40. }
  41. /**
  42. * @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
  43. */
  44. couldAffectReferencingModule() {
  45. return true;
  46. }
  47. /**
  48. * @param {string} context context directory
  49. * @returns {Module} ignored module
  50. */
  51. createIgnoredModule(context) {
  52. const RawModule = require("../RawModule");
  53. const module = new RawModule(
  54. "/* (ignored) */",
  55. `ignored|${context}|${this.request}`,
  56. `${this.request} (ignored)`
  57. );
  58. module.factoryMeta = { sideEffectFree: true };
  59. return module;
  60. }
  61. /**
  62. * @param {ObjectSerializerContext} context context
  63. */
  64. serialize(context) {
  65. const { write } = context;
  66. write(this.request);
  67. write(this.userRequest);
  68. write(this._context);
  69. write(this.range);
  70. write(this.sourceOrder);
  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. this.sourceOrder = read();
  83. super.deserialize(context);
  84. }
  85. }
  86. ModuleDependency.Template = DependencyTemplate;
  87. module.exports = ModuleDependency;