streamChunks.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const streamChunksOfRawSource = require("./streamChunksOfRawSource");
  7. const streamChunksOfSourceMap = require("./streamChunksOfSourceMap");
  8. /** @typedef {import("../Source")} Source */
  9. /** @typedef {import("./getGeneratedSourceInfo").GeneratedSourceInfo} GeneratedSourceInfo */
  10. /** @typedef {(chunk: string | undefined, generatedLine: number, generatedColumn: number, sourceIndex: number, originalLine: number, originalColumn: number, nameIndex: number) => void} OnChunk */
  11. /** @typedef {(sourceIndex: number, source: string | null, sourceContent: string | undefined) => void} OnSource */
  12. /** @typedef {(nameIndex: number, name: string) => void} OnName */
  13. /** @typedef {{ source?: boolean, finalSource?: boolean, columns?: boolean }} Options */
  14. /**
  15. * @callback StreamChunksFunction
  16. * @param {Options} options options
  17. * @param {OnChunk} onChunk on chunk
  18. * @param {OnSource} onSource on source
  19. * @param {OnName} onName on name
  20. */
  21. /** @typedef {Source & { streamChunks?: StreamChunksFunction }} SourceMaybeWithStreamChunksFunction */
  22. /**
  23. * @param {SourceMaybeWithStreamChunksFunction} source source
  24. * @param {Options} options options
  25. * @param {OnChunk} onChunk on chunk
  26. * @param {OnSource} onSource on source
  27. * @param {OnName} onName on name
  28. * @returns {GeneratedSourceInfo} generated source info
  29. */
  30. module.exports = (source, options, onChunk, onSource, onName) => {
  31. if (typeof source.streamChunks === "function") {
  32. return source.streamChunks(options, onChunk, onSource, onName);
  33. }
  34. const sourceAndMap = source.sourceAndMap(options);
  35. if (sourceAndMap.map) {
  36. return streamChunksOfSourceMap(
  37. /** @type {string} */
  38. (sourceAndMap.source),
  39. sourceAndMap.map,
  40. onChunk,
  41. onSource,
  42. onName,
  43. Boolean(options && options.finalSource),
  44. Boolean(options && options.columns !== false),
  45. );
  46. }
  47. return streamChunksOfRawSource(
  48. /** @type {string} */
  49. (sourceAndMap.source),
  50. onChunk,
  51. onSource,
  52. onName,
  53. Boolean(options && options.finalSource),
  54. );
  55. };