streamAndGetSourceAndMap.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const createMappingsSerializer = require("./createMappingsSerializer");
  7. const streamChunks = require("./streamChunks");
  8. /** @typedef {import("../Source").RawSourceMap} RawSourceMap */
  9. /** @typedef {import("./streamChunks").GeneratedSourceInfo} GeneratedSourceInfo */
  10. /** @typedef {import("./streamChunks").OnChunk} OnChunk */
  11. /** @typedef {import("./streamChunks").OnName} OnName */
  12. /** @typedef {import("./streamChunks").OnSource} OnSource */
  13. /** @typedef {import("./streamChunks").Options} Options */
  14. /** @typedef {import("./streamChunks").SourceMaybeWithStreamChunksFunction} SourceMaybeWithStreamChunksFunction */
  15. // eslint-disable-next-line valid-jsdoc
  16. /**
  17. * @param {SourceMaybeWithStreamChunksFunction} inputSource input source
  18. * @param {Options} options options
  19. * @param {OnChunk} onChunk on chunk
  20. * @param {OnSource} onSource on source
  21. * @param {OnName} onName on name
  22. * @returns {{ result: GeneratedSourceInfo, source: string, map: RawSourceMap | null }} result
  23. */
  24. const streamAndGetSourceAndMap = (
  25. inputSource,
  26. options,
  27. onChunk,
  28. onSource,
  29. onName
  30. ) => {
  31. let code = "";
  32. let mappings = "";
  33. /** @type {(string | null)[]} */
  34. let sources = [];
  35. /** @type {(string | null)[]} */
  36. let sourcesContent = [];
  37. /** @type {(string | null)[]} */
  38. let names = [];
  39. const addMapping = createMappingsSerializer(
  40. Object.assign({}, options, { columns: true })
  41. );
  42. const finalSource = !!(options && options.finalSource);
  43. const { generatedLine, generatedColumn, source } = streamChunks(
  44. inputSource,
  45. options,
  46. (
  47. chunk,
  48. generatedLine,
  49. generatedColumn,
  50. sourceIndex,
  51. originalLine,
  52. originalColumn,
  53. nameIndex
  54. ) => {
  55. if (chunk !== undefined) code += chunk;
  56. mappings += addMapping(
  57. generatedLine,
  58. generatedColumn,
  59. sourceIndex,
  60. originalLine,
  61. originalColumn,
  62. nameIndex
  63. );
  64. return onChunk(
  65. finalSource ? undefined : chunk,
  66. generatedLine,
  67. generatedColumn,
  68. sourceIndex,
  69. originalLine,
  70. originalColumn,
  71. nameIndex
  72. );
  73. },
  74. (sourceIndex, source, sourceContent) => {
  75. while (sources.length < sourceIndex) {
  76. sources.push(null);
  77. }
  78. sources[sourceIndex] = source;
  79. if (sourceContent !== undefined) {
  80. while (sourcesContent.length < sourceIndex) {
  81. sourcesContent.push(null);
  82. }
  83. sourcesContent[sourceIndex] = sourceContent;
  84. }
  85. return onSource(sourceIndex, source, sourceContent);
  86. },
  87. (nameIndex, name) => {
  88. while (names.length < nameIndex) {
  89. names.push(null);
  90. }
  91. names[nameIndex] = name;
  92. return onName(nameIndex, name);
  93. }
  94. );
  95. const resultSource = source !== undefined ? source : code;
  96. return {
  97. result: {
  98. generatedLine,
  99. generatedColumn,
  100. source: finalSource ? resultSource : undefined
  101. },
  102. source: resultSource,
  103. map:
  104. mappings.length > 0
  105. ? {
  106. version: 3,
  107. file: "x",
  108. mappings,
  109. // We handle broken sources as `null`, in spec this field should be string, but no information what we should do in such cases if we change type it will be breaking change
  110. sources: /** @type {string[]} */ (sources),
  111. sourcesContent:
  112. sourcesContent.length > 0
  113. ? /** @type {string[]} */ (sourcesContent)
  114. : undefined,
  115. names: /** @type {string[]} */ (names)
  116. }
  117. : null
  118. };
  119. };
  120. module.exports = streamAndGetSourceAndMap;