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. 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} ignored module
  54. */
  55. createIgnoredModule(context) {
  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;