@depup/webpack-sources 3.3.4-depup.0 → 3.6.0-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.
Files changed (53) hide show
  1. package/README.md +2 -2
  2. package/changes.json +1 -1
  3. package/lib/CachedSource.js +257 -63
  4. package/lib/CompatSource.js +42 -1
  5. package/lib/ConcatSource.js +100 -28
  6. package/lib/OriginalSource.js +111 -45
  7. package/lib/PrefixSource.js +60 -11
  8. package/lib/RawSource.js +85 -29
  9. package/lib/ReplaceSource.js +269 -73
  10. package/lib/SizeOnlySource.js +10 -1
  11. package/lib/Source.js +39 -0
  12. package/lib/SourceMapSource.js +92 -10
  13. package/lib/helpers/createMappingsSerializer.js +307 -54
  14. package/lib/helpers/getFromStreamChunks.js +165 -73
  15. package/lib/helpers/getGeneratedSourceInfo.js +7 -4
  16. package/lib/helpers/readMappings.js +6 -3
  17. package/lib/helpers/scopes.js +438 -0
  18. package/lib/helpers/splitIntoLines.js +10 -9
  19. package/lib/helpers/splitIntoPotentialTokens.js +99 -26
  20. package/lib/helpers/streamAndGetSourceAndMap.js +61 -22
  21. package/lib/helpers/streamChunks.js +3 -2
  22. package/lib/helpers/streamChunksOfCombinedSourceMap.js +7 -3
  23. package/lib/helpers/streamChunksOfRawSource.js +23 -15
  24. package/lib/helpers/streamChunksOfSourceMap.js +50 -42
  25. package/lib/index.js +3 -0
  26. package/package.json +34 -23
  27. package/types/CachedSource.d.ts +192 -0
  28. package/types/CompatSource.d.ts +87 -0
  29. package/types/ConcatSource.d.ts +75 -0
  30. package/types/OriginalSource.d.ts +87 -0
  31. package/types/PrefixSource.d.ts +59 -0
  32. package/types/RawSource.d.ts +75 -0
  33. package/types/ReplaceSource.d.ts +107 -0
  34. package/types/SizeOnlySource.d.ts +24 -0
  35. package/types/Source.d.ts +189 -0
  36. package/types/SourceMapSource.d.ts +136 -0
  37. package/types/helpers/createMappingsSerializer.d.ts +60 -0
  38. package/types/helpers/getFromStreamChunks.d.ts +16 -0
  39. package/types/helpers/getGeneratedSourceInfo.d.ts +31 -0
  40. package/types/helpers/getName.d.ts +15 -0
  41. package/types/helpers/getSource.d.ts +15 -0
  42. package/types/helpers/readMappings.d.ts +19 -0
  43. package/types/helpers/scopes.d.ts +119 -0
  44. package/types/helpers/splitIntoLines.d.ts +6 -0
  45. package/types/helpers/splitIntoPotentialTokens.d.ts +37 -0
  46. package/types/helpers/streamAndGetSourceAndMap.d.ts +39 -0
  47. package/types/helpers/streamChunks.d.ts +56 -0
  48. package/types/helpers/streamChunksOfCombinedSourceMap.d.ts +42 -0
  49. package/types/helpers/streamChunksOfRawSource.d.ts +16 -0
  50. package/types/helpers/streamChunksOfSourceMap.d.ts +19 -0
  51. package/types/helpers/stringBufferUtils.d.ts +55 -0
  52. package/types/index.d.ts +50 -0
  53. package/types.d.ts +42 -439
@@ -16,12 +16,209 @@
16
16
  * @returns {string} result
17
17
  */
18
18
 
19
+ /**
20
+ * A push-based serializer: `add()` appends one mapping to an internal
21
+ * byte buffer, `finish()` materialises the whole `mappings` string in a
22
+ * single allocation. Compared to the string-returning
23
+ * {@link MappingsSerializer} this avoids every per-mapping intermediate
24
+ * string (each VLQ digit concatenation) plus the caller-side
25
+ * `mappings += str` cons chain — together the dominant allocation site
26
+ * of `map()` / `sourceAndMap()`.
27
+ * @typedef {object} MappingsWriter
28
+ * @property {(generatedLine: number, generatedColumn: number, sourceIndex: number, originalLine: number, originalColumn: number, nameIndex: number) => void} add append one mapping
29
+ * @property {() => string} finish materialise the mappings string
30
+ */
31
+
19
32
  const ALPHABET = [
20
33
  ..."ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
21
34
  ];
