@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,367 @@
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 streamChunksOfCombinedSourceMap = require("./helpers/streamChunksOfCombinedSourceMap");
11
+ const streamChunksOfSourceMap = require("./helpers/streamChunksOfSourceMap");
12
+ const {
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
+
27
+ class SourceMapSource extends Source {
28
+ /**
29
+ * @param {string | Buffer} value value
30
+ * @param {string} name name
31
+ * @param {string | Buffer | RawSourceMap=} sourceMap source map
32
+ * @param {SourceValue=} originalSource original source
33
+ * @param {(null | string | Buffer | RawSourceMap)=} innerSourceMap inner source map
34
+ * @param {boolean=} removeOriginalSource do remove original source
35
+ */
36
+ constructor(
37
+ value,
38
+ name,
39
+ sourceMap,
40
+ originalSource,
41
+ innerSourceMap,
42
+ removeOriginalSource,
43
+ ) {
44
+ super();
45
+ const valueIsBuffer = Buffer.isBuffer(value);
46
+ /**
47
+ * @private
48
+ * @type {undefined | string}
49
+ */
50
+ this._valueAsString = valueIsBuffer ? undefined : value;
51
+ /**
52
+ * @private
53
+ * @type {undefined | Buffer}
54
+ */
55
+ this._valueAsBuffer = valueIsBuffer ? value : undefined;
56
+
57
+ this._name = name;
58
+
59
+ this._hasSourceMap = Boolean(sourceMap);
60
+ const sourceMapIsBuffer = Buffer.isBuffer(sourceMap);
61
+ const sourceMapIsString = typeof sourceMap === "string";
62
+ /**
63
+ * @private
64
+ * @type {undefined | RawSourceMap}
65
+ */
66
+ this._sourceMapAsObject =
67
+ sourceMapIsBuffer || sourceMapIsString ? undefined : sourceMap;
68
+ /**
69
+ * @private
70
+ * @type {undefined | string}
71
+ */
72
+ this._sourceMapAsString = sourceMapIsString ? sourceMap : undefined;
73
+ /**
74
+ * @private
75
+ * @type {undefined | Buffer}
76
+ */
77
+ this._sourceMapAsBuffer = sourceMapIsBuffer ? sourceMap : undefined;
78
+
79
+ this._hasOriginalSource = Boolean(originalSource);
80
+ const originalSourceIsBuffer = Buffer.isBuffer(originalSource);
81
+ this._originalSourceAsString = originalSourceIsBuffer
82
+ ? undefined
83
+ : originalSource;
84
+ this._originalSourceAsBuffer = originalSourceIsBuffer
85
+ ? originalSource
86
+ : undefined;
87
+
88
+ this._hasInnerSourceMap = Boolean(innerSourceMap);
89
+ const innerSourceMapIsBuffer = Buffer.isBuffer(innerSourceMap);
90
+ const innerSourceMapIsString = typeof innerSourceMap === "string";
91
+ /**
92
+ * @private
93
+ * @type {undefined | RawSourceMap}
94
+ */
95
+
96
+ this._innerSourceMapAsObject =
97
+ innerSourceMapIsBuffer || innerSourceMapIsString
98
+ ? undefined
99
+ : innerSourceMap || undefined;
100
+ /**
101
+ * @private
102
+ * @type {undefined | string}
103
+ */
104
+ this._innerSourceMapAsString = innerSourceMapIsString
105
+ ? innerSourceMap
106
+ : undefined;
107
+ /**
108
+ * @private
109
+ * @type {undefined | Buffer}
110
+ */
111
+ this._innerSourceMapAsBuffer = innerSourceMapIsBuffer
112
+ ? innerSourceMap
113
+ : undefined;
114
+
115
+ this._removeOriginalSource = removeOriginalSource;
116
+ }
117
+
118
+ /**
119
+ * @returns {[Buffer, string, Buffer, Buffer | undefined, Buffer | undefined, boolean | undefined]} args
120
+ */
121
+ getArgsAsBuffers() {
122
+ return [
123
+ this.buffer(),
124
+ this._name,
125
+ this._sourceMapBuffer(),
126
+ this._originalSourceBuffer(),
127
+ this._innerSourceMapBuffer(),
128
+ this._removeOriginalSource,
129
+ ];
130
+ }
131
+
132
+ buffer() {
133
+ if (this._valueAsBuffer === undefined) {
134
+ const value = Buffer.from(
135
+ /** @type {string} */ (this._valueAsString),
136
+ "utf8",
137
+ );
138
+ if (isDualStringBufferCachingEnabled()) {
139
+ this._valueAsBuffer = value;
140
+ }
141
+ return value;
142
+ }
143
+ return this._valueAsBuffer;
144
+ }
145
+
146
+ /**
147
+ * @returns {SourceValue} source
148
+ */
149
+ source() {
150
+ if (this._valueAsString === undefined) {
151
+ const value =
152
+ /** @type {Buffer} */
153
+ (this._valueAsBuffer).toString("utf8");
154
+ if (isDualStringBufferCachingEnabled()) {
155
+ this._valueAsString = value;
156
+ }
157
+ return value;
158
+ }
159
+ return this._valueAsString;
160
+ }
161
+
162
+ /**
163
+ * @private
164
+ * @returns {undefined | Buffer} buffer
165
+ */
166
+ _originalSourceBuffer() {
167
+ if (this._originalSourceAsBuffer === undefined && this._hasOriginalSource) {
168
+ const value = Buffer.from(
169
+ /** @type {string} */
170
+ (this._originalSourceAsString),
171
+ "utf8",
172
+ );
173
+ if (isDualStringBufferCachingEnabled()) {
174
+ this._originalSourceAsBuffer = value;
175
+ }
176
+ return value;
177
+ }
178
+ return this._originalSourceAsBuffer;
179
+ }
180
+
181
+ _originalSourceString() {
182
+ if (this._originalSourceAsString === undefined && this._hasOriginalSource) {
183
+ const value =
184
+ /** @type {Buffer} */
185
+ (this._originalSourceAsBuffer).toString("utf8");
186
+ if (isDualStringBufferCachingEnabled()) {
187
+ this._originalSourceAsString = value;
188
+ }
189
+ return value;
190
+ }
191
+ return this._originalSourceAsString;
192
+ }
193
+
194
+ _innerSourceMapObject() {
195
+ if (this._innerSourceMapAsObject === undefined && this._hasInnerSourceMap) {
196
+ const value = JSON.parse(this._innerSourceMapString());
197
+ if (isDualStringBufferCachingEnabled()) {
198
+ this._innerSourceMapAsObject = value;
199
+ }
200
+ return value;
201
+ }
202
+ return this._innerSourceMapAsObject;
203
+ }
204
+
205
+ _innerSourceMapBuffer() {
206
+ if (this._innerSourceMapAsBuffer === undefined && this._hasInnerSourceMap) {
207
+ const value = Buffer.from(this._innerSourceMapString(), "utf8");
208
+ if (isDualStringBufferCachingEnabled()) {
209
+ this._innerSourceMapAsBuffer = value;
210
+ }
211
+ return value;
212
+ }
213
+ return this._innerSourceMapAsBuffer;
214
+ }
215
+
216
+ /**
217
+ * @private
218
+ * @returns {string} result
219
+ */
220
+ _innerSourceMapString() {
221
+ if (this._innerSourceMapAsString === undefined && this._hasInnerSourceMap) {
222
+ if (this._innerSourceMapAsBuffer !== undefined) {
223
+ const value = this._innerSourceMapAsBuffer.toString("utf8");
224
+ if (isDualStringBufferCachingEnabled()) {
225
+ this._innerSourceMapAsString = value;
226
+ }
227
+ return value;
228
+ }
229
+ const value = JSON.stringify(this._innerSourceMapAsObject);
230
+ if (isDualStringBufferCachingEnabled()) {
231
+ this._innerSourceMapAsString = value;
232
+ }
233
+ return value;
234
+ }
235
+ return /** @type {string} */ (this._innerSourceMapAsString);
236
+ }
237
+
238
+ _sourceMapObject() {
239
+ if (this._sourceMapAsObject === undefined) {
240
+ const value = JSON.parse(this._sourceMapString());
241
+ if (isDualStringBufferCachingEnabled()) {
242
+ this._sourceMapAsObject = value;
243
+ }
244
+ return value;
245
+ }
246
+ return this._sourceMapAsObject;
247
+ }
248
+
249
+ _sourceMapBuffer() {
250
+ if (this._sourceMapAsBuffer === undefined) {
251
+ const value = Buffer.from(this._sourceMapString(), "utf8");
252
+ if (isDualStringBufferCachingEnabled()) {
253
+ this._sourceMapAsBuffer = value;
254
+ }
255
+ return value;
256
+ }
257
+ return this._sourceMapAsBuffer;
258
+ }
259
+
260
+ _sourceMapString() {
261
+ if (this._sourceMapAsString === undefined) {
262
+ if (this._sourceMapAsBuffer !== undefined) {
263
+ const value = this._sourceMapAsBuffer.toString("utf8");
264
+ if (isDualStringBufferCachingEnabled()) {
265
+ this._sourceMapAsString = value;
266
+ }
267
+ return value;
268
+ }
269
+ const value = JSON.stringify(this._sourceMapAsObject);
270
+ if (isDualStringBufferCachingEnabled()) {
271
+ this._sourceMapAsString = value;
272
+ }
273
+ return value;
274
+ }
275
+ return this._sourceMapAsString;
276
+ }
277
+
278
+ /**
279
+ * @param {MapOptions=} options map options
280
+ * @returns {RawSourceMap | null} map
281
+ */
282
+ map(options) {
283
+ if (!this._hasInnerSourceMap) {
284
+ return this._sourceMapObject();
285
+ }
286
+ return getMap(this, options);
287
+ }
288
+
289
+ /**
290
+ * @param {MapOptions=} options map options
291
+ * @returns {SourceAndMap} source and map
292
+ */
293
+ sourceAndMap(options) {
294
+ if (!this._hasInnerSourceMap) {
295
+ return {
296
+ source: this.source(),
297
+ map: this._sourceMapObject(),
298
+ };
299
+ }
300
+ return getSourceAndMap(this, options);
301
+ }
302
+
303
+ /**
304
+ * @param {Options} options options
305
+ * @param {OnChunk} onChunk called for each chunk of code
306
+ * @param {OnSource} onSource called for each source
307
+ * @param {OnName} onName called for each name
308
+ * @returns {GeneratedSourceInfo} generated source info
309
+ */
310
+ streamChunks(options, onChunk, onSource, onName) {
311
+ if (this._hasInnerSourceMap) {
312
+ return streamChunksOfCombinedSourceMap(
313
+ /** @type {string} */
314
+ (this.source()),
315
+ this._sourceMapObject(),
316
+ this._name,
317
+ /** @type {string} */
318
+ (this._originalSourceString()),
319
+ this._innerSourceMapObject(),
320
+ this._removeOriginalSource,
321
+ onChunk,
322
+ onSource,
323
+ onName,
324
+ Boolean(options && options.finalSource),
325
+ Boolean(options && options.columns !== false),
326
+ );
327
+ }
328
+ return streamChunksOfSourceMap(
329
+ /** @type {string} */
330
+ (this.source()),
331
+ this._sourceMapObject(),
332
+ onChunk,
333
+ onSource,
334
+ onName,
335
+ Boolean(options && options.finalSource),
336
+ Boolean(options && options.columns !== false),
337
+ );
338
+ }
339
+
340
+ /**
341
+ * @param {HashLike} hash hash
342
+ * @returns {void}
343
+ */
344
+ updateHash(hash) {
345
+ hash.update("SourceMapSource");
346
+ hash.update(this.buffer());
347
+ hash.update(this._sourceMapBuffer());
348
+
349
+ if (this._hasOriginalSource) {
350
+ hash.update(
351
+ /** @type {Buffer} */
352
+ (this._originalSourceBuffer()),
353
+ );
354
+ }
355
+
356
+ if (this._hasInnerSourceMap) {
357
+ hash.update(
358
+ /** @type {Buffer} */
359
+ (this._innerSourceMapBuffer()),
360
+ );
361
+ }
362
+
363
+ hash.update(this._removeOriginalSource ? "true" : "false");
364
+ }
365
+ }
366
+
367
+ module.exports = SourceMapSource;
@@ -0,0 +1,225 @@
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
+ * @callback MappingsSerializer
10
+ * @param {number} generatedLine generated line
11
+ * @param {number} generatedColumn generated column
12
+ * @param {number} sourceIndex source index
13
+ * @param {number} originalLine original line
14
+ * @param {number} originalColumn generated line
15
+ * @param {number} nameIndex generated line
16
+ * @returns {string} result
17
+ */
18
+
19
+ const ALPHABET = [
20
+ ..."ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
21
+ ];
22
+
23
+ const CONTINUATION_BIT = 0x20;
24
+
25
+ const createFullMappingsSerializer = () => {
26
+ let currentLine = 1;
27
+ let currentColumn = 0;
28
+ let currentSourceIndex = 0;
29
+ let currentOriginalLine = 1;
30
+ let currentOriginalColumn = 0;
31
+ let currentNameIndex = 0;
32
+ let activeMapping = false;
33
+ let activeName = false;
34
+ let initial = true;
35
+ /** @type {MappingsSerializer} */
36
+ return (
37
+ generatedLine,
38
+ generatedColumn,
39
+ sourceIndex,
40
+ originalLine,
41
+ originalColumn,
42
+ nameIndex,
43
+ ) => {
44
+ if (activeMapping && currentLine === generatedLine) {
45
+ // A mapping is still active
46
+ if (
47
+ sourceIndex === currentSourceIndex &&
48
+ originalLine === currentOriginalLine &&
49
+ originalColumn === currentOriginalColumn &&
50
+ !activeName &&
51
+ nameIndex < 0
52
+ ) {
53
+ // avoid repeating the same original mapping
54
+ return "";
55
+ }
56
+ }
57
+ // No mapping is active
58
+ else if (sourceIndex < 0) {
59
+ // avoid writing unneccessary generated mappings
60
+ return "";
61
+ }
62
+
63
+ /** @type {undefined | string} */
64
+ let str;
65
+ if (currentLine < generatedLine) {
66
+ str = ";".repeat(generatedLine - currentLine);
67
+ currentLine = generatedLine;
68
+ currentColumn = 0;
69
+ initial = false;
70
+ } else if (initial) {
71
+ str = "";
72
+ initial = false;
73
+ } else {
74
+ str = ",";
75
+ }
76
+
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);
98
+ currentColumn = generatedColumn;
99
+ if (sourceIndex >= 0) {
100
+ activeMapping = true;
101
+ if (sourceIndex === currentSourceIndex) {
102
+ str += "A";
103
+ } else {
104
+ writeValue(sourceIndex - currentSourceIndex);
105
+ currentSourceIndex = sourceIndex;
106
+ }
107
+ writeValue(originalLine - currentOriginalLine);
108
+ currentOriginalLine = originalLine;
109
+ if (originalColumn === currentOriginalColumn) {
110
+ str += "A";
111
+ } else {
112
+ writeValue(originalColumn - currentOriginalColumn);
113
+ currentOriginalColumn = originalColumn;
114
+ }
115
+ if (nameIndex >= 0) {
116
+ writeValue(nameIndex - currentNameIndex);
117
+ currentNameIndex = nameIndex;
118
+ activeName = true;
119
+ } else {
120
+ activeName = false;
121
+ }
122
+ } else {
123
+ activeMapping = false;
124
+ }
125
+ return str;
126
+ };
127
+ };
128
+
129
+ const createLinesOnlyMappingsSerializer = () => {
130
+ let lastWrittenLine = 0;
131
+ let currentLine = 1;
132
+ let currentSourceIndex = 0;
133
+ let currentOriginalLine = 1;
134
+ /** @type {MappingsSerializer} */
135
+ return (
136
+ generatedLine,
137
+ _generatedColumn,
138
+ sourceIndex,
139
+ originalLine,
140
+ _originalColumn,
141
+ _nameIndex,
142
+ ) => {
143
+ if (sourceIndex < 0) {
144
+ // avoid writing generated mappings at all
145
+ return "";
146
+ }
147
+ if (lastWrittenLine === generatedLine) {
148
+ // avoid writing multiple original mappings per line
149
+ return "";
150
+ }
151
+ /** @type {undefined | string} */
152
+ 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
+ lastWrittenLine = generatedLine;
174
+ if (generatedLine === currentLine + 1) {
175
+ currentLine = generatedLine;
176
+ if (sourceIndex === currentSourceIndex) {
177
+ if (originalLine === currentOriginalLine + 1) {
178
+ currentOriginalLine = originalLine;
179
+ return ";AACA";
180
+ }
181
+ str = ";AA";
182
+ writeValue(originalLine - currentOriginalLine);
183
+ currentOriginalLine = originalLine;
184
+ return `${str}A`;
185
+ }
186
+ str = ";A";
187
+ writeValue(sourceIndex - currentSourceIndex);
188
+ currentSourceIndex = sourceIndex;
189
+ writeValue(originalLine - currentOriginalLine);
190
+ currentOriginalLine = originalLine;
191
+ return `${str}A`;
192
+ }
193
+ str = ";".repeat(generatedLine - currentLine);
194
+ currentLine = generatedLine;
195
+ if (sourceIndex === currentSourceIndex) {
196
+ if (originalLine === currentOriginalLine + 1) {
197
+ currentOriginalLine = originalLine;
198
+ return `${str}AACA`;
199
+ }
200
+ str += "AA";
201
+ writeValue(originalLine - currentOriginalLine);
202
+ currentOriginalLine = originalLine;
203
+ return `${str}A`;
204
+ }
205
+ str += "A";
206
+ writeValue(sourceIndex - currentSourceIndex);
207
+ currentSourceIndex = sourceIndex;
208
+ writeValue(originalLine - currentOriginalLine);
209
+ currentOriginalLine = originalLine;
210
+ return `${str}A`;
211
+ };
212
+ };
213
+
214
+ /**
215
+ * @param {{ columns?: boolean }=} options options
216
+ * @returns {MappingsSerializer} mappings serializer
217
+ */
218
+ const createMappingsSerializer = (options) => {
219
+ const linesOnly = options && options.columns === false;
220
+ return linesOnly
221
+ ? createLinesOnlyMappingsSerializer()
222
+ : createFullMappingsSerializer();
223
+ };
224
+
225
+ module.exports = createMappingsSerializer;