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