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 createMappingsSerializer = require("./createMappingsSerializer");
|
---|
9 | const streamChunks = require("./streamChunks");
|
---|
10 |
|
---|
11 | const streamAndGetSourceAndMap = (
|
---|
12 | inputSource,
|
---|
13 | options,
|
---|
14 | onChunk,
|
---|
15 | onSource,
|
---|
16 | onName
|
---|
17 | ) => {
|
---|
18 | let code = "";
|
---|
19 | let mappings = "";
|
---|
20 | let sources = [];
|
---|
21 | let sourcesContent = [];
|
---|
22 | let names = [];
|
---|
23 | const addMapping = createMappingsSerializer(
|
---|
24 | Object.assign({}, options, { columns: true })
|
---|
25 | );
|
---|
26 | const finalSource = !!(options && options.finalSource);
|
---|
27 | const { generatedLine, generatedColumn, source } = streamChunks(
|
---|
28 | inputSource,
|
---|
29 | options,
|
---|
30 | (
|
---|
31 | chunk,
|
---|
32 | generatedLine,
|
---|
33 | generatedColumn,
|
---|
34 | sourceIndex,
|
---|
35 | originalLine,
|
---|
36 | originalColumn,
|
---|
37 | nameIndex
|
---|
38 | ) => {
|
---|
39 | if (chunk !== undefined) code += chunk;
|
---|
40 | mappings += addMapping(
|
---|
41 | generatedLine,
|
---|
42 | generatedColumn,
|
---|
43 | sourceIndex,
|
---|
44 | originalLine,
|
---|
45 | originalColumn,
|
---|
46 | nameIndex
|
---|
47 | );
|
---|
48 | return onChunk(
|
---|
49 | finalSource ? undefined : chunk,
|
---|
50 | generatedLine,
|
---|
51 | generatedColumn,
|
---|
52 | sourceIndex,
|
---|
53 | originalLine,
|
---|
54 | originalColumn,
|
---|
55 | nameIndex
|
---|
56 | );
|
---|
57 | },
|
---|
58 | (sourceIndex, source, sourceContent) => {
|
---|
59 | while (sources.length < sourceIndex) {
|
---|
60 | sources.push(null);
|
---|
61 | }
|
---|
62 | sources[sourceIndex] = source;
|
---|
63 | if (sourceContent !== undefined) {
|
---|
64 | while (sourcesContent.length < sourceIndex) {
|
---|
65 | sourcesContent.push(null);
|
---|
66 | }
|
---|
67 | sourcesContent[sourceIndex] = sourceContent;
|
---|
68 | }
|
---|
69 | return onSource(sourceIndex, source, sourceContent);
|
---|
70 | },
|
---|
71 | (nameIndex, name) => {
|
---|
72 | while (names.length < nameIndex) {
|
---|
73 | names.push(null);
|
---|
74 | }
|
---|
75 | names[nameIndex] = name;
|
---|
76 | return onName(nameIndex, name);
|
---|
77 | }
|
---|
78 | );
|
---|
79 | const resultSource = source !== undefined ? source : code;
|
---|
80 | return {
|
---|
81 | result: {
|
---|
82 | generatedLine,
|
---|
83 | generatedColumn,
|
---|
84 | source: finalSource ? resultSource : undefined
|
---|
85 | },
|
---|
86 | source: resultSource,
|
---|
87 | map:
|
---|
88 | mappings.length > 0
|
---|
89 | ? {
|
---|
90 | version: 3,
|
---|
91 | file: "x",
|
---|
92 | mappings,
|
---|
93 | sources,
|
---|
94 | sourcesContent:
|
---|
95 | sourcesContent.length > 0 ? sourcesContent : undefined,
|
---|
96 | names
|
---|
97 | }
|
---|
98 | : null
|
---|
99 | };
|
---|
100 | };
|
---|
101 |
|
---|
102 | module.exports = streamAndGetSourceAndMap;
|
---|