ModuleDependency.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 RawModule = require("../RawModule");
  9. /** @typedef {import("../Dependency").TRANSITIVE} TRANSITIVE */
  10. /** @typedef {import("../Module")} Module */
  11. /** @typedef {import("../javascript/JavascriptParser").ImportAttributes} ImportAttributes */
  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. */
  18. constructor(request) {
  19. super();
  20. this.request = request;
  21. this.userRequest = request;
  22. this.range = undefined;
  23. // TODO move it to subclasses and rename
  24. // assertions must be serialized by subclasses that use it
  25. /** @type {ImportAttributes | undefined} */
  26. this.assertions = undefined;
  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. let str = `context${this._context || ""}|module${this.request}`;
  40. if (this.assertions !== undefined) {
  41. str += JSON.stringify(this.assertions);
  42. }
  43. return str;
  44. }
  45. /**
  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. * @param {string} context context directory
  53. * @returns {Module | null} a module
  54. */
  55. createIgnoredModule(context) {
  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;