streamAndGetSourceAndMap.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { createMappingsWriter } = 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. /**
  16. * @param {SourceMaybeWithStreamChunksFunction} inputSource input source
  17. * @param {Options} options options
  18. * @param {OnChunk} onChunk on chunk
  19. * @param {OnSource} onSource on source
  20. * @param {OnName} onName on name
  21. * @returns {{ result: GeneratedSourceInfo, source: string, map: RawSourceMap | null }} result
  22. */
  23. const streamAndGetSourceAndMap = (
  24. inputSource,
  25. options,
  26. onChunk,
  27. onSource,
  28. onName,
  29. ) => {
  30. let code = "";
  31. /** @type {(string | null)[]} */
  32. const potentialSources = [];
  33. /** @type {(string | null)[]} */
  34. const potentialSourcesContent = [];
  35. /** @type {(string | null)[]} */
  36. const potentialNames = [];
  37. const mappingsWriter = createMappingsWriter({ ...options, columns: true });
  38. const addMapping = mappingsWriter.add;
  39. const finalSource = Boolean(options && options.finalSource);
  40. // The caller may have passed `source: false` (getMap does), but this
  41. // helper caches the source text, so the inner stream must produce it.
  42. const innerOptions =
  43. options && options.source === false
  44. ? { ...options, source: true }
  45. : options;
  46. const { generatedLine, generatedColumn, source } = streamChunks(
  47. inputSource,
  48. innerOptions,
  49. (
  50. chunk,
  51. generatedLine,
  52. generatedColumn,
  53. sourceIndex,
  54. originalLine,
  55. originalColumn,
  56. nameIndex,
  57. ) => {
  58. if (chunk !== undefined) code += chunk;
  59. addMapping(
  60. generatedLine,
  61. generatedColumn,
  62. sourceIndex,
  63. originalLine,
  64. originalColumn,
  65. nameIndex,
  66. );
  67. return onChunk(
  68. finalSource ? undefined : chunk,
  69. generatedLine,
  70. generatedColumn,
  71. sourceIndex,
  72. originalLine,
  73. originalColumn,
  74. nameIndex,
  75. );
  76. },
  77. (sourceIndex, source, sourceContent) => {
  78. while (potentialSources.length < sourceIndex) {
  79. potentialSources.push(null);
  80. }
  81. potentialSources[sourceIndex] = source;
  82. if (sourceContent !== undefined) {
  83. while (potentialSourcesContent.length < sourceIndex) {
  84. potentialSourcesContent.push(null);
  85. }
  86. potentialSourcesContent[sourceIndex] = sourceContent;
  87. }
  88. return onSource(sourceIndex, source, sourceContent);
  89. },
  90. (nameIndex, name) => {
  91. while (potentialNames.length < nameIndex) {
  92. potentialNames.push(null);
  93. }
  94. potentialNames[nameIndex] = name;
  95. return onName(nameIndex, name);
  96. },
  97. );
  98. const resultSource = source !== undefined ? source : code;
  99. const mappings = mappingsWriter.finish();
  100. return {
  101. result: {
  102. generatedLine,
  103. generatedColumn,
  104. source: finalSource ? resultSource : undefined,
  105. },
  106. source: resultSource,
  107. map:
  108. mappings.length > 0
  109. ? {
  110. version: 3,
  111. file: "x",
  112. mappings,
  113. // 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
  114. sources: /** @type {string[]} */ (potentialSources),
  115. sourcesContent:
  116. potentialSourcesContent.length > 0
  117. ? /** @type {string[]} */ (potentialSourcesContent)
  118. : undefined,
  119. names: /** @type {string[]} */ (potentialNames),
  120. }
  121. : null,
  122. };
  123. };
  124. module.exports = streamAndGetSourceAndMap;