@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
@@ -19,7 +19,6 @@ class SizeOnlySource extends Source {
19
19
  constructor(size) {
20
20
  super();
21
21
  /**
22
- * @private
23
22
  * @type {number}
24
23
  */
25
24
  this._size = size;
@@ -31,6 +30,9 @@ class SizeOnlySource extends Source {
31
30
  );
32
31
  }
33
32
 
33
+ /**
34
+ * @returns {number} size
35
+ */
34
36
  size() {
35
37
  return this._size;
36
38
  }
@@ -49,6 +51,13 @@ class SizeOnlySource extends Source {
49
51
  throw this._error();
50
52
  }
51
53
 
54
+ /**
55
+ * @returns {Buffer[]} buffers
56
+ */
57
+ buffers() {
58
+ throw this._error();
59
+ }
60
+
52
61
  /**
53
62
  * @param {MapOptions=} options map options
54
63
  * @returns {RawSourceMap | null} map
package/lib/Source.js CHANGED
@@ -9,6 +9,7 @@
9
9
  * @typedef {object} MapOptions
10
10
  * @property {boolean=} columns need columns?
11
11
  * @property {boolean=} module is module
12
+ * @property {boolean=} scopes emit the `scopes` field from the bindings the sources declare
12
13
  */
13
14
 
14
15
  /**
@@ -22,6 +23,7 @@
22
23
  * @property {string} file file
23
24
  * @property {string=} debugId debug id
24
25
  * @property {number[]=} ignoreList ignore list
26
+ * @property {string=} scopes encoded `scopes` field of the "Scopes" proposal
25
27
  */
26
28
 
27
29
  /** @typedef {string | Buffer} SourceValue */
@@ -38,6 +40,13 @@
38
40
  * @property {(encoding?: string) => string | Buffer} digest get hash digest
39
41
  */
40
42
 
43
+ /**
44
+ * @typedef {object} ClearCacheOptions
45
+ * @property {boolean=} maps drop cached source maps (default `true`)
46
+ * @property {boolean=} source drop cached source/buffer copies (default `true`)
47
+ * @property {boolean=} parsedMap drop the parsed object form of cached source maps on `SourceMapSource` instances (default `false` — re-parsing JSON is significantly more expensive than `toString`). Only takes effect when a serialized form (buffer or string) is also retained, so the data remains recoverable.
48
+ */
49
+
41
50
  class Source {
42
51
  /**
43
52
  * @returns {SourceValue} source
@@ -46,12 +55,25 @@ class Source {
46
55
  throw new Error("Abstract");
47
56
  }
48
57
 
58
+ /**
59
+ * @returns {Buffer} buffer
60
+ */
49
61
  buffer() {
50
62
  const source = this.source();
51
63
  if (Buffer.isBuffer(source)) return source;
52
64
  return Buffer.from(source, "utf8");
53
65
  }
54
66
 
67
+ /**
68
+ * @returns {Buffer[]} buffers
69
+ */
70
+ buffers() {
71
+ return [this.buffer()];
72
+ }
73
+
74
+ /**
75
+ * @returns {number} size
76
+ */
55
77
  size() {
56
78
  return this.buffer().length;
57
79
  }
@@ -84,6 +106,23 @@ class Source {
84
106
  updateHash(hash) {
85
107
  throw new Error("Abstract");
86
108
  }
109
+
110
+ /**
111
+ * Release cached data held by this source. clearCache is a memory
112
+ * hint: it never affects correctness or output, only how expensive
113
+ * the next read is. Subclasses override; the base is a no-op so
114
+ * every Source supports the call. Composite sources always recurse
115
+ * into wrapped sources. When the same child is reachable via several
116
+ * parents (e.g. modules shared across webpack chunks), pass a shared
117
+ * `visited` WeakSet so each subtree is walked at most once.
118
+ * Not safe to call concurrently with source/map/sourceAndMap/
119
+ * streamChunks/updateHash on the same instance.
120
+ * @param {ClearCacheOptions=} options selectors
121
+ * @param {WeakSet<Source>=} visited de-duplication set shared across calls
122
+ * @returns {void}
123
+ */
124
+ // eslint-disable-next-line no-unused-vars
125
+ clearCache(options, visited) {}
87
126
  }
88
127
 
89
128
  module.exports = Source;
@@ -13,6 +13,7 @@ const {
13
13
  isDualStringBufferCachingEnabled,
14
14
  } = require("./helpers/stringBufferUtils");
15
15
 
16
+ /** @typedef {import("./Source").ClearCacheOptions} ClearCacheOptions */
16
17
  /** @typedef {import("./Source").HashLike} HashLike */
17
18
  /** @typedef {import("./Source").MapOptions} MapOptions */
18
19
  /** @typedef {import("./Source").RawSourceMap} RawSourceMap */
@@ -44,12 +45,10 @@ class SourceMapSource extends Source {
44
45
  super();
45
46
  const valueIsBuffer = Buffer.isBuffer(value);
46
47
  /**
47
- * @private
48
48
  * @type {undefined | string}
49
49
  */
50
50
  this._valueAsString = valueIsBuffer ? undefined : value;
51
51
  /**
52
- * @private
53
52
  * @type {undefined | Buffer}
54
53
  */
55
54
  this._valueAsBuffer = valueIsBuffer ? value : undefined;
@@ -60,18 +59,15 @@ class SourceMapSource extends Source {
60
59
  const sourceMapIsBuffer = Buffer.isBuffer(sourceMap);
61
60
  const sourceMapIsString = typeof sourceMap === "string";
62
61
  /**
63
- * @private
64
62
  * @type {undefined | RawSourceMap}
65
63
  */
66
64
  this._sourceMapAsObject =
67
65
  sourceMapIsBuffer || sourceMapIsString ? undefined : sourceMap;
68
66
  /**
69
- * @private
70
67
  * @type {undefined | string}
71
68
  */
72
69
  this._sourceMapAsString = sourceMapIsString ? sourceMap : undefined;
73
70
  /**
74
- * @private
75
71
  * @type {undefined | Buffer}
76
72
  */
77
73
  this._sourceMapAsBuffer = sourceMapIsBuffer ? sourceMap : undefined;
@@ -89,7 +85,6 @@ class SourceMapSource extends Source {
89
85
  const innerSourceMapIsBuffer = Buffer.isBuffer(innerSourceMap);
90
86
  const innerSourceMapIsString = typeof innerSourceMap === "string";
91
87
  /**
92
- * @private
93
88
  * @type {undefined | RawSourceMap}
94
89
  */
95
90
 
@@ -98,14 +93,12 @@ class SourceMapSource extends Source {
98
93
  ? undefined
99
94
  : innerSourceMap || undefined;
100
95
  /**
101
- * @private
102
96
  * @type {undefined | string}
103
97
  */
104
98
  this._innerSourceMapAsString = innerSourceMapIsString
105
99
  ? innerSourceMap
106
100
  : undefined;
107
101
  /**
108
- * @private
109
102
  * @type {undefined | Buffer}
110
103
  */
111
104
  this._innerSourceMapAsBuffer = innerSourceMapIsBuffer
@@ -129,6 +122,9 @@ class SourceMapSource extends Source {
129
122
  ];
130
123
  }
131
124
 
125
+ /**
126
+ * @returns {Buffer} buffer
127
+ */
132
128
  buffer() {
133
129
  if (this._valueAsBuffer === undefined) {
134
130
  const value = Buffer.from(
@@ -143,6 +139,20 @@ class SourceMapSource extends Source {
143
139
  return this._valueAsBuffer;
144
140
  }
145
141
 
142
+ /**
143
+ * @returns {number} size
144
+ */
145
+ size() {
146
+ if (this._cachedSize !== undefined) return this._cachedSize;
147
+ if (this._valueAsBuffer !== undefined) {
148
+ return (this._cachedSize = this._valueAsBuffer.length);
149
+ }
150
+ return (this._cachedSize = Buffer.byteLength(
151
+ /** @type {string} */ (this._valueAsString),
152
+ "utf8",
153
+ ));
154
+ }
155
+
146
156
  /**
147
157
  * @returns {SourceValue} source
148
158
  */
@@ -160,7 +170,6 @@ class SourceMapSource extends Source {
160
170
  }
161
171
 
162
172
  /**
163
- * @private
164
173
  * @returns {undefined | Buffer} buffer
165
174
  */
166
175
  _originalSourceBuffer() {
@@ -214,7 +223,6 @@ class SourceMapSource extends Source {
214
223
  }
215
224
 
216
225
  /**
217
- * @private
218
226
  * @returns {string} result
219
227
  */
220
228
  _innerSourceMapString() {
@@ -337,6 +345,80 @@ class SourceMapSource extends Source {
337
345
  );
338
346
  }
339
347
 
348
+ /**
349
+ * Release cached data held by this source. clearCache is a memory
350
+ * hint: it never affects correctness or output, only how expensive
351
+ * the next read is. Subclasses override; the base is a no-op so
352
+ * every Source supports the call. Composite sources always recurse
353
+ * into wrapped sources. When the same child is reachable via several
354
+ * parents (e.g. modules shared across webpack chunks), pass a shared
355
+ * `visited` WeakSet so each subtree is walked at most once.
356
+ * Not safe to call concurrently with source/map/sourceAndMap/
357
+ * streamChunks/updateHash on the same instance.
358
+ * @param {ClearCacheOptions=} options selectors
359
+ * @param {WeakSet<Source>=} visited de-duplication set shared across calls
360
+ * @returns {void}
361
+ */
362
+ clearCache(options, visited) {
363
+ if (visited !== undefined) {
364
+ if (visited.has(this)) return;
365
+ visited.add(this);
366
+ }
367
+ const clearSource = !options || options.source !== false;
368
+ const clearMaps = !options || options.maps !== false;
369
+ const clearParsedMap = options !== undefined && options.parsedMap === true;
370
+ // For every dual-cached pair, drop the string form when the
371
+ // buffer is also held — buffer is more compact in V8 (1 byte/char
372
+ // vs 2) and the string rehydrates cheaply. Parsed object forms
373
+ // of source maps are heavier but only dropped when
374
+ // `parsedMap: true` is set, because re-parsing JSON is much more
375
+ // expensive than `toString` from a buffer.
376
+ if (clearSource) {
377
+ if (
378
+ this._valueAsString !== undefined &&
379
+ this._valueAsBuffer !== undefined
380
+ ) {
381
+ this._valueAsString = undefined;
382
+ }
383
+ if (
384
+ this._originalSourceAsString !== undefined &&
385
+ this._originalSourceAsBuffer !== undefined
386
+ ) {
387
+ this._originalSourceAsString = undefined;
388
+ }
389
+ }
390
+ if (clearMaps) {
391
+ if (
392
+ this._sourceMapAsString !== undefined &&
393
+ this._sourceMapAsBuffer !== undefined
394
+ ) {
395
+ this._sourceMapAsString = undefined;
396
+ }
397
+ if (
398
+ clearParsedMap &&
399
+ this._sourceMapAsObject !== undefined &&
400
+ (this._sourceMapAsBuffer !== undefined ||
401
+ this._sourceMapAsString !== undefined)
402
+ ) {
403
+ this._sourceMapAsObject = undefined;
404
+ }
405
+ if (
406
+ this._innerSourceMapAsString !== undefined &&
407
+ this._innerSourceMapAsBuffer !== undefined
408
+ ) {
409
+ this._innerSourceMapAsString = undefined;
410
+ }
411
+ if (
412
+ clearParsedMap &&
413
+ this._innerSourceMapAsObject !== undefined &&
414
+ (this._innerSourceMapAsBuffer !== undefined ||
415
+ this._innerSourceMapAsString !== undefined)
416
+ ) {
417
+ this._innerSourceMapAsObject = undefined;
418
+ }
419
+ }
420
+ }
421
+
340
422
  /**
341
423
  * @param {HashLike} hash hash
342
424
  * @returns {void}