22
35
 
36
+ // Char codes of ALPHABET for the buffer-writing path.
37
+ const ALPHABET_CODES = new Uint8Array(64);
38
+ for (let i = 0; i < 64; i++) ALPHABET_CODES[i] = ALPHABET[i].charCodeAt(0);
39
+
40
+ const CH_SEMICOLON = 59; // ;
41
+ const CH_COMMA = 44; // ,
42
+ const CH_A = 65; // A
43
+
23
44
  const CONTINUATION_BIT = 0x20;
24
45
 
46
+ /**
47
+ * Append a VLQ-encoded signed integer to `str`. Hoisted to module scope so
48
+ * that both serializers share a single function object and avoid allocating
49
+ * a new closure on every call.
50
+ * @param {string} str current string buffer
51
+ * @param {number} value signed integer to encode
52
+ * @returns {string} updated string buffer
53
+ */
54
+ const writeValue = (str, value) => {
55
+ const sign = (value >>> 31) & 1;
56
+ const mask = value >> 31;
57
+ const absValue = (value + mask) ^ mask;
58
+ let data = (absValue << 1) | sign;
59
+ for (;;) {
60
+ const sextet = data & 0x1f;
61
+ data >>= 5;
62
+ if (data === 0) {
63
+ return str + ALPHABET[sextet];
64
+ }
65
+ str += ALPHABET[sextet | CONTINUATION_BIT];
66
+ }
67
+ };
68
+
69
+ /**
70
+ * Byte-buffer state shared by the writer variants. A plain object (not a
71
+ * class) so the hot `add` closures capture it directly.
72
+ * @returns {{ buf: Uint8Array, pos: number }} state
73
+ */
74
+ const createBufferState = () => ({ buf: new Uint8Array(1024), pos: 0 });
75
+
76
+ /**
77
+ * Ensure space for `n` more bytes.
78
+ * @param {{ buf: Uint8Array, pos: number }} state state
79
+ * @param {number} n bytes needed
80
+ * @returns {Uint8Array} the (possibly grown) buffer
81
+ */
82
+ const ensure = (state, n) => {
83
+ const { buf, pos } = state;
84
+ if (pos + n <= buf.length) return buf;
85
+ let nextLength = buf.length * 2;
86
+ while (nextLength < pos + n) nextLength *= 2;
87
+ const next = new Uint8Array(nextLength);
88
+ next.set(buf);
89
+ state.buf = next;
90
+ return next;
91
+ };
92
+
93
+ /**
94
+ * Append a VLQ-encoded signed integer to the byte buffer. The caller must
95
+ * have reserved space already (a 32-bit value needs at most 7 sextets).
96
+ * @param {{ buf: Uint8Array, pos: number }} state state
97
+ * @param {number} value signed integer to encode
98
+ * @returns {void}
99
+ */
100
+ const writeValueBytes = (state, value) => {
101
+ const { buf } = state;
102
+ let { pos } = state;
103
+ const sign = (value >>> 31) & 1;
104
+ const mask = value >> 31;
105
+ const absValue = (value + mask) ^ mask;
106
+ let data = (absValue << 1) | sign;
107
+ for (;;) {
108
+ const sextet = data & 0x1f;
109
+ data >>= 5;
110
+ if (data === 0) {
111
+ buf[pos++] = ALPHABET_CODES[sextet];
112
+ break;
113
+ }
114
+ buf[pos++] = ALPHABET_CODES[sextet | CONTINUATION_BIT];
115
+ }
116
+ state.pos = pos;
117
+ };
118
+
119
+ /**
120
+ * @param {{ buf: Uint8Array, pos: number }} state state
121
+ * @returns {string} the mappings accumulated so far
122
+ */
123
+ const bufferToString = (state) => {
124
+ if (state.pos === 0) return "";
125
+ // The mappings alphabet is pure ASCII, so a latin1 decode is exact and
126
+ // needs no re-encoding pass.
127
+ return Buffer.from(state.buf.buffer, 0, state.pos).toString("latin1");
128
+ };
129
+
130
+ /**
131
+ * @returns {MappingsWriter} writer
132
+ */
133
+ const createFullMappingsWriter = () => {
134
+ const state = createBufferState();
135
+ let currentLine = 1;
136
+ let currentColumn = 0;
137
+ let currentSourceIndex = 0;
138
+ let currentOriginalLine = 1;
139
+ let currentOriginalColumn = 0;
140
+ let currentNameIndex = 0;
141
+ let activeMapping = false;
142
+ let activeName = false;
143
+ let initial = true;
144
+ return {
145
+ add(
146
+ generatedLine,
147
+ generatedColumn,
148
+ sourceIndex,
149
+ originalLine,
150
+ originalColumn,
151
+ nameIndex,
152
+ ) {
153
+ if (activeMapping && currentLine === generatedLine) {
154
+ // A mapping is still active
155
+ if (
156
+ sourceIndex === currentSourceIndex &&
157
+ originalLine === currentOriginalLine &&
158
+ originalColumn === currentOriginalColumn &&
159
+ !activeName &&
160
+ nameIndex < 0
161
+ ) {
162
+ // avoid repeating the same original mapping
163
+ return;
164
+ }
165
+ }
166
+ // No mapping is active
167
+ else if (sourceIndex < 0) {
168
+ // avoid writing unneccessary generated mappings
169
+ return;
170
+ }
171
+
172
+ // Reserve the worst case for one mapping in a single check:
173
+ // line separators + 5 VLQ values of up to 7 sextets each.
174
+ const lineDiff = generatedLine - currentLine;
175
+ const buf = ensure(state, (lineDiff > 0 ? lineDiff : 1) + 35);
176
+ if (lineDiff > 0) {
177
+ // Consecutive lines (diff === 1) are the dominant case.
178
+ buf[state.pos++] = CH_SEMICOLON;
179
+ for (let i = 1; i < lineDiff; i++) buf[state.pos++] = CH_SEMICOLON;
180
+ currentLine = generatedLine;
181
+ currentColumn = 0;
182
+ initial = false;
183
+ } else if (initial) {
184
+ initial = false;
185
+ } else {
186
+ buf[state.pos++] = CH_COMMA;
187
+ }
188
+
189
+ writeValueBytes(state, generatedColumn - currentColumn);
190
+ currentColumn = generatedColumn;
191
+ if (sourceIndex >= 0) {
192
+ activeMapping = true;
193
+ if (sourceIndex === currentSourceIndex) {
194
+ buf[state.pos++] = CH_A;
195
+ } else {
196
+ writeValueBytes(state, sourceIndex - currentSourceIndex);
197
+ currentSourceIndex = sourceIndex;
198
+ }
199
+ writeValueBytes(state, originalLine - currentOriginalLine);
200
+ currentOriginalLine = originalLine;
201
+ if (originalColumn === currentOriginalColumn) {
202
+ buf[state.pos++] = CH_A;
203
+ } else {
204
+ writeValueBytes(state, originalColumn - currentOriginalColumn);
205
+ currentOriginalColumn = originalColumn;
206
+ }
207
+ if (nameIndex >= 0) {
208
+ writeValueBytes(state, nameIndex - currentNameIndex);
209
+ currentNameIndex = nameIndex;
210
+ activeName = true;
211
+ } else {
212
+ activeName = false;
213
+ }
214
+ } else {
215
+ activeMapping = false;
216
+ }
217
+ },
218
+ finish: () => bufferToString(state),
219
+ };
220
+ };
221
+
25
222
  const createFullMappingsSerializer = () => {
26
223
  let currentLine = 1;
27
224
  let currentColumn = 0;
@@ -60,10 +257,14 @@ const createFullMappingsSerializer = () => {
60
257
  return "";
61
258
  }
62
259
 
63
- /** @type {undefined | string} */
64
260
  let str;
65
261
  if (currentLine < generatedLine) {
66
- str = ";".repeat(generatedLine - currentLine);
262
+ // Consecutive lines (diff === 1) are the dominant case; avoid the
263
+ // `.repeat()` call entirely for them.
264
+ str =
265
+ generatedLine === currentLine + 1
266
+ ? ";"
267
+ : ";".repeat(generatedLine - currentLine);
67
268
  currentLine = generatedLine;
68
269
  currentColumn = 0;
69
270
  initial = false;
@@ -74,46 +275,26 @@ const createFullMappingsSerializer = () => {
74
275
  str = ",";
75
276
  }
76
277
 
77
- /**
78
- * @param {number} value value
79
- * @returns {void}
80
- */
81
- const writeValue = (value) => {
82
- const sign = (value >>> 31) & 1;
83
- const mask = value >> 31;
84
- const absValue = (value + mask) ^ mask;
85
- let data = (absValue << 1) | sign;
86
- for (;;) {
87
- const sextet = data & 0x1f;
88
- data >>= 5;
89
- if (data === 0) {
90
- str += ALPHABET[sextet];
91
- break;
92
- } else {
93
- str += ALPHABET[sextet | CONTINUATION_BIT];
94
- }
95
- }
96
- };
97
- writeValue(generatedColumn - currentColumn);
278
+ str = writeValue(str, generatedColumn - currentColumn);
98
279
  currentColumn = generatedColumn;
99
280
  if (sourceIndex >= 0) {
100
281
  activeMapping = true;
101
282
  if (sourceIndex === currentSourceIndex) {
102
283
  str += "A";
103
284
  } else {
104
- writeValue(sourceIndex - currentSourceIndex);
285
+ str = writeValue(str, sourceIndex - currentSourceIndex);
105
286
  currentSourceIndex = sourceIndex;
106
287
  }
107
- writeValue(originalLine - currentOriginalLine);
288
+ str = writeValue(str, originalLine - currentOriginalLine);
108
289
  currentOriginalLine = originalLine;
109
290
  if (originalColumn === currentOriginalColumn) {
110
291
  str += "A";
111
292
  } else {
112
- writeValue(originalColumn - currentOriginalColumn);
293
+ str = writeValue(str, originalColumn - currentOriginalColumn);
113
294
  currentOriginalColumn = originalColumn;
114
295
  }
115
296
  if (nameIndex >= 0) {
116
- writeValue(nameIndex - currentNameIndex);
297
+ str = writeValue(str, nameIndex - currentNameIndex);
117
298
  currentNameIndex = nameIndex;
118
299
  activeName = true;
119
300
  } else {
@@ -148,28 +329,7 @@ const createLinesOnlyMappingsSerializer = () => {
148
329
  // avoid writing multiple original mappings per line
149
330
  return "";
150
331
  }
151
- /** @type {undefined | string} */
152
332
  let str;
153
- /**
154
- * @param {number} value value
155
- * @returns {void}
156
- */
157
- const writeValue = (value) => {
158
- const sign = (value >>> 31) & 1;
159
- const mask = value >> 31;
160
- const absValue = (value + mask) ^ mask;
161
- let data = (absValue << 1) | sign;
162
- for (;;) {
163
- const sextet = data & 0x1f;
164
- data >>= 5;
165
- if (data === 0) {
166
- str += ALPHABET[sextet];
167
- break;
168
- } else {
169
- str += ALPHABET[sextet | CONTINUATION_BIT];
170
- }
171
- }
172
- };
173
333
  lastWrittenLine = generatedLine;
174
334
  if (generatedLine === currentLine + 1) {
175
335
  currentLine = generatedLine;
@@ -179,14 +339,14 @@ const createLinesOnlyMappingsSerializer = () => {
179
339
  return ";AACA";
180
340
  }
181
341
  str = ";AA";
182
- writeValue(originalLine - currentOriginalLine);
342
+ str = writeValue(str, originalLine - currentOriginalLine);
183
343
  currentOriginalLine = originalLine;
184
344
  return `${str}A`;
185
345
  }
186
346
  str = ";A";
187
- writeValue(sourceIndex - currentSourceIndex);
347
+ str = writeValue(str, sourceIndex - currentSourceIndex);
188
348
  currentSourceIndex = sourceIndex;
189
- writeValue(originalLine - currentOriginalLine);
349
+ str = writeValue(str, originalLine - currentOriginalLine);
190
350
  currentOriginalLine = originalLine;
191
351
  return `${str}A`;
192
352
  }
@@ -198,19 +358,100 @@ const createLinesOnlyMappingsSerializer = () => {
198
358
  return `${str}AACA`;
199
359
  }
200
360
  str += "AA";
201
- writeValue(originalLine - currentOriginalLine);
361
+ str = writeValue(str, originalLine - currentOriginalLine);
202
362
  currentOriginalLine = originalLine;
203
363
  return `${str}A`;
204
364
  }
205
365
  str += "A";
206
- writeValue(sourceIndex - currentSourceIndex);
366
+ str = writeValue(str, sourceIndex - currentSourceIndex);
207
367
  currentSourceIndex = sourceIndex;
208
- writeValue(originalLine - currentOriginalLine);
368
+ str = writeValue(str, originalLine - currentOriginalLine);
209
369
  currentOriginalLine = originalLine;
210
370
  return `${str}A`;
211
371
  };
212
372
  };
