ModuleDependency.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 memoize = require("../util/memoize");
  9. /** @typedef {import("../Dependency").TRANSITIVE} TRANSITIVE */
  10. /** @typedef {import("../Module")} Module */
  11. const getRawModule = memoize(() => require("../RawModule"));
  12. class ModuleDependency extends Dependency {
  13. /**
  14. * @param {string} request request path which needs resolving
  15. */
  16. constructor(request) {
  17. super();
  18. this.request = request;
  19. this.userRequest = request;
  20. this.range = undefined;
  21. // assertions must be serialized by subclasses that use it
  22. /** @type {Record<string, any> | undefined} */
  23. this.assertions = undefined;
  24. this._context = undefined;
  25. }
  26. /**
  27. * @returns {string | undefined} a request context
  28. */
  29. getContext() {
  30. return this._context;
  31. }
  32. /**
  33. * @returns {string | null} an identifier to merge equal requests
  34. */
  35. getResourceIdentifier() {
  36. let str = `context${this._context || ""}|module${this.request}`;
  37. if (this.assertions !== undefined) {
  38. str += JSON.stringify(this.assertions);
  39. }
  40. return str;
  41. }
  42. /**
  43. * @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
  44. */
  45. couldAffectReferencingModule() {
  46. return true;
  47. }
  48. /**
  49. * @param {string} context context directory
  50. * @returns {Module} a module
  51. */
  52. createIgnoredModule(context) {
  53. const RawModule = getRawModule();
  54. return new RawModule(
  55. "/* (ignored) */",
  56. `ignored|${context}|${this.request}`,
  57. `${this.request} (ignored)`
  58. );
  59. }
  60. serialize(context) {
  61. const { write } = context;
  62. write(this.request);
  63. write(this.userRequest);
  64. write(this._context);
  65. write(this.range);
  66. super.serialize(context);
  67. }
  68. deserialize(context) {
  69. const { read } = context;
  70. this.request = read();
  71. this.userRequest = read();
  72. this._context = read();
  73. this.range = read();
  74. super.deserialize(context);
  75. }
  76. }
  77. ModuleDependency.Template = DependencyTemplate;
  78. module.exports = ModuleDependency;