CompatSource.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const Source = require("./Source");
  7. /** @typedef {import("./Source").ClearCacheOptions} ClearCacheOptions */
  8. /** @typedef {import("./Source").HashLike} HashLike */
  9. /** @typedef {import("./Source").MapOptions} MapOptions */
  10. /** @typedef {import("./Source").RawSourceMap} RawSourceMap */
  11. /** @typedef {import("./Source").SourceAndMap} SourceAndMap */
  12. /** @typedef {import("./Source").SourceValue} SourceValue */
  13. /**
  14. * @typedef {object} SourceLike
  15. * @property {() => SourceValue} source source
  16. * @property {(() => Buffer)=} buffer buffer
  17. * @property {(() => Buffer[])=} buffers buffers
  18. * @property {(() => number)=} size size
  19. * @property {((options?: MapOptions) => RawSourceMap | null)=} map map
  20. * @property {((options?: MapOptions) => SourceAndMap)=} sourceAndMap source and map
  21. * @property {((hash: HashLike) => void)=} updateHash hash updater
  22. * @property {((options?: ClearCacheOptions, visited?: WeakSet<Source>) => void)=} clearCache clear cache
  23. */
  24. class CompatSource extends Source {
  25. /**
  26. * @param {SourceLike} sourceLike source like
  27. * @returns {Source} source
  28. */
  29. static from(sourceLike) {
  30. return sourceLike instanceof Source
  31. ? sourceLike
  32. : new CompatSource(sourceLike);
  33. }
  34. /**
  35. * @param {SourceLike} sourceLike source like
  36. */
  37. constructor(sourceLike) {
  38. super();
  39. /**
  40. * @private
  41. * @type {SourceLike}
  42. */
  43. this._sourceLike = sourceLike;
  44. }
  45. /**
  46. * @returns {SourceValue} source
  47. */
  48. source() {
  49. return this._sourceLike.source();
  50. }
  51. /**
  52. * @returns {Buffer} buffer
  53. */
  54. buffer() {
  55. if (typeof this._sourceLike.buffer === "function") {
  56. return this._sourceLike.buffer();
  57. }
  58. return super.buffer();
  59. }
  60. /**
  61. * @returns {Buffer[]} buffers
  62. */
  63. buffers() {
  64. if (typeof this._sourceLike.buffers === "function") {
  65. return this._sourceLike.buffers();
  66. }
  67. return super.buffers();
  68. }
  69. /**
  70. * @returns {number} size
  71. */
  72. size() {
  73. if (typeof this._sourceLike.size === "function") {
  74. return this._sourceLike.size();
  75. }
  76. return super.size();
  77. }
  78. /**
  79. * @param {MapOptions=} options map options
  80. * @returns {RawSourceMap | null} map
  81. */
  82. map(options) {
  83. if (typeof this._sourceLike.map === "function") {
  84. return this._sourceLike.map(options);
  85. }
  86. return super.map(options);
  87. }
  88. /**
  89. * @param {MapOptions=} options map options
  90. * @returns {SourceAndMap} source and map
  91. */
  92. sourceAndMap(options) {
  93. if (typeof this._sourceLike.sourceAndMap === "function") {
  94. return this._sourceLike.sourceAndMap(options);
  95. }
  96. return super.sourceAndMap(options);
  97. }
  98. /**
  99. * Release cached data held by this source. clearCache is a memory
  100. * hint: it never affects correctness or output, only how expensive
  101. * the next read is. Subclasses override; the base is a no-op so
  102. * every Source supports the call. Composite sources always recurse
  103. * into wrapped sources. When the same child is reachable via several
  104. * parents (e.g. modules shared across webpack chunks), pass a shared
  105. * `visited` WeakSet so each subtree is walked at most once.
  106. * Not safe to call concurrently with source/map/sourceAndMap/
  107. * streamChunks/updateHash on the same instance.
  108. * @param {ClearCacheOptions=} options selectors
  109. * @param {WeakSet<Source>=} visited de-duplication set shared across calls
  110. * @returns {void}
  111. */
  112. clearCache(options, visited) {
  113. if (visited !== undefined && visited.has(this)) return;
  114. if (visited !== undefined) visited.add(this);
  115. const sourceLike = this._sourceLike;
  116. if (typeof sourceLike.clearCache === "function") {
  117. sourceLike.clearCache(options, visited);
  118. }
  119. }
  120. /**
  121. * @param {HashLike} hash hash
  122. * @returns {void}
  123. */
  124. updateHash(hash) {
  125. if (typeof this._sourceLike.updateHash === "function") {
  126. return this._sourceLike.updateHash(hash);
  127. }
  128. if (typeof this._sourceLike.map === "function") {
  129. throw new Error(
  130. "A Source-like object with a 'map' method must also provide an 'updateHash' method",
  131. );
  132. }
  133. hash.update(this.buffer());
  134. }
  135. }
  136. module.exports = CompatSource;