ModuleDependency.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  12. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  13. class ModuleDependency extends Dependency {
  14. /**
  15. * @param {string} request request path which needs resolving
  16. */
  17. constructor(request) {
  18. super();
  19. this.request = request;
  20. this.userRequest = request;
  21. this.range = undefined;
  22. // TODO move it to subclasses and rename
  23. // assertions must be serialized by subclasses that use it
  24. /** @type {ImportAttributes | 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} ignored module
  53. */
  54. createIgnoredModule(context) {
  55. const RawModule = require("../RawModule");
  56. const module = new RawModule(
  57. "/* (ignored) */",
  58. `ignored|${context}|${this.request}`,
  59. `${this.request} (ignored)`
  60. );
  61. module.factoryMeta = { sideEffectFree: true };
  62. return module;
  63. }
  64. /**
  65. * @param {ObjectSerializerContext} context context
  66. */
  67. serialize(context) {
  68. const { write } = context;
  69. write(this.request);
  70. write(this.userRequest);
  71. write(this._context);
  72. write(this.range);
  73. super.serialize(context);
  74. }
  75. /**
  76. * @param {ObjectDeserializerContext} context context
  77. */
  78. deserialize(context) {
  79. const { read } = context;
  80. this.request = read();
  81. this.userRequest = read();
  82. this._context = read();
  83. this.range = read();
  84. super.deserialize(context);
  85. }
  86. }
  87. ModuleDependency.Template = DependencyTemplate;
  88. module.exports = ModuleDependency;