streamChunksOfSourceMap.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const getGeneratedSourceInfo = require("./getGeneratedSourceInfo");
  7. const getSource = require("./getSource");
  8. const readMappings = require("./readMappings");
  9. const splitIntoLines = require("./splitIntoLines");
  10. const streamChunksOfSourceMapFull = (
  11. source,
  12. sourceMap,
  13. onChunk,
  14. onSource,
  15. onName
  16. ) => {
  17. const lines = splitIntoLines(source);
  18. if (lines.length === 0) {
  19. return {
  20. generatedLine: 1,
  21. generatedColumn: 0
  22. };
  23. }
  24. const { sources, sourcesContent, names, mappings } = sourceMap;
  25. for (let i = 0; i < sources.length; i++) {
  26. onSource(
  27. i,
  28. getSource(sourceMap, i),
  29. (sourcesContent && sourcesContent[i]) || undefined
  30. );
  31. }
  32. if (names) {
  33. for (let i = 0; i < names.length; i++) {
  34. onName(i, names[i]);
  35. }
  36. }
  37. const lastLine = lines[lines.length - 1];
  38. const lastNewLine = lastLine.endsWith("\n");
  39. const finalLine = lastNewLine ? lines.length + 1 : lines.length;
  40. const finalColumn = lastNewLine ? 0 : lastLine.length;
  41. let currentGeneratedLine = 1;
  42. let currentGeneratedColumn = 0;
  43. let mappingActive = false;
  44. let activeMappingSourceIndex = -1;
  45. let activeMappingOriginalLine = -1;
  46. let activeMappingOriginalColumn = -1;
  47. let activeMappingNameIndex = -1;
  48. const onMapping = (
  49. generatedLine,
  50. generatedColumn,
  51. sourceIndex,
  52. originalLine,
  53. originalColumn,
  54. nameIndex
  55. ) => {
  56. if (mappingActive && currentGeneratedLine <= lines.length) {
  57. let chunk;
  58. const mappingLine = currentGeneratedLine;
  59. const mappingColumn = currentGeneratedColumn;
  60. const line = lines[currentGeneratedLine - 1];
  61. if (generatedLine !== currentGeneratedLine) {
  62. chunk = line.slice(currentGeneratedColumn);
  63. currentGeneratedLine++;
  64. currentGeneratedColumn = 0;
  65. } else {
  66. chunk = line.slice(currentGeneratedColumn, generatedColumn);
  67. currentGeneratedColumn = generatedColumn;
  68. }
  69. if (chunk) {
  70. onChunk(
  71. chunk,
  72. mappingLine,
  73. mappingColumn,
  74. activeMappingSourceIndex,
  75. activeMappingOriginalLine,
  76. activeMappingOriginalColumn,
  77. activeMappingNameIndex
  78. );
  79. }
  80. mappingActive = false;
  81. }
  82. if (generatedLine > currentGeneratedLine && currentGeneratedColumn > 0) {
  83. if (currentGeneratedLine <= lines.length) {
  84. const chunk = lines[currentGeneratedLine - 1].slice(
  85. currentGeneratedColumn
  86. );
  87. onChunk(
  88. chunk,
  89. currentGeneratedLine,
  90. currentGeneratedColumn,
  91. -1,
  92. -1,
  93. -1,
  94. -1
  95. );
  96. }
  97. currentGeneratedLine++;
  98. currentGeneratedColumn = 0;
  99. }
  100. while (generatedLine > currentGeneratedLine) {
  101. if (currentGeneratedLine <= lines.length) {
  102. onChunk(
  103. lines[currentGeneratedLine - 1],
  104. currentGeneratedLine,
  105. 0,
  106. -1,
  107. -1,
  108. -1,
  109. -1
  110. );
  111. }
  112. currentGeneratedLine++;
  113. }
  114. if (generatedColumn > currentGeneratedColumn) {
  115. if (currentGeneratedLine <= lines.length) {
  116. const chunk = lines[currentGeneratedLine - 1].slice(
  117. currentGeneratedColumn,
  118. generatedColumn
  119. );
  120. onChunk(
  121. chunk,
  122. currentGeneratedLine,
  123. currentGeneratedColumn,
  124. -1,
  125. -1,
  126. -1,
  127. -1
  128. );
  129. }
  130. currentGeneratedColumn = generatedColumn;
  131. }
  132. if (
  133. sourceIndex >= 0 &&
  134. (generatedLine < finalLine ||
  135. (generatedLine === finalLine && generatedColumn < finalColumn))
  136. ) {
  137. mappingActive = true;
  138. activeMappingSourceIndex = sourceIndex;
  139. activeMappingOriginalLine = originalLine;
  140. activeMappingOriginalColumn = originalColumn;
  141. activeMappingNameIndex = nameIndex;
  142. }
  143. };
  144. readMappings(mappings, onMapping);
  145. onMapping(finalLine, finalColumn, -1, -1, -1, -1);
  146. return {
  147. generatedLine: finalLine,
  148. generatedColumn: finalColumn
  149. };
  150. };
  151. const streamChunksOfSourceMapLinesFull = (
  152. source,
  153. sourceMap,
  154. onChunk,
  155. onSource,
  156. _onName
  157. ) => {
  158. const lines = splitIntoLines(source);
  159. if (lines.length === 0) {
  160. return {
  161. generatedLine: 1,
  162. generatedColumn: 0
  163. };
  164. }
  165. const { sources, sourcesContent, mappings } = sourceMap;
  166. for (let i = 0; i < sources.length; i++) {
  167. onSource(
  168. i,
  169. getSource(sourceMap, i),
  170. (sourcesContent && sourcesContent[i]) || undefined
  171. );
  172. }
  173. let currentGeneratedLine = 1;
  174. const onMapping = (
  175. generatedLine,
  176. _generatedColumn,
  177. sourceIndex,
  178. originalLine,
  179. originalColumn,
  180. _nameIndex
  181. ) => {
  182. if (
  183. sourceIndex < 0 ||
  184. generatedLine < currentGeneratedLine ||
  185. generatedLine > lines.length
  186. ) {
  187. return;
  188. }
  189. while (generatedLine > currentGeneratedLine) {
  190. if (currentGeneratedLine <= lines.length) {
  191. onChunk(
  192. lines[currentGeneratedLine - 1],
  193. currentGeneratedLine,
  194. 0,
  195. -1,
  196. -1,
  197. -1,
  198. -1
  199. );
  200. }
  201. currentGeneratedLine++;
  202. }
  203. if (generatedLine <= lines.length) {
  204. onChunk(
  205. lines[generatedLine - 1],
  206. generatedLine,
  207. 0,
  208. sourceIndex,
  209. originalLine,
  210. originalColumn,
  211. -1
  212. );
  213. currentGeneratedLine++;
  214. }
  215. };
  216. readMappings(mappings, onMapping);
  217. for (; currentGeneratedLine <= lines.length; currentGeneratedLine++) {
  218. onChunk(
  219. lines[currentGeneratedLine - 1],
  220. currentGeneratedLine,
  221. 0,
  222. -1,
  223. -1,
  224. -1,
  225. -1
  226. );
  227. }
  228. const lastLine = lines[lines.length - 1];
  229. const lastNewLine = lastLine.endsWith("\n");
  230. const finalLine = lastNewLine ? lines.length + 1 : lines.length;
  231. const finalColumn = lastNewLine ? 0 : lastLine.length;
  232. return {
  233. generatedLine: finalLine,
  234. generatedColumn: finalColumn
  235. };
  236. };
  237. const streamChunksOfSourceMapFinal = (
  238. source,
  239. sourceMap,
  240. onChunk,
  241. onSource,
  242. onName
  243. ) => {
  244. const result = getGeneratedSourceInfo(source);
  245. const { generatedLine: finalLine, generatedColumn: finalColumn } = result;
  246. if (finalLine === 1 && finalColumn === 0) return result;
  247. const { sources, sourcesContent, names, mappings } = sourceMap;
  248. for (let i = 0; i < sources.length; i++) {
  249. onSource(
  250. i,
  251. getSource(sourceMap, i),
  252. (sourcesContent && sourcesContent[i]) || undefined
  253. );
  254. }
  255. if (names) {
  256. for (let i = 0; i < names.length; i++) {
  257. onName(i, names[i]);
  258. }
  259. }
  260. let mappingActiveLine = 0;
  261. const onMapping = (
  262. generatedLine,
  263. generatedColumn,
  264. sourceIndex,
  265. originalLine,
  266. originalColumn,
  267. nameIndex
  268. ) => {
  269. if (
  270. generatedLine >= finalLine &&
  271. (generatedColumn >= finalColumn || generatedLine > finalLine)
  272. ) {
  273. return;
  274. }
  275. if (sourceIndex >= 0) {
  276. onChunk(
  277. undefined,
  278. generatedLine,
  279. generatedColumn,
  280. sourceIndex,
  281. originalLine,
  282. originalColumn,
  283. nameIndex
  284. );
  285. mappingActiveLine = generatedLine;
  286. } else if (mappingActiveLine === generatedLine) {
  287. onChunk(undefined, generatedLine, generatedColumn, -1, -1, -1, -1);
  288. mappingActiveLine = 0;
  289. }
  290. };
  291. readMappings(mappings, onMapping);
  292. return result;
  293. };
  294. const streamChunksOfSourceMapLinesFinal = (
  295. source,
  296. sourceMap,
  297. onChunk,
  298. onSource,
  299. _onName
  300. ) => {
  301. const result = getGeneratedSourceInfo(source);
  302. const { generatedLine, generatedColumn } = result;
  303. if (generatedLine === 1 && generatedColumn === 0) {
  304. return {
  305. generatedLine: 1,
  306. generatedColumn: 0
  307. };
  308. }
  309. const { sources, sourcesContent, mappings } = sourceMap;
  310. for (let i = 0; i < sources.length; i++) {
  311. onSource(
  312. i,
  313. getSource(sourceMap, i),
  314. (sourcesContent && sourcesContent[i]) || undefined
  315. );
  316. }
  317. const finalLine = generatedColumn === 0 ? generatedLine - 1 : generatedLine;
  318. let currentGeneratedLine = 1;
  319. const onMapping = (
  320. generatedLine,
  321. _generatedColumn,
  322. sourceIndex,
  323. originalLine,
  324. originalColumn,
  325. _nameIndex
  326. ) => {
  327. if (
  328. sourceIndex >= 0 &&
  329. currentGeneratedLine <= generatedLine &&
  330. generatedLine <= finalLine
  331. ) {
  332. onChunk(
  333. undefined,
  334. generatedLine,
  335. 0,
  336. sourceIndex,
  337. originalLine,
  338. originalColumn,
  339. -1
  340. );
  341. currentGeneratedLine = generatedLine + 1;
  342. }
  343. };
  344. readMappings(mappings, onMapping);
  345. return result;
  346. };
  347. module.exports = (
  348. source,
  349. sourceMap,
  350. onChunk,
  351. onSource,
  352. onName,
  353. finalSource,
  354. columns
  355. ) => {
  356. if (columns) {
  357. return finalSource
  358. ? streamChunksOfSourceMapFinal(
  359. source,
  360. sourceMap,
  361. onChunk,
  362. onSource,
  363. onName
  364. )
  365. : streamChunksOfSourceMapFull(
  366. source,
  367. sourceMap,
  368. onChunk,
  369. onSource,
  370. onName
  371. );
  372. } else {
  373. return finalSource
  374. ? streamChunksOfSourceMapLinesFinal(
  375. source,
  376. sourceMap,
  377. onChunk,
  378. onSource,
  379. onName
  380. )
  381. : streamChunksOfSourceMapLinesFull(
  382. source,
  383. sourceMap,
  384. onChunk,
  385. onSource,
  386. onName
  387. );
  388. }
  389. };