ModuleDependency.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. }
  25. /**
  26. * @returns {string | null} an identifier to merge equal requests
  27. */
  28. getResourceIdentifier() {
  29. let str = `module${this.request}`;
  30. if (this.assertions !== undefined) {
  31. str += JSON.stringify(this.assertions);
  32. }
  33. return str;
  34. }
  35. /**
  36. * @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
  37. */
  38. couldAffectReferencingModule() {
  39. return true;
  40. }
  41. /**
  42. * @param {string} context context directory
  43. * @returns {Module} a module
  44. */
  45. createIgnoredModule(context) {
  46. const RawModule = getRawModule();
  47. return new RawModule(
  48. "/* (ignored) */",
  49. `ignored|${context}|${this.request}`,
  50. `${this.request} (ignored)`
  51. );
  52. }
  53. serialize(context) {
  54. const { write } = context;
  55. write(this.request);
  56. write(this.userRequest);
  57. write(this.range);
  58. super.serialize(context);
  59. }
  60. deserialize(context) {
  61. const { read } = context;
  62. this.request = read();
  63. this.userRequest = read();
  64. this.range = read();
  65. super.deserialize(context);
  66. }
  67. }
  68. ModuleDependency.Template = DependencyTemplate;
  69. module.exports = ModuleDependency;