1 |
|
2 |
|
3 |
|
4 |
|
5 | "use strict";
|
6 |
|
7 | const Source = require("./Source");
|
8 | const streamChunksOfSourceMap = require("./helpers/streamChunksOfSourceMap");
|
9 | const streamChunksOfCombinedSourceMap = require("./helpers/streamChunksOfCombinedSourceMap");
|
10 | const { getMap, getSourceAndMap } = require("./helpers/getFromStreamChunks");
|
11 |
|
12 | class SourceMapSource extends Source {
|
13 | constructor(
|
14 | value,
|
15 | name,
|
16 | sourceMap,
|
17 | originalSource,
|
18 | innerSourceMap,
|
19 | removeOriginalSource
|
20 | ) {
|
21 | super();
|
22 | const valueIsBuffer = Buffer.isBuffer(value);
|
23 | this._valueAsString = valueIsBuffer ? undefined : value;
|
24 | this._valueAsBuffer = valueIsBuffer ? value : undefined;
|
25 |
|
26 | this._name = name;
|
27 |
|
28 | this._hasSourceMap = !!sourceMap;
|
29 | const sourceMapIsBuffer = Buffer.isBuffer(sourceMap);
|
30 | const sourceMapIsString = typeof sourceMap === "string";
|
31 | this._sourceMapAsObject =
|
32 | sourceMapIsBuffer || sourceMapIsString ? undefined : sourceMap;
|
33 | this._sourceMapAsString = sourceMapIsString ? sourceMap : undefined;
|
34 | this._sourceMapAsBuffer = sourceMapIsBuffer ? sourceMap : undefined;
|
35 |
|
36 | this._hasOriginalSource = !!originalSource;
|
37 | const originalSourceIsBuffer = Buffer.isBuffer(originalSource);
|
38 | this._originalSourceAsString = originalSourceIsBuffer
|
39 | ? undefined
|
40 | : originalSource;
|
41 | this._originalSourceAsBuffer = originalSourceIsBuffer
|
42 | ? originalSource
|
43 | : undefined;
|
44 |
|
45 | this._hasInnerSourceMap = !!innerSourceMap;
|
46 | const innerSourceMapIsBuffer = Buffer.isBuffer(innerSourceMap);
|
47 | const innerSourceMapIsString = typeof innerSourceMap === "string";
|
48 | this._innerSourceMapAsObject =
|
49 | innerSourceMapIsBuffer || innerSourceMapIsString
|
50 | ? undefined
|
51 | : innerSourceMap;
|
52 | this._innerSourceMapAsString = innerSourceMapIsString
|
53 | ? innerSourceMap
|
54 | : undefined;
|
55 | this._innerSourceMapAsBuffer = innerSourceMapIsBuffer
|
56 | ? innerSourceMap
|
57 | : undefined;
|
58 |
|
59 | this._removeOriginalSource = removeOriginalSource;
|
60 | }
|
61 |
|
62 | _ensureValueBuffer() {
|
63 | if (this._valueAsBuffer === undefined) {
|
64 | this._valueAsBuffer = Buffer.from(this._valueAsString, "utf-8");
|
65 | }
|
66 | }
|
67 |
|
68 | _ensureValueString() {
|
69 | if (this._valueAsString === undefined) {
|
70 | this._valueAsString = this._valueAsBuffer.toString("utf-8");
|
71 | }
|
72 | }
|
73 |
|
74 | _ensureOriginalSourceBuffer() {
|
75 | if (this._originalSourceAsBuffer === undefined && this._hasOriginalSource) {
|
76 | this._originalSourceAsBuffer = Buffer.from(
|
77 | this._originalSourceAsString,
|
78 | "utf-8"
|
79 | );
|
80 | }
|
81 | }
|
82 |
|
83 | _ensureOriginalSourceString() {
|
84 | if (this._originalSourceAsString === undefined && this._hasOriginalSource) {
|
85 | this._originalSourceAsString = this._originalSourceAsBuffer.toString(
|
86 | "utf-8"
|
87 | );
|
88 | }
|
89 | }
|
90 |
|
91 | _ensureInnerSourceMapObject() {
|
92 | if (this._innerSourceMapAsObject === undefined && this._hasInnerSourceMap) {
|
93 | this._ensureInnerSourceMapString();
|
94 | this._innerSourceMapAsObject = JSON.parse(this._innerSourceMapAsString);
|
95 | }
|
96 | }
|
97 |
|
98 | _ensureInnerSourceMapBuffer() {
|
99 | if (this._innerSourceMapAsBuffer === undefined && this._hasInnerSourceMap) {
|
100 | this._ensureInnerSourceMapString();
|
101 | this._innerSourceMapAsBuffer = Buffer.from(
|
102 | this._innerSourceMapAsString,
|
103 | "utf-8"
|
104 | );
|
105 | }
|
106 | }
|
107 |
|
108 | _ensureInnerSourceMapString() {
|
109 | if (this._innerSourceMapAsString === undefined && this._hasInnerSourceMap) {
|
110 | if (this._innerSourceMapAsBuffer !== undefined) {
|
111 | this._innerSourceMapAsString = this._innerSourceMapAsBuffer.toString(
|
112 | "utf-8"
|
113 | );
|
114 | } else {
|
115 | this._innerSourceMapAsString = JSON.stringify(
|
116 | this._innerSourceMapAsObject
|
117 | );
|
118 | }
|
119 | }
|
120 | }
|
121 |
|
122 | _ensureSourceMapObject() {
|
123 | if (this._sourceMapAsObject === undefined) {
|
124 | this._ensureSourceMapString();
|
125 | this._sourceMapAsObject = JSON.parse(this._sourceMapAsString);
|
126 | }
|
127 | }
|
128 |
|
129 | _ensureSourceMapBuffer() {
|
130 | if (this._sourceMapAsBuffer === undefined) {
|
131 | this._ensureSourceMapString();
|
132 | this._sourceMapAsBuffer = Buffer.from(this._sourceMapAsString, "utf-8");
|
133 | }
|
134 | }
|
135 |
|
136 | _ensureSourceMapString() {
|
137 | if (this._sourceMapAsString === undefined) {
|
138 | if (this._sourceMapAsBuffer !== undefined) {
|
139 | this._sourceMapAsString = this._sourceMapAsBuffer.toString("utf-8");
|
140 | } else {
|
141 | this._sourceMapAsString = JSON.stringify(this._sourceMapAsObject);
|
142 | }
|
143 | }
|
144 | }
|
145 |
|
146 | getArgsAsBuffers() {
|
147 | this._ensureValueBuffer();
|
148 | this._ensureSourceMapBuffer();
|
149 | this._ensureOriginalSourceBuffer();
|
150 | this._ensureInnerSourceMapBuffer();
|
151 | return [
|
152 | this._valueAsBuffer,
|
153 | this._name,
|
154 | this._sourceMapAsBuffer,
|
155 | this._originalSourceAsBuffer,
|
156 | this._innerSourceMapAsBuffer,
|
157 | this._removeOriginalSource
|
158 | ];
|
159 | }
|
160 |
|
161 | source() {
|
162 | this._ensureValueString();
|
163 | return this._valueAsString;
|
164 | }
|
165 |
|
166 | map(options) {
|
167 | if (!this._hasInnerSourceMap) {
|
168 | this._ensureSourceMapObject();
|
169 | return this._sourceMapAsObject;
|
170 | }
|
171 | return getMap(this, options);
|
172 | }
|
173 |
|
174 | sourceAndMap(options) {
|
175 | if (!this._hasInnerSourceMap) {
|
176 | this._ensureValueString();
|
177 | this._ensureSourceMapObject();
|
178 | return {
|
179 | source: this._valueAsString,
|
180 | map: this._sourceMapAsObject
|
181 | };
|
182 | }
|
183 | return getSourceAndMap(this, options);
|
184 | }
|
185 |
|
186 | streamChunks(options, onChunk, onSource, onName) {
|
187 | this._ensureValueString();
|
188 | this._ensureSourceMapObject();
|
189 | this._ensureOriginalSourceString();
|
190 | if (this._hasInnerSourceMap) {
|
191 | this._ensureInnerSourceMapObject();
|
192 | return streamChunksOfCombinedSourceMap(
|
193 | this._valueAsString,
|
194 | this._sourceMapAsObject,
|
195 | this._name,
|
196 | this._originalSourceAsString,
|
197 | this._innerSourceMapAsObject,
|
198 | this._removeOriginalSource,
|
199 | onChunk,
|
200 | onSource,
|
201 | onName,
|
202 | !!(options && options.finalSource)
|
203 | );
|
204 | } else {
|
205 | return streamChunksOfSourceMap(
|
206 | this._valueAsString,
|
207 | this._sourceMapAsObject,
|
208 | onChunk,
|
209 | onSource,
|
210 | onName,
|
211 | !!(options && options.finalSource)
|
212 | );
|
213 | }
|
214 | }
|
215 |
|
216 | updateHash(hash) {
|
217 | this._ensureValueBuffer();
|
218 | this._ensureSourceMapBuffer();
|
219 | this._ensureOriginalSourceBuffer();
|
220 | this._ensureInnerSourceMapBuffer();
|
221 |
|
222 | hash.update("SourceMapSource");
|
223 |
|
224 | hash.update(this._valueAsBuffer);
|
225 |
|
226 | hash.update(this._sourceMapAsBuffer);
|
227 |
|
228 | if (this._hasOriginalSource) {
|
229 | hash.update(this._originalSourceAsBuffer);
|
230 | }
|
231 |
|
232 | if (this._hasInnerSourceMap) {
|
233 | hash.update(this._innerSourceMapAsBuffer);
|
234 | }
|
235 |
|
236 | hash.update(this._removeOriginalSource ? "true" : "false");
|
237 | }
|
238 | }
|
239 |
|
240 | module.exports = SourceMapSource;
|