213
373
 
374
+ /**
375
+ * Lines-only mappings emit at most one short — usually constant — segment
376
+ * per generated line, so the classic string encoding plus cons-string
377
+ * append is already optimal there (measurably faster than byte-writing).
378
+ * Only the full serializer, which emits per token, benefits from the byte
379
+ * buffer. The encoding below mirrors `createLinesOnlyMappingsSerializer`,
380
+ * inlined so `add` costs a single call.
381
+ * @returns {MappingsWriter} writer
382
+ */
383
+ const createLinesOnlyMappingsWriter = () => {
384
+ let mappings = "";
385
+ let lastWrittenLine = 0;
386
+ let currentLine = 1;
387
+ let currentSourceIndex = 0;
388
+ let currentOriginalLine = 1;
389
+ return {
390
+ add(
391
+ generatedLine,
392
+ _generatedColumn,
393
+ sourceIndex,
394
+ originalLine,
395
+ _originalColumn,
396
+ _nameIndex,
397
+ ) {
398
+ if (sourceIndex < 0) {
399
+ // avoid writing generated mappings at all
400
+ return;
401
+ }
402
+ if (lastWrittenLine === generatedLine) {
403
+ // avoid writing multiple original mappings per line
404
+ return;
405
+ }
406
+ let str;
407
+ lastWrittenLine = generatedLine;
408
+ if (generatedLine === currentLine + 1) {
409
+ currentLine = generatedLine;
410
+ if (sourceIndex === currentSourceIndex) {
411
+ if (originalLine === currentOriginalLine + 1) {
412
+ currentOriginalLine = originalLine;
413
+ mappings += ";AACA";
414
+ return;
415
+ }
416
+ str = ";AA";
417
+ str = writeValue(str, originalLine - currentOriginalLine);
418
+ currentOriginalLine = originalLine;
419
+ mappings += `${str}A`;
420
+ return;
421
+ }
422
+ str = ";A";
423
+ str = writeValue(str, sourceIndex - currentSourceIndex);
424
+ currentSourceIndex = sourceIndex;
425
+ str = writeValue(str, originalLine - currentOriginalLine);
426
+ currentOriginalLine = originalLine;
427
+ mappings += `${str}A`;
428
+ return;
429
+ }
430
+ str = ";".repeat(generatedLine - currentLine);
431
+ currentLine = generatedLine;
432
+ if (sourceIndex === currentSourceIndex) {
433
+ if (originalLine === currentOriginalLine + 1) {
434
+ currentOriginalLine = originalLine;
435
+ mappings += `${str}AACA`;
436
+ return;
437
+ }
438
+ str += "AA";
439
+ str = writeValue(str, originalLine - currentOriginalLine);
440
+ currentOriginalLine = originalLine;
441
+ mappings += `${str}A`;
442
+ return;
443
+ }
444
+ str += "A";
445
+ str = writeValue(str, sourceIndex - currentSourceIndex);
446
+ currentSourceIndex = sourceIndex;
447
+ str = writeValue(str, originalLine - currentOriginalLine);
448
+ currentOriginalLine = originalLine;
449
+ mappings += `${str}A`;
450
+ },
451
+ finish: () => mappings,
452
+ };
453
+ };
454
+
214
455
  /**
215
456
  * @param {{ columns?: boolean }=} options options
216
457
  * @returns {MappingsSerializer} mappings serializer
@@ -222,4 +463,16 @@ const createMappingsSerializer = (options) => {
222
463
  : createFullMappingsSerializer();
223
464
  };
224
465
 
466
+ /**
467
+ * @param {{ columns?: boolean }=} options options
468
+ * @returns {MappingsWriter} push-based mappings writer
469
+ */
470
+ const createMappingsWriter = (options) => {
471
+ const linesOnly = options && options.columns === false;
472
+ return linesOnly
473
+ ? createLinesOnlyMappingsWriter()
474
+ : createFullMappingsWriter();
475
+ };
476
+
225
477
  module.exports = createMappingsSerializer;
478
+ module.exports.createMappingsWriter = createMappingsWriter;