@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,557 @@
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 splitIntoLines = require("./helpers/splitIntoLines");
11
+ const streamChunks = require("./helpers/streamChunks");
12
+
13
+ /** @typedef {import("./Source").HashLike} HashLike */
14
+ /** @typedef {import("./Source").MapOptions} MapOptions */
15
+ /** @typedef {import("./Source").RawSourceMap} RawSourceMap */
16
+ /** @typedef {import("./Source").SourceAndMap} SourceAndMap */
17
+ /** @typedef {import("./Source").SourceValue} SourceValue */
18
+ /** @typedef {import("./helpers/getGeneratedSourceInfo").GeneratedSourceInfo} GeneratedSourceInfo */
19
+ /** @typedef {import("./helpers/streamChunks").OnChunk} OnChunk */
20
+ /** @typedef {import("./helpers/streamChunks").OnName} OnName */
21
+ /** @typedef {import("./helpers/streamChunks").OnSource} OnSource */
22
+ /** @typedef {import("./helpers/streamChunks").Options} Options */
23
+
24
+ // since v8 7.0, Array.prototype.sort is stable
25
+ const hasStableSort =
26
+ typeof process === "object" &&
27
+ process.versions &&
28
+ typeof process.versions.v8 === "string" &&
29
+ !/^[0-6]\./.test(process.versions.v8);
30
+
31
+ // This is larger than max string length
32
+ const MAX_SOURCE_POSITION = 0x20000000;
33
+
34
+ class Replacement {
35
+ /**
36
+ * @param {number} start start
37
+ * @param {number} end end
38
+ * @param {string} content content
39
+ * @param {string=} name name
40
+ */
41
+ constructor(start, end, content, name) {
42
+ this.start = start;
43
+ this.end = end;
44
+ this.content = content;
45
+ this.name = name;
46
+ if (!hasStableSort) {
47
+ this.index = -1;
48
+ }
49
+ }
50
+ }
51
+
52
+ class ReplaceSource extends Source {
53
+ /**
54
+ * @param {Source} source source
55
+ * @param {string=} name name
56
+ */
57
+ constructor(source, name) {
58
+ super();
59
+ /**
60
+ * @private
61
+ * @type {Source}
62
+ */
63
+ this._source = source;
64
+ /**
65
+ * @private
66
+ * @type {string | undefined}
67
+ */
68
+ this._name = name;
69
+ /** @type {Replacement[]} */
70
+ this._replacements = [];
71
+ /**
72
+ * @private
73
+ * @type {boolean}
74
+ */
75
+ this._isSorted = true;
76
+ }
77
+
78
+ getName() {
79
+ return this._name;
80
+ }
81
+
82
+ getReplacements() {
83
+ this._sortReplacements();
84
+ return this._replacements;
85
+ }
86
+
87
+ /**
88
+ * @param {number} start start
89
+ * @param {number} end end
90
+ * @param {string} newValue new value
91
+ * @param {string=} name name
92
+ * @returns {void}
93
+ */
94
+ replace(start, end, newValue, name) {
95
+ if (typeof newValue !== "string") {
96
+ throw new Error(
97
+ `insertion must be a string, but is a ${typeof newValue}`,
98
+ );
99
+ }
100
+ this._replacements.push(new Replacement(start, end, newValue, name));
101
+ this._isSorted = false;
102
+ }
103
+
104
+ /**
105
+ * @param {number} pos pos
106
+ * @param {string} newValue new value
107
+ * @param {string=} name name
108
+ * @returns {void}
109
+ */
110
+ insert(pos, newValue, name) {
111
+ if (typeof newValue !== "string") {
112
+ throw new Error(
113
+ `insertion must be a string, but is a ${typeof newValue}: ${newValue}`,
114
+ );
115
+ }
116
+ this._replacements.push(new Replacement(pos, pos - 1, newValue, name));
117
+ this._isSorted = false;
118
+ }
119
+
120
+ /**
121
+ * @returns {SourceValue} source
122
+ */
123
+ source() {
124
+ if (this._replacements.length === 0) {
125
+ return this._source.source();
126
+ }
127
+ let current = this._source.source();
128
+ let pos = 0;
129
+ const result = [];
130
+
131
+ this._sortReplacements();
132
+ for (const replacement of this._replacements) {
133
+ const start = Math.floor(replacement.start);
134
+ const end = Math.floor(replacement.end + 1);
135
+ if (pos < start) {
136
+ const offset = start - pos;
137
+ result.push(current.slice(0, offset));
138
+ current = current.slice(offset);
139
+ pos = start;
140
+ }
141
+ result.push(replacement.content);
142
+ if (pos < end) {
143
+ const offset = end - pos;
144
+ current = current.slice(offset);
145
+ pos = end;
146
+ }
147
+ }
148
+ result.push(current);
149
+ return result.join("");
150
+ }
151
+
152
+ /**
153
+ * @param {MapOptions=} options map options
154
+ * @returns {RawSourceMap | null} map
155
+ */
156
+ map(options) {
157
+ if (this._replacements.length === 0) {
158
+ return this._source.map(options);
159
+ }
160
+ return getMap(this, options);
161
+ }
162
+
163
+ /**
164
+ * @param {MapOptions=} options map options
165
+ * @returns {SourceAndMap} source and map
166
+ */
167
+ sourceAndMap(options) {
168
+ if (this._replacements.length === 0) {
169
+ return this._source.sourceAndMap(options);
170
+ }
171
+ return getSourceAndMap(this, options);
172
+ }
173
+
174
+ original() {
175
+ return this._source;
176
+ }
177
+
178
+ _sortReplacements() {
179
+ if (this._isSorted) return;
180
+ if (hasStableSort) {
181
+ this._replacements.sort((a, b) => {
182
+ const diff1 = a.start - b.start;
183
+ if (diff1 !== 0) return diff1;
184
+ const diff2 = a.end - b.end;
185
+ if (diff2 !== 0) return diff2;
186
+ return 0;
187
+ });
188
+ } else {
189
+ for (const [i, repl] of this._replacements.entries()) repl.index = i;
190
+ this._replacements.sort((a, b) => {
191
+ const diff1 = a.start - b.start;
192
+ if (diff1 !== 0) return diff1;
193
+ const diff2 = a.end - b.end;
194
+ if (diff2 !== 0) return diff2;
195
+ return (
196
+ /** @type {number} */ (a.index) - /** @type {number} */ (b.index)
197
+ );
198
+ });
199
+ }
200
+ this._isSorted = true;
201
+ }
202
+
203
+ /**
204
+ * @param {Options} options options
205
+ * @param {OnChunk} onChunk called for each chunk of code
206
+ * @param {OnSource} onSource called for each source
207
+ * @param {OnName} onName called for each name
208
+ * @returns {GeneratedSourceInfo} generated source info
209
+ */
210
+ streamChunks(options, onChunk, onSource, onName) {
211
+ this._sortReplacements();
212
+ const replacements = this._replacements;
213
+ let pos = 0;
214
+ let i = 0;
215
+ let replacementEnd = -1;
216
+ let nextReplacement =
217
+ i < replacements.length
218
+ ? Math.floor(replacements[i].start)
219
+ : MAX_SOURCE_POSITION;
220
+ let generatedLineOffset = 0;
221
+ let generatedColumnOffset = 0;
222
+ let generatedColumnOffsetLine = 0;
223
+ /** @type {(string | string[] | undefined)[]} */
224
+ const sourceContents = [];
225
+ /** @type {Map<string, number>} */
226
+ const nameMapping = new Map();
227
+ /** @type {number[]} */
228
+ const nameIndexMapping = [];
229
+ /**
230
+ * @param {number} sourceIndex source index
231
+ * @param {number} line line
232
+ * @param {number} column column
233
+ * @param {string} expectedChunk expected chunk
234
+ * @returns {boolean} result
235
+ */
236
+ const checkOriginalContent = (sourceIndex, line, column, expectedChunk) => {
237
+ /** @type {undefined | string | string[]} */
238
+ let content =
239
+ sourceIndex < sourceContents.length
240
+ ? sourceContents[sourceIndex]
241
+ : undefined;
242
+ if (content === undefined) return false;
243
+ if (typeof content === "string") {
244
+ content = splitIntoLines(content);
245
+ sourceContents[sourceIndex] = content;
246
+ }
247
+ const contentLine = line <= content.length ? content[line - 1] : null;
248
+ if (contentLine === null) return false;
249
+ return (
250
+ contentLine.slice(column, column + expectedChunk.length) ===
251
+ expectedChunk
252
+ );
253
+ };
254
+ const { generatedLine, generatedColumn } = streamChunks(
255
+ this._source,
256
+ { ...options, finalSource: false },
257
+ (
258
+ _chunk,
259
+ generatedLine,
260
+ generatedColumn,
261
+ sourceIndex,
262
+ originalLine,
263
+ originalColumn,
264
+ nameIndex,
265
+ ) => {
266
+ let chunkPos = 0;
267
+ const chunk = /** @type {string} */ (_chunk);
268
+ const endPos = pos + chunk.length;
269
+
270
+ // Skip over when it has been replaced
271
+ if (replacementEnd > pos) {
272
+ // Skip over the whole chunk
273
+ if (replacementEnd >= endPos) {
274
+ const line = generatedLine + generatedLineOffset;
275
+ if (chunk.endsWith("\n")) {
276
+ generatedLineOffset--;
277
+ if (generatedColumnOffsetLine === line) {
278
+ // undo exiting corrections form the current line
279
+ generatedColumnOffset += generatedColumn;
280
+ }
281
+ } else if (generatedColumnOffsetLine === line) {
282
+ generatedColumnOffset -= chunk.length;
283
+ } else {
284
+ generatedColumnOffset = -chunk.length;
285
+ generatedColumnOffsetLine = line;
286
+ }
287
+ pos = endPos;
288
+ return;
289
+ }
290
+
291
+ // Partially skip over chunk
292
+ chunkPos = replacementEnd - pos;
293
+ if (
294
+ checkOriginalContent(
295
+ sourceIndex,
296
+ originalLine,
297
+ originalColumn,
298
+ chunk.slice(0, chunkPos),
299
+ )
300
+ ) {
301
+ originalColumn += chunkPos;
302
+ }
303
+ pos += chunkPos;
304
+ const line = generatedLine + generatedLineOffset;
305
+ if (generatedColumnOffsetLine === line) {
306
+ generatedColumnOffset -= chunkPos;
307
+ } else {
308
+ generatedColumnOffset = -chunkPos;
309
+ generatedColumnOffsetLine = line;
310
+ }
311
+ generatedColumn += chunkPos;
312
+ }
313
+
314
+ // Is a replacement in the chunk?
315
+ if (nextReplacement < endPos) {
316
+ do {
317
+ let line = generatedLine + generatedLineOffset;
318
+ if (nextReplacement > pos) {
319
+ // Emit chunk until replacement
320
+ const offset = nextReplacement - pos;
321
+ const chunkSlice = chunk.slice(chunkPos, chunkPos + offset);
322
+ onChunk(
323
+ chunkSlice,
324
+ line,
325
+ generatedColumn +
326
+ (line === generatedColumnOffsetLine
327
+ ? generatedColumnOffset
328
+ : 0),
329
+ sourceIndex,
330
+ originalLine,
331
+ originalColumn,
332
+ nameIndex < 0 || nameIndex >= nameIndexMapping.length
333
+ ? -1
334
+ : nameIndexMapping[nameIndex],
335
+ );
336
+ generatedColumn += offset;
337
+ chunkPos += offset;
338
+ pos = nextReplacement;
339
+ if (
340
+ checkOriginalContent(
341
+ sourceIndex,
342
+ originalLine,
343
+ originalColumn,
344
+ chunkSlice,
345
+ )
346
+ ) {
347
+ originalColumn += chunkSlice.length;
348
+ }
349
+ }
350
+
351
+ // Insert replacement content splitted into chunks by lines
352
+ const { content, name } = replacements[i];
353
+ const matches = splitIntoLines(content);
354
+ let replacementNameIndex = nameIndex;
355
+ if (sourceIndex >= 0 && name) {
356
+ let globalIndex = nameMapping.get(name);
357
+ if (globalIndex === undefined) {
358
+ globalIndex = nameMapping.size;
359
+ nameMapping.set(name, globalIndex);
360
+ onName(globalIndex, name);
361
+ }
362
+ replacementNameIndex = globalIndex;
363
+ }
364
+ for (let m = 0; m < matches.length; m++) {
365
+ const contentLine = matches[m];
366
+ onChunk(
367
+ contentLine,
368
+ line,
369
+ generatedColumn +
370
+ (line === generatedColumnOffsetLine
371
+ ? generatedColumnOffset
372
+ : 0),
373
+ sourceIndex,
374
+ originalLine,
375
+ originalColumn,
376
+ replacementNameIndex,
377
+ );
378
+
379
+ // Only the first chunk has name assigned
380
+ replacementNameIndex = -1;
381
+
382
+ if (m === matches.length - 1 && !contentLine.endsWith("\n")) {
383
+ if (generatedColumnOffsetLine === line) {
384
+ generatedColumnOffset += contentLine.length;
385
+ } else {
386
+ generatedColumnOffset = contentLine.length;
387
+ generatedColumnOffsetLine = line;
388
+ }
389
+ } else {
390
+ generatedLineOffset++;
391
+ line++;
392
+ generatedColumnOffset = -generatedColumn;
393
+ generatedColumnOffsetLine = line;
394
+ }
395
+ }
396
+
397
+ // Remove replaced content by settings this variable
398
+ replacementEnd = Math.max(
399
+ replacementEnd,
400
+ Math.floor(replacements[i].end + 1),
401
+ );
402
+
403
+ // Move to next replacement
404
+ i++;
405
+ nextReplacement =
406
+ i < replacements.length
407
+ ? Math.floor(replacements[i].start)
408
+ : MAX_SOURCE_POSITION;
409
+
410
+ // Skip over when it has been replaced
411
+ const offset = chunk.length - endPos + replacementEnd - chunkPos;
412
+ if (offset > 0) {
413
+ // Skip over whole chunk
414
+ if (replacementEnd >= endPos) {
415
+ const line = generatedLine + generatedLineOffset;
416
+ if (chunk.endsWith("\n")) {
417
+ generatedLineOffset--;
418
+ if (generatedColumnOffsetLine === line) {
419
+ // undo exiting corrections form the current line
420
+ generatedColumnOffset += generatedColumn;
421
+ }
422
+ } else if (generatedColumnOffsetLine === line) {
423
+ generatedColumnOffset -= chunk.length - chunkPos;
424
+ } else {
425
+ generatedColumnOffset = chunkPos - chunk.length;
426
+ generatedColumnOffsetLine = line;
427
+ }
428
+ pos = endPos;
429
+ return;
430
+ }
431
+
432
+ // Partially skip over chunk
433
+ const line = generatedLine + generatedLineOffset;
434
+ if (
435
+ checkOriginalContent(
436
+ sourceIndex,
437
+ originalLine,
438
+ originalColumn,
439
+ chunk.slice(chunkPos, chunkPos + offset),
440
+ )
441
+ ) {
442
+ originalColumn += offset;
443
+ }
444
+ chunkPos += offset;
445
+ pos += offset;
446
+ if (generatedColumnOffsetLine === line) {
447
+ generatedColumnOffset -= offset;
448
+ } else {
449
+ generatedColumnOffset = -offset;
450
+ generatedColumnOffsetLine = line;
451
+ }
452
+ generatedColumn += offset;
453
+ }
454
+ } while (nextReplacement < endPos);
455
+ }
456
+
457
+ // Emit remaining chunk
458
+ if (chunkPos < chunk.length) {
459
+ const chunkSlice = chunkPos === 0 ? chunk : chunk.slice(chunkPos);
460
+ const line = generatedLine + generatedLineOffset;
461
+ onChunk(
462
+ chunkSlice,
463
+ line,
464
+ generatedColumn +
465
+ (line === generatedColumnOffsetLine ? generatedColumnOffset : 0),
466
+ sourceIndex,
467
+ originalLine,
468
+ originalColumn,
469
+ nameIndex < 0 ? -1 : nameIndexMapping[nameIndex],
470
+ );
471
+ }
472
+ pos = endPos;
473
+ },
474
+ (sourceIndex, source, sourceContent) => {
475
+ while (sourceContents.length < sourceIndex) {
476
+ sourceContents.push(undefined);
477
+ }
478
+ sourceContents[sourceIndex] = sourceContent;
479
+ onSource(sourceIndex, source, sourceContent);
480
+ },
481
+ (nameIndex, name) => {
482
+ let globalIndex = nameMapping.get(name);
483
+ if (globalIndex === undefined) {
484
+ globalIndex = nameMapping.size;
485
+ nameMapping.set(name, globalIndex);
486
+ onName(globalIndex, name);
487
+ }
488
+ nameIndexMapping[nameIndex] = globalIndex;
489
+ },
490
+ );
491
+
492
+ // Handle remaining replacements
493
+ let remainer = "";
494
+ for (; i < replacements.length; i++) {
495
+ remainer += replacements[i].content;
496
+ }
497
+
498
+ // Insert remaining replacements content splitted into chunks by lines
499
+ let line = /** @type {number} */ (generatedLine) + generatedLineOffset;
500
+ const matches = splitIntoLines(remainer);
501
+ for (let m = 0; m < matches.length; m++) {
502
+ const contentLine = matches[m];
503
+ onChunk(
504
+ contentLine,
505
+ line,
506
+ /** @type {number} */
507
+ (generatedColumn) +
508
+ (line === generatedColumnOffsetLine ? generatedColumnOffset : 0),
509
+ -1,
510
+ -1,
511
+ -1,
512
+ -1,
513
+ );
514
+
515
+ if (m === matches.length - 1 && !contentLine.endsWith("\n")) {
516
+ if (generatedColumnOffsetLine === line) {
517
+ generatedColumnOffset += contentLine.length;
518
+ } else {
519
+ generatedColumnOffset = contentLine.length;
520
+ generatedColumnOffsetLine = line;
521
+ }
522
+ } else {
523
+ generatedLineOffset++;
524
+ line++;
525
+ generatedColumnOffset = -(/** @type {number} */ (generatedColumn));
526
+ generatedColumnOffsetLine = line;
527
+ }
528
+ }
529
+
530
+ return {
531
+ generatedLine: line,
532
+ generatedColumn:
533
+ /** @type {number} */
534
+ (generatedColumn) +
535
+ (line === generatedColumnOffsetLine ? generatedColumnOffset : 0),
536
+ };
537
+ }
538
+
539
+ /**
540
+ * @param {HashLike} hash hash
541
+ * @returns {void}
542
+ */
543
+ updateHash(hash) {
544
+ this._sortReplacements();
545
+ hash.update("ReplaceSource");
546
+ this._source.updateHash(hash);
547
+ hash.update(this._name || "");
548
+ for (const repl of this._replacements) {
549
+ hash.update(
550
+ `${repl.start}${repl.end}${repl.content}${repl.name ? repl.name : ""}`,
551
+ );
552
+ }
553
+ }
554
+ }
555
+
556
+ module.exports = ReplaceSource;
557
+ module.exports.Replacement = Replacement;
@@ -0,0 +1,71 @@
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
+
10
+ /** @typedef {import("./Source").HashLike} HashLike */
11
+ /** @typedef {import("./Source").MapOptions} MapOptions */
12
+ /** @typedef {import("./Source").RawSourceMap} RawSourceMap */
13
+ /** @typedef {import("./Source").SourceValue} SourceValue */
14
+
15
+ class SizeOnlySource extends Source {
16
+ /**
17
+ * @param {number} size size
18
+ */
19
+ constructor(size) {
20
+ super();
21
+ /**
22
+ * @private
23
+ * @type {number}
24
+ */
25
+ this._size = size;
26
+ }
27
+
28
+ _error() {
29
+ return new Error(
30
+ "Content and Map of this Source is not available (only size() is supported)",
31
+ );
32
+ }
33
+
34
+ size() {
35
+ return this._size;
36
+ }
37
+
38
+ /**
39
+ * @returns {SourceValue} source
40
+ */
41
+ source() {
42
+ throw this._error();
43
+ }
44
+
45
+ /**
46
+ * @returns {Buffer} buffer
47
+ */
48
+ buffer() {
49
+ throw this._error();
50
+ }
51
+
52
+ /**
53
+ * @param {MapOptions=} options map options
54
+ * @returns {RawSourceMap | null} map
55
+ */
56
+ // eslint-disable-next-line no-unused-vars
57
+ map(options) {
58
+ throw this._error();
59
+ }
60
+
61
+ /**
62
+ * @param {HashLike} hash hash
63
+ * @returns {void}
64
+ */
65
+ // eslint-disable-next-line no-unused-vars
66
+ updateHash(hash) {
67
+ throw this._error();
68
+ }
69
+ }
70
+
71
+ module.exports = SizeOnlySource;
package/lib/Source.js ADDED
@@ -0,0 +1,89 @@
1
+ /*
2
+ MIT License http://www.opensource.org/licenses/mit-license.php
3
+ Author Tobias Koppers @sokra
4
+ */
5
+
6
+ "use strict";
7
+
8
+ /**
9
+ * @typedef {object} MapOptions
10
+ * @property {boolean=} columns need columns?
11
+ * @property {boolean=} module is module
12
+ */
13
+
14
+ /**
15
+ * @typedef {object} RawSourceMap
16
+ * @property {number} version version
17
+ * @property {string[]} sources sources
18
+ * @property {string[]} names names
19
+ * @property {string=} sourceRoot source root
20
+ * @property {string[]=} sourcesContent sources content
21
+ * @property {string} mappings mappings
22
+ * @property {string} file file
23
+ * @property {string=} debugId debug id
24
+ * @property {number[]=} ignoreList ignore list
25
+ */
26
+
27
+ /** @typedef {string | Buffer} SourceValue */
28
+
29
+ /**
30
+ * @typedef {object} SourceAndMap
31
+ * @property {SourceValue} source source
32
+ * @property {RawSourceMap | null} map map
33
+ */
34
+
35
+ /**
36
+ * @typedef {object} HashLike
37
+ * @property {(data: string | Buffer, inputEncoding?: string) => HashLike} update make hash update
38
+ * @property {(encoding?: string) => string | Buffer} digest get hash digest
39
+ */
40
+
41
+ class Source {
42
+ /**
43
+ * @returns {SourceValue} source
44
+ */
45
+ source() {
46
+ throw new Error("Abstract");
47
+ }
48
+
49
+ buffer() {
50
+ const source = this.source();
51
+ if (Buffer.isBuffer(source)) return source;
52
+ return Buffer.from(source, "utf8");
53
+ }
54
+
55
+ size() {
56
+ return this.buffer().length;
57
+ }
58
+
59
+ /**
60
+ * @param {MapOptions=} options map options
61
+ * @returns {RawSourceMap | null} map
62
+ */
63
+ // eslint-disable-next-line no-unused-vars
64
+ map(options) {
65
+ return null;
66
+ }
67
+
68
+ /**
69
+ * @param {MapOptions=} options map options
70
+ * @returns {SourceAndMap} source and map
71
+ */
72
+ sourceAndMap(options) {
73
+ return {
74
+ source: this.source(),
75
+ map: this.map(options),
76
+ };
77
+ }
78
+
79
+ /**
80
+ * @param {HashLike} hash hash
81
+ * @returns {void}
82
+ */
83
+ // eslint-disable-next-line no-unused-vars
84
+ updateHash(hash) {
85
+ throw new Error("Abstract");
86
+ }
87
+ }
88
+
89
+ module.exports = Source;