Source.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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
  9. * @property {boolean=} module
  10. */
  11. /**
  12. * @typedef {object} RawSourceMap
  13. * @property {number} version
  14. * @property {string[]} sources
  15. * @property {string[]} names
  16. * @property {string=} sourceRoot
  17. * @property {string[]=} sourcesContent
  18. * @property {string} mappings
  19. * @property {string} file
  20. */
  21. /** @typedef {string | Buffer} SourceValue */
  22. /**
  23. * @typedef {object} SourceAndMap
  24. * @property {SourceValue} source
  25. * @property {RawSourceMap | null} map
  26. */
  27. /**
  28. * @typedef {object} Hash
  29. * @property {(data: string | Buffer, inputEncoding?: string) => Hash} update
  30. * @property {(encoding?: string) => string | Buffer} digest
  31. */
  32. class Source {
  33. /**
  34. * @returns {SourceValue} source
  35. */
  36. source() {
  37. throw new Error("Abstract");
  38. }
  39. buffer() {
  40. const source = this.source();
  41. if (Buffer.isBuffer(source)) return source;
  42. return Buffer.from(source, "utf-8");
  43. }
  44. size() {
  45. return this.buffer().length;
  46. }
  47. /**
  48. * @param {MapOptions=} options map options
  49. * @returns {RawSourceMap | null} map
  50. */
  51. map(options) {
  52. return null;
  53. }
  54. /**
  55. * @param {MapOptions=} options map options
  56. * @returns {SourceAndMap} source and map
  57. */
  58. sourceAndMap(options) {
  59. return {
  60. source: this.source(),
  61. map: this.map(options)
  62. };
  63. }
  64. /**
  65. * @param {Hash} hash hash
  66. * @returns {void}
  67. */
  68. updateHash(hash) {
  69. throw new Error("Abstract");
  70. }
  71. }
  72. module.exports = Source;