source: frontend/node_modules/webpack-sources/lib/OriginalSource.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: 6.0 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 getGeneratedSourceInfo = require("./helpers/getGeneratedSourceInfo");
11const splitIntoPotentialTokens = require("./helpers/splitIntoPotentialTokens");
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 OriginalSource extends Source {
28 /**
29 * @param {string | Buffer} value value
30 * @param {string} name name
31 */
32 constructor(value, name) {
33 super();
34
35 const isBuffer = Buffer.isBuffer(value);
36
37 /**
38 * @private
39 * @type {undefined | string}
40 */
41 this._value = isBuffer ? undefined : value;
42 /**
43 * @private
44 * @type {undefined | Buffer}
45 */
46 this._valueAsBuffer = isBuffer ? value : undefined;
47 /**
48 * @private
49 * @type {string}
50 */
51 this._name = name;
52 }
53
54 getName() {
55 return this._name;
56 }
57
58 /**
59 * @returns {SourceValue} source
60 */
61 source() {
62 if (this._value === undefined) {
63 const value =
64 /** @type {Buffer} */
65 (this._valueAsBuffer).toString("utf8");
66 if (isDualStringBufferCachingEnabled()) {
67 this._value = value;
68 }
69 return value;
70 }
71 return this._value;
72 }
73
74 /**
75 * @returns {Buffer} buffer
76 */
77 buffer() {
78 if (this._valueAsBuffer === undefined) {
79 const value = Buffer.from(/** @type {string} */ (this._value), "utf8");
80 if (isDualStringBufferCachingEnabled()) {
81 this._valueAsBuffer = value;
82 }
83 return value;
84 }
85 return this._valueAsBuffer;
86 }
87
88 /**
89 * @returns {number} size
90 */
91 size() {
92 if (this._cachedSize !== undefined) return this._cachedSize;
93 if (this._valueAsBuffer !== undefined) {
94 return (this._cachedSize = this._valueAsBuffer.length);
95 }
96 return (this._cachedSize = Buffer.byteLength(
97 /** @type {string} */ (this._value),
98 "utf8",
99 ));
100 }
101
102 /**
103 * @param {MapOptions=} options map options
104 * @returns {RawSourceMap | null} map
105 */
106 map(options) {
107 return getMap(this, options);
108 }
109
110 /**
111 * @param {MapOptions=} options map options
112 * @returns {SourceAndMap} source and map
113 */
114 sourceAndMap(options) {
115 return getSourceAndMap(this, options);
116 }
117
118 /**
119 * @param {Options} options options
120 * @param {OnChunk} onChunk called for each chunk of code
121 * @param {OnSource} onSource called for each source
122 * @param {OnName} _onName called for each name
123 * @returns {GeneratedSourceInfo} generated source info
124 */
125 streamChunks(options, onChunk, onSource, _onName) {
126 if (this._value === undefined) {
127 this._value =
128 /** @type {Buffer} */
129 (this._valueAsBuffer).toString("utf8");
130 }
131 onSource(0, this._name, this._value);
132 const finalSource = Boolean(options && options.finalSource);
133 if (!options || options.columns !== false) {
134 // With column info we need to read all lines and split them
135 const matches = splitIntoPotentialTokens(this._value);
136 let line = 1;
137 let column = 0;
138 if (matches !== null) {
139 for (const match of matches) {
140 const isEndOfLine = match.endsWith("\n");
141 if (isEndOfLine && match.length === 1) {
142 if (!finalSource) onChunk(match, line, column, -1, -1, -1, -1);
143 } else {
144 const chunk = finalSource ? undefined : match;
145 onChunk(chunk, line, column, 0, line, column, -1);
146 }
147 if (isEndOfLine) {
148 line++;
149 column = 0;
150 } else {
151 column += match.length;
152 }
153 }
154 }
155 return {
156 generatedLine: line,
157 generatedColumn: column,
158 source: finalSource ? this._value : undefined,
159 };
160 } else if (finalSource) {
161 // Without column info and with final source we only
162 // need meta info to generate mapping
163 const result = getGeneratedSourceInfo(this._value);
164 const { generatedLine, generatedColumn } = result;
165 if (generatedColumn === 0) {
166 for (
167 let line = 1;
168 line < /** @type {number} */ (generatedLine);
169 line++
170 ) {
171 onChunk(undefined, line, 0, 0, line, 0, -1);
172 }
173 } else {
174 for (
175 let line = 1;
176 line <= /** @type {number} */ (generatedLine);
177 line++
178 ) {
179 onChunk(undefined, line, 0, 0, line, 0, -1);
180 }
181 }
182 return result;
183 }
184 // Without column info, but also without final source.
185 // We only get here when (options.columns === false && !finalSource),
186 // so the source field is always undefined and the chunk arg is always
187 // the line text. Single-pass scan over newlines avoids the
188 // splitIntoLines array allocation.
189 const value = this._value;
190 const len = value.length;
191 if (len === 0) {
192 return { generatedLine: 1, generatedColumn: 0, source: undefined };
193 }
194 let line = 1;
195 let i = 0;
196 while (i < len) {
197 const n = value.indexOf("\n", i);
198 if (n === -1) {
199 const lastLine = i === 0 ? value : value.slice(i);
200 onChunk(lastLine, line, 0, 0, line, 0, -1);
201 return {
202 generatedLine: line,
203 generatedColumn: lastLine.length,
204 source: undefined,
205 };
206 }
207 const chunk = n === i ? "\n" : value.slice(i, n + 1);
208 onChunk(chunk, line, 0, 0, line, 0, -1);
209 line++;
210 i = n + 1;
211 }
212 // Source ended with a newline.
213 return { generatedLine: line, generatedColumn: 0, source: undefined };
214 }
215
216 /**
217 * @param {HashLike} hash hash
218 * @returns {void}
219 */
220 updateHash(hash) {
221 hash.update("OriginalSource");
222 hash.update(this.buffer());
223 hash.update(this._name || "");
224 }
225}
226
227module.exports = OriginalSource;
Note: See TracBrowser for help on using the repository browser.