Source.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. /**
  7. * @typedef {object} MapOptions
  8. * @property {boolean=} columns need columns?
  9. * @property {boolean=} module is module
  10. */
  11. /**
  12. * @typedef {object} RawSourceMap
  13. * @property {number} version version
  14. * @property {string[]} sources sources
  15. * @property {string[]} names names
  16. * @property {string=} sourceRoot source root
  17. * @property {string[]=} sourcesContent sources content
  18. * @property {string} mappings mappings
  19. * @property {string} file file
  20. * @property {string=} debugId debug id
  21. * @property {number[]=} ignoreList ignore list
  22. */
  23. /** @typedef {string | Buffer} SourceValue */
  24. /**
  25. * @typedef {object} SourceAndMap
  26. * @property {SourceValue} source source
  27. * @property {RawSourceMap | null} map map
  28. */
  29. /**
  30. * @typedef {object} HashLike
  31. * @property {(data: string | Buffer, inputEncoding?: string) => HashLike} update make hash update
  32. * @property {(encoding?: string) => string | Buffer} digest get hash digest
  33. */
  34. /**
  35. * @typedef {object} ClearCacheOptions
  36. * @property {boolean=} maps drop cached source maps (default `true`)
  37. * @property {boolean=} source drop cached source/buffer copies (default `true`)
  38. * @property {boolean=} parsedMap drop the parsed object form of cached source maps on `SourceMapSource` instances (default `false` — re-parsing JSON is significantly more expensive than `toString`). Only takes effect when a serialized form (buffer or string) is also retained, so the data remains recoverable.
  39. */
  40. class Source {
  41. /**
  42. * @returns {SourceValue} source
  43. */
  44. source() {
  45. throw new Error("Abstract");
  46. }
  47. /**
  48. * @returns {Buffer} buffer
  49. */
  50. buffer() {
  51. const source = this.source();
  52. if (Buffer.isBuffer(source)) return source;
  53. return Buffer.from(source, "utf8");
  54. }
  55. /**
  56. * @returns {Buffer[]} buffers
  57. */
  58. buffers() {
  59. return [this.buffer()];
  60. }
  61. /**
  62. * @returns {number} size
  63. */
  64. size() {
  65. return this.buffer().length;
  66. }
  67. /**
  68. * @param {MapOptions=} options map options
  69. * @returns {RawSourceMap | null} map
  70. */
  71. // eslint-disable-next-line no-unused-vars
  72. map(options) {
  73. return null;
  74. }
  75. /**
  76. * @param {MapOptions=} options map options
  77. * @returns {SourceAndMap} source and map
  78. */
  79. sourceAndMap(options) {
  80. return {
  81. source: this.source(),
  82. map: this.map(options),
  83. };
  84. }
  85. /**
  86. * @param {HashLike} hash hash
  87. * @returns {void}
  88. */
  89. // eslint-disable-next-line no-unused-vars
  90. updateHash(hash) {
  91. throw new Error("Abstract");
  92. }
  93. /**
  94. * Release cached data held by this source. clearCache is a memory
  95. * hint: it never affects correctness or output, only how expensive
  96. * the next read is. Subclasses override; the base is a no-op so
  97. * every Source supports the call. Composite sources always recurse
  98. * into wrapped sources. When the same child is reachable via several
  99. * parents (e.g. modules shared across webpack chunks), pass a shared
  100. * `visited` WeakSet so each subtree is walked at most once.
  101. * Not safe to call concurrently with source/map/sourceAndMap/
  102. * streamChunks/updateHash on the same instance.
  103. * @param {ClearCacheOptions=} options selectors
  104. * @param {WeakSet<Source>=} visited de-duplication set shared across calls
  105. * @returns {void}
  106. */
  107. // eslint-disable-next-line no-unused-vars
  108. clearCache(options, visited) {}
  109. }
  110. module.exports = Source;