@depup/webpack-sources 3.3.4-depup.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,398 @@
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 RawSource = require("./RawSource");
9
+ const Source = require("./Source");
10
+ const { getMap, getSourceAndMap } = require("./helpers/getFromStreamChunks");
11
+ const streamChunks = require("./helpers/streamChunks");
12
+
13
+ /** @typedef {import("./CompatSource").SourceLike} SourceLike */
14
+ /** @typedef {import("./Source").HashLike} HashLike */
15
+ /** @typedef {import("./Source").MapOptions} MapOptions */
16
+ /** @typedef {import("./Source").RawSourceMap} RawSourceMap */
17
+ /** @typedef {import("./Source").SourceAndMap} SourceAndMap */
18
+ /** @typedef {import("./Source").SourceValue} SourceValue */
19
+ /** @typedef {import("./helpers/getGeneratedSourceInfo").GeneratedSourceInfo} GeneratedSourceInfo */
20
+ /** @typedef {import("./helpers/streamChunks").OnChunk} OnChunk */
21
+ /** @typedef {import("./helpers/streamChunks").OnName} OnName */
22
+ /** @typedef {import("./helpers/streamChunks").OnSource} OnSource */
23
+ /** @typedef {import("./helpers/streamChunks").Options} Options */
24
+
25
+ /** @typedef {string | Source | SourceLike} Child */
26
+
27
+ const stringsAsRawSources = new WeakSet();
28
+
29
+ class ConcatSource extends Source {
30
+ /**
31
+ * @param {Child[]} args children
32
+ */
33
+ constructor(...args) {
34
+ super();
35
+ /**
36
+ * @private
37
+ * @type {Child[]}
38
+ */
39
+ this._children = [];
40
+
41
+ for (const item of args) {
42
+ if (item instanceof ConcatSource) {
43
+ for (const child of item._children) {
44
+ this._children.push(child);
45
+ }
46
+ } else {
47
+ this._children.push(item);
48
+ }
49
+ }
50
+
51
+ /**
52
+ * @private
53
+ * @type {boolean}
54
+ */
55
+ this._isOptimized = args.length === 0;
56
+ }
57
+
58
+ /**
59
+ * @returns {Source[]} children
60
+ */
61
+ getChildren() {
62
+ if (!this._isOptimized) this._optimize();
63
+ return /** @type {Source[]} */ (this._children);
64
+ }
65
+
66
+ /**
67
+ * @param {Child} item item
68
+ * @returns {void}
69
+ */
70
+ add(item) {
71
+ if (item instanceof ConcatSource) {
72
+ for (const child of item._children) {
73
+ this._children.push(child);
74
+ }
75
+ } else {
76
+ this._children.push(item);
77
+ }
78
+ this._isOptimized = false;
79
+ }
80
+
81
+ /**
82
+ * @param {Child[]} items items
83
+ * @returns {void}
84
+ */
85
+ addAllSkipOptimizing(items) {
86
+ for (const item of items) {
87
+ this._children.push(item);
88
+ }
89
+ }
90
+
91
+ buffer() {
92
+ if (!this._isOptimized) this._optimize();
93
+ /** @type {Buffer[]} */
94
+ const buffers = [];
95
+ for (const child of /** @type {SourceLike[]} */ (this._children)) {
96
+ if (typeof child.buffer === "function") {
97
+ buffers.push(child.buffer());
98
+ } else {
99
+ const bufferOrString = child.source();
100
+ if (Buffer.isBuffer(bufferOrString)) {
101
+ buffers.push(bufferOrString);
102
+ } else {
103
+ // This will not happen
104
+ buffers.push(Buffer.from(bufferOrString, "utf8"));
105
+ }
106
+ }
107
+ }
108
+ return Buffer.concat(buffers);
109
+ }
110
+
111
+ /**
112
+ * @returns {SourceValue} source
113
+ */
114
+ source() {
115
+ if (!this._isOptimized) this._optimize();
116
+ let source = "";
117
+ for (const child of this._children) {
118
+ source += /** @type {Source} */ (child).source();
119
+ }
120
+ return source;
121
+ }
122
+
123
+ size() {
124
+ if (!this._isOptimized) this._optimize();
125
+ let size = 0;
126
+ for (const child of this._children) {
127
+ size += /** @type {Source} */ (child).size();
128
+ }
129
+ return size;
130
+ }
131
+
132
+ /**
133
+ * @param {MapOptions=} options map options
134
+ * @returns {RawSourceMap | null} map
135
+ */
136
+ map(options) {
137
+ return getMap(this, options);
138
+ }
139
+
140
+ /**
141
+ * @param {MapOptions=} options map options
142
+ * @returns {SourceAndMap} source and map
143
+ */
144
+ sourceAndMap(options) {
145
+ return getSourceAndMap(this, options);
146
+ }
147
+
148
+ /**
149
+ * @param {Options} options options
150
+ * @param {OnChunk} onChunk called for each chunk of code
151
+ * @param {OnSource} onSource called for each source
152
+ * @param {OnName} onName called for each name
153
+ * @returns {GeneratedSourceInfo} generated source info
154
+ */
155
+ streamChunks(options, onChunk, onSource, onName) {
156
+ if (!this._isOptimized) this._optimize();
157
+ if (this._children.length === 1) {
158
+ return /** @type {ConcatSource[]} */ (this._children)[0].streamChunks(
159
+ options,
160
+ onChunk,
161
+ onSource,
162
+ onName,
163
+ );
164
+ }
165
+ let currentLineOffset = 0;
166
+ let currentColumnOffset = 0;
167
+ const sourceMapping = new Map();
168
+ const nameMapping = new Map();
169
+ const finalSource = Boolean(options && options.finalSource);
170
+ let code = "";
171
+ let needToCloseMapping = false;
172
+ for (const item of /** @type {Source[]} */ (this._children)) {
173
+ /** @type {number[]} */
174
+ const sourceIndexMapping = [];
175
+ /** @type {number[]} */
176
+ const nameIndexMapping = [];
177
+ let lastMappingLine = 0;
178
+ const { generatedLine, generatedColumn, source } = streamChunks(
179
+ item,
180
+ options,
181
+ // eslint-disable-next-line no-loop-func
182
+ (
183
+ chunk,
184
+ generatedLine,
185
+ generatedColumn,
186
+ sourceIndex,
187
+ originalLine,
188
+ originalColumn,
189
+ nameIndex,
190
+ ) => {
191
+ const line = generatedLine + currentLineOffset;
192
+ const column =
193
+ generatedLine === 1
194
+ ? generatedColumn + currentColumnOffset
195
+ : generatedColumn;
196
+ if (needToCloseMapping) {
197
+ if (generatedLine !== 1 || generatedColumn !== 0) {
198
+ onChunk(
199
+ undefined,
200
+ currentLineOffset + 1,
201
+ currentColumnOffset,
202
+ -1,
203
+ -1,
204
+ -1,
205
+ -1,
206
+ );
207
+ }
208
+ needToCloseMapping = false;
209
+ }
210
+ const resultSourceIndex =
211
+ sourceIndex < 0 || sourceIndex >= sourceIndexMapping.length
212
+ ? -1
213
+ : sourceIndexMapping[sourceIndex];
214
+ const resultNameIndex =
215
+ nameIndex < 0 || nameIndex >= nameIndexMapping.length
216
+ ? -1
217
+ : nameIndexMapping[nameIndex];
218
+ lastMappingLine = resultSourceIndex < 0 ? 0 : generatedLine;
219
+ let _chunk;
220
+ // When using finalSource, we process the entire source code at once at the end, rather than chunk by chunk
221
+ if (finalSource) {
222
+ if (chunk !== undefined) code += chunk;
223
+ } else {
224
+ _chunk = chunk;
225
+ }
226
+ if (resultSourceIndex < 0) {
227
+ onChunk(_chunk, line, column, -1, -1, -1, -1);
228
+ } else {
229
+ onChunk(
230
+ _chunk,
231
+ line,
232
+ column,
233
+ resultSourceIndex,
234
+ originalLine,
235
+ originalColumn,
236
+ resultNameIndex,
237
+ );
238
+ }
239
+ },
240
+ (i, source, sourceContent) => {
241
+ let globalIndex = sourceMapping.get(source);
242
+ if (globalIndex === undefined) {
243
+ sourceMapping.set(source, (globalIndex = sourceMapping.size));
244
+ onSource(globalIndex, source, sourceContent);
245
+ }
246
+ sourceIndexMapping[i] = globalIndex;
247
+ },
248
+ (i, name) => {
249
+ let globalIndex = nameMapping.get(name);
250
+ if (globalIndex === undefined) {
251
+ nameMapping.set(name, (globalIndex = nameMapping.size));
252
+ onName(globalIndex, name);
253
+ }
254
+ nameIndexMapping[i] = globalIndex;
255
+ },
256
+ );
257
+ if (source !== undefined) code += source;
258
+ if (
259
+ needToCloseMapping &&
260
+ (generatedLine !== 1 || generatedColumn !== 0)
261
+ ) {
262
+ onChunk(
263
+ undefined,
264
+ currentLineOffset + 1,
265
+ currentColumnOffset,
266
+ -1,
267
+ -1,
268
+ -1,
269
+ -1,
270
+ );
271
+ needToCloseMapping = false;
272
+ }
273
+ if (/** @type {number} */ (generatedLine) > 1) {
274
+ currentColumnOffset = /** @type {number} */ (generatedColumn);
275
+ } else {
276
+ currentColumnOffset += /** @type {number} */ (generatedColumn);
277
+ }
278
+ needToCloseMapping =
279
+ needToCloseMapping ||
280
+ (finalSource && lastMappingLine === generatedLine);
281
+ currentLineOffset += /** @type {number} */ (generatedLine) - 1;
282
+ }
283
+ return {
284
+ generatedLine: currentLineOffset + 1,
285
+ generatedColumn: currentColumnOffset,
286
+ source: finalSource ? code : undefined,
287
+ };
288
+ }
289
+
290
+ /**
291
+ * @param {HashLike} hash hash
292
+ * @returns {void}
293
+ */
294
+ updateHash(hash) {
295
+ if (!this._isOptimized) this._optimize();
296
+ hash.update("ConcatSource");
297
+ for (const item of this._children) {
298
+ /** @type {Source} */
299
+ (item).updateHash(hash);
300
+ }
301
+ }
302
+
303
+ _optimize() {
304
+ const newChildren = [];
305
+ let currentString;
306
+ /** @type {undefined | string | [string, string] | SourceLike} */
307
+ let currentRawSources;
308
+ /**
309
+ * @param {string} string string
310
+ * @returns {void}
311
+ */
312
+ const addStringToRawSources = (string) => {
313
+ if (currentRawSources === undefined) {
314
+ currentRawSources = string;
315
+ } else if (Array.isArray(currentRawSources)) {
316
+ currentRawSources.push(string);
317
+ } else {
318
+ currentRawSources = [
319
+ typeof currentRawSources === "string"
320
+ ? currentRawSources
321
+ : /** @type {string} */ (currentRawSources.source()),
322
+ string,
323
+ ];
324
+ }
325
+ };
326
+ /**
327
+ * @param {SourceLike} source source
328
+ * @returns {void}
329
+ */
330
+ const addSourceToRawSources = (source) => {
331
+ if (currentRawSources === undefined) {
332
+ currentRawSources = source;
333
+ } else if (Array.isArray(currentRawSources)) {
334
+ currentRawSources.push(
335
+ /** @type {string} */
336
+ (source.source()),
337
+ );
338
+ } else {
339
+ currentRawSources = [
340
+ typeof currentRawSources === "string"
341
+ ? currentRawSources
342
+ : /** @type {string} */ (currentRawSources.source()),
343
+ /** @type {string} */
344
+ (source.source()),
345
+ ];
346
+ }
347
+ };
348
+ const mergeRawSources = () => {
349
+ if (Array.isArray(currentRawSources)) {
350
+ const rawSource = new RawSource(currentRawSources.join(""));
351
+ stringsAsRawSources.add(rawSource);
352
+ newChildren.push(rawSource);
353
+ } else if (typeof currentRawSources === "string") {
354
+ const rawSource = new RawSource(currentRawSources);
355
+ stringsAsRawSources.add(rawSource);
356
+ newChildren.push(rawSource);
357
+ } else {
358
+ newChildren.push(currentRawSources);
359
+ }
360
+ };
361
+ for (const child of this._children) {
362
+ if (typeof child === "string") {
363
+ if (currentString === undefined) {
364
+ currentString = child;
365
+ } else {
366
+ currentString += child;
367
+ }
368
+ } else {
369
+ if (currentString !== undefined) {
370
+ addStringToRawSources(currentString);
371
+ currentString = undefined;
372
+ }
373
+ if (stringsAsRawSources.has(child)) {
374
+ addSourceToRawSources(
375
+ /** @type {SourceLike} */
376
+ (child),
377
+ );
378
+ } else {
379
+ if (currentRawSources !== undefined) {
380
+ mergeRawSources();
381
+ currentRawSources = undefined;
382
+ }
383
+ newChildren.push(child);
384
+ }
385
+ }
386
+ }
387
+ if (currentString !== undefined) {
388
+ addStringToRawSources(currentString);
389
+ }
390
+ if (currentRawSources !== undefined) {
391
+ mergeRawSources();
392
+ }
393
+ this._children = newChildren;
394
+ this._isOptimized = true;
395
+ }
396
+ }
397
+
398
+ module.exports = ConcatSource;
@@ -0,0 +1,202 @@
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 Source = require("./Source");
9
+ const { getMap, getSourceAndMap } = require("./helpers/getFromStreamChunks");
10
+ const getGeneratedSourceInfo = require("./helpers/getGeneratedSourceInfo");
11
+ const splitIntoLines = require("./helpers/splitIntoLines");
12
+ const splitIntoPotentialTokens = require("./helpers/splitIntoPotentialTokens");
13
+ const {
14
+ isDualStringBufferCachingEnabled,
15
+ } = require("./helpers/stringBufferUtils");
16
+
17
+ /** @typedef {import("./Source").HashLike} HashLike */
18
+ /** @typedef {import("./Source").MapOptions} MapOptions */
19
+ /** @typedef {import("./Source").RawSourceMap} RawSourceMap */
20
+ /** @typedef {import("./Source").SourceAndMap} SourceAndMap */
21
+ /** @typedef {import("./Source").SourceValue} SourceValue */
22
+ /** @typedef {import("./helpers/getGeneratedSourceInfo").GeneratedSourceInfo} GeneratedSourceInfo */
23
+ /** @typedef {import("./helpers/streamChunks").OnChunk} OnChunk */
24
+ /** @typedef {import("./helpers/streamChunks").OnName} OnName */
25
+ /** @typedef {import("./helpers/streamChunks").OnSource} OnSource */
26
+ /** @typedef {import("./helpers/streamChunks").Options} Options */
27
+
28
+ class OriginalSource extends Source {
29
+ /**
30
+ * @param {string | Buffer} value value
31
+ * @param {string} name name
32
+ */
33
+ constructor(value, name) {
34
+ super();
35
+
36
+ const isBuffer = Buffer.isBuffer(value);
37
+
38
+ /**
39
+ * @private
40
+ * @type {undefined | string}
41
+ */
42
+ this._value = isBuffer ? undefined : value;
43
+ /**
44
+ * @private
45
+ * @type {undefined | Buffer}
46
+ */
47
+ this._valueAsBuffer = isBuffer ? value : undefined;
48
+ /**
49
+ * @private
50
+ * @type {string}
51
+ */
52
+ this._name = name;
53
+ }
54
+
55
+ getName() {
56
+ return this._name;
57
+ }
58
+
59
+ /**
60
+ * @returns {SourceValue} source
61
+ */
62
+ source() {
63
+ if (this._value === undefined) {
64
+ const value =
65
+ /** @type {Buffer} */
66
+ (this._valueAsBuffer).toString("utf8");
67
+ if (isDualStringBufferCachingEnabled()) {
68
+ this._value = value;
69
+ }
70
+ return value;
71
+ }
72
+ return this._value;
73
+ }
74
+
75
+ buffer() {
76
+ if (this._valueAsBuffer === undefined) {
77
+ const value = Buffer.from(/** @type {string} */ (this._value), "utf8");
78
+ if (isDualStringBufferCachingEnabled()) {
79
+ this._valueAsBuffer = value;
80
+ }
81
+ return value;
82
+ }
83
+ return this._valueAsBuffer;
84
+ }
85
+
86
+ /**
87
+ * @param {MapOptions=} options map options
88
+ * @returns {RawSourceMap | null} map
89
+ */
90
+ map(options) {
91
+ return getMap(this, options);
92
+ }
93
+
94
+ /**
95
+ * @param {MapOptions=} options map options
96
+ * @returns {SourceAndMap} source and map
97
+ */
98
+ sourceAndMap(options) {
99
+ return getSourceAndMap(this, options);
100
+ }
101
+
102
+ /**
103
+ * @param {Options} options options
104
+ * @param {OnChunk} onChunk called for each chunk of code
105
+ * @param {OnSource} onSource called for each source
106
+ * @param {OnName} _onName called for each name
107
+ * @returns {GeneratedSourceInfo} generated source info
108
+ */
109
+ streamChunks(options, onChunk, onSource, _onName) {
110
+ if (this._value === undefined) {
111
+ this._value =
112
+ /** @type {Buffer} */
113
+ (this._valueAsBuffer).toString("utf8");
114
+ }
115
+ onSource(0, this._name, this._value);
116
+ const finalSource = Boolean(options && options.finalSource);
117
+ if (!options || options.columns !== false) {
118
+ // With column info we need to read all lines and split them
119
+ const matches = splitIntoPotentialTokens(this._value);
120
+ let line = 1;
121
+ let column = 0;
122
+ if (matches !== null) {
123
+ for (const match of matches) {
124
+ const isEndOfLine = match.endsWith("\n");
125
+ if (isEndOfLine && match.length === 1) {
126
+ if (!finalSource) onChunk(match, line, column, -1, -1, -1, -1);
127
+ } else {
128
+ const chunk = finalSource ? undefined : match;
129
+ onChunk(chunk, line, column, 0, line, column, -1);
130
+ }
131
+ if (isEndOfLine) {
132
+ line++;
133
+ column = 0;
134
+ } else {
135
+ column += match.length;
136
+ }
137
+ }
138
+ }
139
+ return {
140
+ generatedLine: line,
141
+ generatedColumn: column,
142
+ source: finalSource ? this._value : undefined,
143
+ };
144
+ } else if (finalSource) {
145
+ // Without column info and with final source we only
146
+ // need meta info to generate mapping
147
+ const result = getGeneratedSourceInfo(this._value);
148
+ const { generatedLine, generatedColumn } = result;
149
+ if (generatedColumn === 0) {
150
+ for (
151
+ let line = 1;
152
+ line < /** @type {number} */ (generatedLine);
153
+ line++
154
+ ) {
155
+ onChunk(undefined, line, 0, 0, line, 0, -1);
156
+ }
157
+ } else {
158
+ for (
159
+ let line = 1;
160
+ line <= /** @type {number} */ (generatedLine);
161
+ line++
162
+ ) {
163
+ onChunk(undefined, line, 0, 0, line, 0, -1);
164
+ }
165
+ }
166
+ return result;
167
+ }
168
+ // Without column info, but also without final source
169
+ // we need to split source by lines
170
+ let line = 1;
171
+ const matches = splitIntoLines(this._value);
172
+ /** @type {string | undefined} */
173
+ let match;
174
+ for (match of matches) {
175
+ onChunk(finalSource ? undefined : match, line, 0, 0, line, 0, -1);
176
+ line++;
177
+ }
178
+ return matches.length === 0 || /** @type {string} */ (match).endsWith("\n")
179
+ ? {
180
+ generatedLine: matches.length + 1,
181
+ generatedColumn: 0,
182
+ source: finalSource ? this._value : undefined,
183
+ }
184
+ : {
185
+ generatedLine: matches.length,
186
+ generatedColumn: /** @type {string} */ (match).length,
187
+ source: finalSource ? this._value : undefined,
188
+ };
189
+ }
190
+
191
+ /**
192
+ * @param {HashLike} hash hash
193
+ * @returns {void}
194
+ */
195
+ updateHash(hash) {
196
+ hash.update("OriginalSource");
197
+ hash.update(this.buffer());
198
+ hash.update(this._name || "");
199
+ }
200
+ }
201
+
202
+ module.exports = OriginalSource;