source: frontend/node_modules/webpack-sources/lib/SourceMapSource.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 9.8 KB
RevLine 
[9af201e]1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const Source = require("./Source");
9const { getMap, getSourceAndMap } = require("./helpers/getFromStreamChunks");
10const streamChunksOfCombinedSourceMap = require("./helpers/streamChunksOfCombinedSourceMap");
11const streamChunksOfSourceMap = require("./helpers/streamChunksOfSourceMap");
12const {
13 isDualStringBufferCachingEnabled,
14} = require("./helpers/stringBufferUtils");
15
16/** @typedef {import("./Source").HashLike} HashLike */
17/** @typedef {import("./Source").MapOptions} MapOptions */
18/** @typedef {import("./Source").RawSourceMap} RawSourceMap */
19/** @typedef {import("./Source").SourceAndMap} SourceAndMap */
20/** @typedef {import("./Source").SourceValue} SourceValue */
21/** @typedef {import("./helpers/getGeneratedSourceInfo").GeneratedSourceInfo} GeneratedSourceInfo */
22/** @typedef {import("./helpers/streamChunks").OnChunk} OnChunk */
23/** @typedef {import("./helpers/streamChunks").OnName} OnName */
24/** @typedef {import("./helpers/streamChunks").OnSource} OnSource */
25/** @typedef {import("./helpers/streamChunks").Options} Options */
26
27class SourceMapSource extends Source {
28 /**
29 * @param {string | Buffer} value value
30 * @param {string} name name
31 * @param {string | Buffer | RawSourceMap=} sourceMap source map
32 * @param {SourceValue=} originalSource original source
33 * @param {(null | string | Buffer | RawSourceMap)=} innerSourceMap inner source map
34 * @param {boolean=} removeOriginalSource do remove original source
35 */
36 constructor(
37 value,
38 name,
39 sourceMap,
40 originalSource,
41 innerSourceMap,
42 removeOriginalSource,
43 ) {
44 super();
45 const valueIsBuffer = Buffer.isBuffer(value);
46 /**
47 * @private
48 * @type {undefined | string}
49 */
50 this._valueAsString = valueIsBuffer ? undefined : value;
51 /**
52 * @private
53 * @type {undefined | Buffer}
54 */
55 this._valueAsBuffer = valueIsBuffer ? value : undefined;
56
57 this._name = name;
58
59 this._hasSourceMap = Boolean(sourceMap);
60 const sourceMapIsBuffer = Buffer.isBuffer(sourceMap);
61 const sourceMapIsString = typeof sourceMap === "string";
62 /**
63 * @private
64 * @type {undefined | RawSourceMap}
65 */
66 this._sourceMapAsObject =
67 sourceMapIsBuffer || sourceMapIsString ? undefined : sourceMap;
68 /**
69 * @private
70 * @type {undefined | string}
71 */
72 this._sourceMapAsString = sourceMapIsString ? sourceMap : undefined;
73 /**
74 * @private
75 * @type {undefined | Buffer}
76 */
77 this._sourceMapAsBuffer = sourceMapIsBuffer ? sourceMap : undefined;
78
79 this._hasOriginalSource = Boolean(originalSource);
80 const originalSourceIsBuffer = Buffer.isBuffer(originalSource);
81 this._originalSourceAsString = originalSourceIsBuffer
82 ? undefined
83 : originalSource;
84 this._originalSourceAsBuffer = originalSourceIsBuffer
85 ? originalSource
86 : undefined;
87
88 this._hasInnerSourceMap = Boolean(innerSourceMap);
89 const innerSourceMapIsBuffer = Buffer.isBuffer(innerSourceMap);
90 const innerSourceMapIsString = typeof innerSourceMap === "string";
91 /**
92 * @private
93 * @type {undefined | RawSourceMap}
94 */
95
96 this._innerSourceMapAsObject =
97 innerSourceMapIsBuffer || innerSourceMapIsString
98 ? undefined
99 : innerSourceMap || undefined;
100 /**
101 * @private
102 * @type {undefined | string}
103 */
104 this._innerSourceMapAsString = innerSourceMapIsString
105 ? innerSourceMap
106 : undefined;
107 /**
108 * @private
109 * @type {undefined | Buffer}
110 */
111 this._innerSourceMapAsBuffer = innerSourceMapIsBuffer
112 ? innerSourceMap
113 : undefined;
114
115 this._removeOriginalSource = removeOriginalSource;
116 }
117
118 /**
119 * @returns {[Buffer, string, Buffer, Buffer | undefined, Buffer | undefined, boolean | undefined]} args
120 */
121 getArgsAsBuffers() {
122 return [
123 this.buffer(),
124 this._name,
125 this._sourceMapBuffer(),
126 this._originalSourceBuffer(),
127 this._innerSourceMapBuffer(),
128 this._removeOriginalSource,
129 ];
130 }
131
132 /**
133 * @returns {Buffer} buffer
134 */
135 buffer() {
136 if (this._valueAsBuffer === undefined) {
137 const value = Buffer.from(
138 /** @type {string} */ (this._valueAsString),
139 "utf8",
140 );
141 if (isDualStringBufferCachingEnabled()) {
142 this._valueAsBuffer = value;
143 }
144 return value;
145 }
146 return this._valueAsBuffer;
147 }
148
149 /**
150 * @returns {number} size
151 */
152 size() {
153 if (this._cachedSize !== undefined) return this._cachedSize;
154 if (this._valueAsBuffer !== undefined) {
155 return (this._cachedSize = this._valueAsBuffer.length);
156 }
157 return (this._cachedSize = Buffer.byteLength(
158 /** @type {string} */ (this._valueAsString),
159 "utf8",
160 ));
161 }
162
163 /**
164 * @returns {SourceValue} source
165 */
166 source() {
167 if (this._valueAsString === undefined) {
168 const value =
169 /** @type {Buffer} */
170 (this._valueAsBuffer).toString("utf8");
171 if (isDualStringBufferCachingEnabled()) {
172 this._valueAsString = value;
173 }
174 return value;
175 }
176 return this._valueAsString;
177 }
178
179 /**
180 * @private
181 * @returns {undefined | Buffer} buffer
182 */
183 _originalSourceBuffer() {
184 if (this._originalSourceAsBuffer === undefined && this._hasOriginalSource) {
185 const value = Buffer.from(
186 /** @type {string} */
187 (this._originalSourceAsString),
188 "utf8",
189 );
190 if (isDualStringBufferCachingEnabled()) {
191 this._originalSourceAsBuffer = value;
192 }
193 return value;
194 }
195 return this._originalSourceAsBuffer;
196 }
197
198 _originalSourceString() {
199 if (this._originalSourceAsString === undefined && this._hasOriginalSource) {
200 const value =
201 /** @type {Buffer} */
202 (this._originalSourceAsBuffer).toString("utf8");
203 if (isDualStringBufferCachingEnabled()) {
204 this._originalSourceAsString = value;
205 }
206 return value;
207 }
208 return this._originalSourceAsString;
209 }
210
211 _innerSourceMapObject() {
212 if (this._innerSourceMapAsObject === undefined && this._hasInnerSourceMap) {
213 const value = JSON.parse(this._innerSourceMapString());
214 if (isDualStringBufferCachingEnabled()) {
215 this._innerSourceMapAsObject = value;
216 }
217 return value;
218 }
219 return this._innerSourceMapAsObject;
220 }
221
222 _innerSourceMapBuffer() {
223 if (this._innerSourceMapAsBuffer === undefined && this._hasInnerSourceMap) {
224 const value = Buffer.from(this._innerSourceMapString(), "utf8");
225 if (isDualStringBufferCachingEnabled()) {
226 this._innerSourceMapAsBuffer = value;
227 }
228 return value;
229 }
230 return this._innerSourceMapAsBuffer;
231 }
232
233 /**
234 * @private
235 * @returns {string} result
236 */
237 _innerSourceMapString() {
238 if (this._innerSourceMapAsString === undefined && this._hasInnerSourceMap) {
239 if (this._innerSourceMapAsBuffer !== undefined) {
240 const value = this._innerSourceMapAsBuffer.toString("utf8");
241 if (isDualStringBufferCachingEnabled()) {
242 this._innerSourceMapAsString = value;
243 }
244 return value;
245 }
246 const value = JSON.stringify(this._innerSourceMapAsObject);
247 if (isDualStringBufferCachingEnabled()) {
248 this._innerSourceMapAsString = value;
249 }
250 return value;
251 }
252 return /** @type {string} */ (this._innerSourceMapAsString);
253 }
254
255 _sourceMapObject() {
256 if (this._sourceMapAsObject === undefined) {
257 const value = JSON.parse(this._sourceMapString());
258 if (isDualStringBufferCachingEnabled()) {
259 this._sourceMapAsObject = value;
260 }
261 return value;
262 }
263 return this._sourceMapAsObject;
264 }
265
266 _sourceMapBuffer() {
267 if (this._sourceMapAsBuffer === undefined) {
268 const value = Buffer.from(this._sourceMapString(), "utf8");
269 if (isDualStringBufferCachingEnabled()) {
270 this._sourceMapAsBuffer = value;
271 }
272 return value;
273 }
274 return this._sourceMapAsBuffer;
275 }
276
277 _sourceMapString() {
278 if (this._sourceMapAsString === undefined) {
279 if (this._sourceMapAsBuffer !== undefined) {
280 const value = this._sourceMapAsBuffer.toString("utf8");
281 if (isDualStringBufferCachingEnabled()) {
282 this._sourceMapAsString = value;
283 }
284 return value;
285 }
286 const value = JSON.stringify(this._sourceMapAsObject);
287 if (isDualStringBufferCachingEnabled()) {
288 this._sourceMapAsString = value;
289 }
290 return value;
291 }
292 return this._sourceMapAsString;
293 }
294
295 /**
296 * @param {MapOptions=} options map options
297 * @returns {RawSourceMap | null} map
298 */
299 map(options) {
300 if (!this._hasInnerSourceMap) {
301 return this._sourceMapObject();
302 }
303 return getMap(this, options);
304 }
305
306 /**
307 * @param {MapOptions=} options map options
308 * @returns {SourceAndMap} source and map
309 */
310 sourceAndMap(options) {
311 if (!this._hasInnerSourceMap) {
312 return {
313 source: this.source(),
314 map: this._sourceMapObject(),
315 };
316 }
317 return getSourceAndMap(this, options);
318 }
319
320 /**
321 * @param {Options} options options
322 * @param {OnChunk} onChunk called for each chunk of code
323 * @param {OnSource} onSource called for each source
324 * @param {OnName} onName called for each name
325 * @returns {GeneratedSourceInfo} generated source info
326 */
327 streamChunks(options, onChunk, onSource, onName) {
328 if (this._hasInnerSourceMap) {
329 return streamChunksOfCombinedSourceMap(
330 /** @type {string} */
331 (this.source()),
332 this._sourceMapObject(),
333 this._name,
334 /** @type {string} */
335 (this._originalSourceString()),
336 this._innerSourceMapObject(),
337 this._removeOriginalSource,
338 onChunk,
339 onSource,
340 onName,
341 Boolean(options && options.finalSource),
342 Boolean(options && options.columns !== false),
343 );
344 }
345 return streamChunksOfSourceMap(
346 /** @type {string} */
347 (this.source()),
348 this._sourceMapObject(),
349 onChunk,
350 onSource,
351 onName,
352 Boolean(options && options.finalSource),
353 Boolean(options && options.columns !== false),
354 );
355 }
356
357 /**
358 * @param {HashLike} hash hash
359 * @returns {void}
360 */
361 updateHash(hash) {
362 hash.update("SourceMapSource");
363 hash.update(this.buffer());
364 hash.update(this._sourceMapBuffer());
365
366 if (this._hasOriginalSource) {
367 hash.update(
368 /** @type {Buffer} */
369 (this._originalSourceBuffer()),
370 );
371 }
372
373 if (this._hasInnerSourceMap) {
374 hash.update(
375 /** @type {Buffer} */
376 (this._innerSourceMapBuffer()),
377 );
378 }
379
380 hash.update(this._removeOriginalSource ? "true" : "false");
381 }
382}
383
384module.exports = SourceMapSource;
Note: See TracBrowser for help on using the repository browser.