@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,366 @@
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 splitIntoLines = require("./splitIntoLines");
9
+ const streamChunksOfSourceMap = require("./streamChunksOfSourceMap");
10
+
11
+ /** @typedef {import("../Source").RawSourceMap} RawSourceMap */
12
+ /** @typedef {import("./getGeneratedSourceInfo").GeneratedSourceInfo} GeneratedSourceInfo */
13
+ /** @typedef {import("./streamChunks").OnChunk} onChunk */
14
+ /** @typedef {import("./streamChunks").OnName} OnName */
15
+ /** @typedef {import("./streamChunks").OnSource} OnSource */
16
+
17
+ /**
18
+ * @param {string} source source
19
+ * @param {RawSourceMap} sourceMap source map
20
+ * @param {string} innerSourceName inner source name
21
+ * @param {string} innerSource inner source
22
+ * @param {RawSourceMap} innerSourceMap inner source map
23
+ * @param {boolean | undefined} removeInnerSource do remove inner source
24
+ * @param {onChunk} onChunk on chunk
25
+ * @param {OnSource} onSource on source
26
+ * @param {OnName} onName on name
27
+ * @param {boolean} finalSource finalSource
28
+ * @param {boolean} columns columns
29
+ * @returns {GeneratedSourceInfo} generated source info
30
+ */
31
+ const streamChunksOfCombinedSourceMap = (
32
+ source,
33
+ sourceMap,
34
+ innerSourceName,
35
+ innerSource,
36
+ innerSourceMap,
37
+ removeInnerSource,
38
+ onChunk,
39
+ onSource,
40
+ onName,
41
+ finalSource,
42
+ columns,
43
+ ) => {
44
+ /** @type {Map<string | null, number>} */
45
+ const sourceMapping = new Map();
46
+ /** @type {Map<string, number>} */
47
+ const nameMapping = new Map();
48
+ /** @type {number[]} */
49
+ const sourceIndexMapping = [];
50
+ /** @type {number[]} */
51
+ const nameIndexMapping = [];
52
+ /** @type {string[]} */
53
+ const nameIndexValueMapping = [];
54
+ let outerSourceIndex = -2;
55
+ /** @type {number[]} */
56
+ const innerSourceIndexMapping = [];
57
+ /** @type {[string | null, string | undefined][]} */
58
+ const innerSourceIndexValueMapping = [];
59
+ /** @type {(string | undefined)[]} */
60
+ const innerSourceContents = [];
61
+ /** @type {(null | undefined | string[])[]} */
62
+ const innerSourceContentLines = [];
63
+ /** @type {number[]} */
64
+ const innerNameIndexMapping = [];
65
+ /** @type {string[]} */
66
+ const innerNameIndexValueMapping = [];
67
+ /** @typedef {[number, number, number, number, number] | number[]} MappingsData */
68
+ /** @type {{ chunks: string[], mappingsData: MappingsData }[]} */
69
+ const innerSourceMapLineData = [];
70
+ /**
71
+ * @param {number} line line
72
+ * @param {number} column column
73
+ * @returns {number} result
74
+ */
75
+ const findInnerMapping = (line, column) => {
76
+ if (line > innerSourceMapLineData.length) return -1;
77
+ const { mappingsData } = innerSourceMapLineData[line - 1];
78
+ let l = 0;
79
+ let r = mappingsData.length / 5;
80
+ while (l < r) {
81
+ const m = (l + r) >> 1;
82
+ if (mappingsData[m * 5] <= column) {
83
+ l = m + 1;
84
+ } else {
85
+ r = m;
86
+ }
87
+ }
88
+ if (l === 0) return -1;
89
+ return l - 1;
90
+ };
91
+ return streamChunksOfSourceMap(
92
+ source,
93
+ sourceMap,
94
+ (
95
+ chunk,
96
+ generatedLine,
97
+ generatedColumn,
98
+ sourceIndex,
99
+ originalLine,
100
+ originalColumn,
101
+ nameIndex,
102
+ ) => {
103
+ // Check if this is a mapping to the inner source
104
+ if (sourceIndex === outerSourceIndex) {
105
+ // Check if there is a mapping in the inner source
106
+ const idx = findInnerMapping(originalLine, originalColumn);
107
+ if (idx !== -1) {
108
+ const { chunks, mappingsData } =
109
+ innerSourceMapLineData[originalLine - 1];
110
+ const mi = idx * 5;
111
+ const innerSourceIndex = mappingsData[mi + 1];
112
+ const innerOriginalLine = mappingsData[mi + 2];
113
+ let innerOriginalColumn = mappingsData[mi + 3];
114
+ let innerNameIndex = mappingsData[mi + 4];
115
+ if (innerSourceIndex >= 0) {
116
+ // Check for an identity mapping
117
+ // where we are allowed to adjust the original column
118
+ const innerChunk = chunks[idx];
119
+ const innerGeneratedColumn = mappingsData[mi];
120
+ const locationInChunk = originalColumn - innerGeneratedColumn;
121
+ if (locationInChunk > 0) {
122
+ let originalSourceLines =
123
+ innerSourceIndex < innerSourceContentLines.length
124
+ ? innerSourceContentLines[innerSourceIndex]
125
+ : null;
126
+ if (originalSourceLines === undefined) {
127
+ const originalSource = innerSourceContents[innerSourceIndex];
128
+ originalSourceLines = originalSource
129
+ ? splitIntoLines(originalSource)
130
+ : null;
131
+ innerSourceContentLines[innerSourceIndex] = originalSourceLines;
132
+ }
133
+ if (originalSourceLines !== null) {
134
+ const originalChunk =
135
+ innerOriginalLine <= originalSourceLines.length
136
+ ? originalSourceLines[innerOriginalLine - 1].slice(
137
+ innerOriginalColumn,
138
+ innerOriginalColumn + locationInChunk,
139
+ )
140
+ : "";
141
+ if (innerChunk.slice(0, locationInChunk) === originalChunk) {
142
+ innerOriginalColumn += locationInChunk;
143
+ innerNameIndex = -1;
144
+ }
145
+ }
146
+ }
147
+
148
+ // We have a inner mapping to original source
149
+
150
+ // emit source when needed and compute global source index
151
+ let sourceIndex =
152
+ innerSourceIndex < innerSourceIndexMapping.length
153
+ ? innerSourceIndexMapping[innerSourceIndex]
154
+ : -2;
155
+ if (sourceIndex === -2) {
156
+ const [source, sourceContent] =
157
+ innerSourceIndex < innerSourceIndexValueMapping.length
158
+ ? innerSourceIndexValueMapping[innerSourceIndex]
159
+ : [null, undefined];
160
+ let globalIndex = sourceMapping.get(source);
161
+ if (globalIndex === undefined) {
162
+ sourceMapping.set(source, (globalIndex = sourceMapping.size));
163
+ onSource(globalIndex, source, sourceContent);
164
+ }
165
+ sourceIndex = globalIndex;
166
+ innerSourceIndexMapping[innerSourceIndex] = sourceIndex;
167
+ }
168
+
169
+ // emit name when needed and compute global name index
170
+ let finalNameIndex = -1;
171
+ if (innerNameIndex >= 0) {
172
+ // when we have a inner name
173
+ finalNameIndex =
174
+ innerNameIndex < innerNameIndexMapping.length
175
+ ? innerNameIndexMapping[innerNameIndex]
176
+ : -2;
177
+ if (finalNameIndex === -2) {
178
+ const name =
179
+ innerNameIndex < innerNameIndexValueMapping.length
180
+ ? innerNameIndexValueMapping[innerNameIndex]
181
+ : undefined;
182
+ if (name) {
183
+ let globalIndex = nameMapping.get(name);
184
+ if (globalIndex === undefined) {
185
+ nameMapping.set(name, (globalIndex = nameMapping.size));
186
+ onName(globalIndex, name);
187
+ }
188
+ finalNameIndex = globalIndex;
189
+ } else {
190
+ finalNameIndex = -1;
191
+ }
192
+ innerNameIndexMapping[innerNameIndex] = finalNameIndex;
193
+ }
194
+ } else if (nameIndex >= 0) {
195
+ // when we don't have an inner name,
196
+ // but we have an outer name
197
+ // it can be used when inner original code equals to the name
198
+ let originalSourceLines =
199
+ innerSourceContentLines[innerSourceIndex];
200
+ if (originalSourceLines === undefined) {
201
+ const originalSource = innerSourceContents[innerSourceIndex];
202
+ originalSourceLines = originalSource
203
+ ? splitIntoLines(originalSource)
204
+ : null;
205
+ innerSourceContentLines[innerSourceIndex] = originalSourceLines;
206
+ }
207
+ if (originalSourceLines !== null) {
208
+ const name = nameIndexValueMapping[nameIndex];
209
+ const originalName =
210
+ innerOriginalLine <= originalSourceLines.length
211
+ ? originalSourceLines[innerOriginalLine - 1].slice(
212
+ innerOriginalColumn,
213
+ innerOriginalColumn + name.length,
214
+ )
215
+ : "";
216
+ if (name === originalName) {
217
+ finalNameIndex =
218
+ nameIndex < nameIndexMapping.length
219
+ ? nameIndexMapping[nameIndex]
220
+ : -2;
221
+ if (finalNameIndex === -2) {
222
+ const name = nameIndexValueMapping[nameIndex];
223
+ if (name) {
224
+ let globalIndex = nameMapping.get(name);
225
+ if (globalIndex === undefined) {
226
+ nameMapping.set(name, (globalIndex = nameMapping.size));
227
+ onName(globalIndex, name);
228
+ }
229
+ finalNameIndex = globalIndex;
230
+ } else {
231
+ finalNameIndex = -1;
232
+ }
233
+ nameIndexMapping[nameIndex] = finalNameIndex;
234
+ }
235
+ }
236
+ }
237
+ }
238
+ onChunk(
239
+ chunk,
240
+ generatedLine,
241
+ generatedColumn,
242
+ sourceIndex,
243
+ innerOriginalLine,
244
+ innerOriginalColumn,
245
+ finalNameIndex,
246
+ );
247
+ return;
248
+ }
249
+ }
250
+
251
+ // We have a mapping to the inner source, but no inner mapping
252
+ if (removeInnerSource) {
253
+ onChunk(chunk, generatedLine, generatedColumn, -1, -1, -1, -1);
254
+ return;
255
+ }
256
+ if (sourceIndexMapping[sourceIndex] === -2) {
257
+ let globalIndex = sourceMapping.get(innerSourceName);
258
+ if (globalIndex === undefined) {
259
+ sourceMapping.set(source, (globalIndex = sourceMapping.size));
260
+ onSource(globalIndex, innerSourceName, innerSource);
261
+ }
262
+ sourceIndexMapping[sourceIndex] = globalIndex;
263
+ }
264
+ }
265
+
266
+ const finalSourceIndex =
267
+ sourceIndex < 0 || sourceIndex >= sourceIndexMapping.length
268
+ ? -1
269
+ : sourceIndexMapping[sourceIndex];
270
+ if (finalSourceIndex < 0) {
271
+ // no source, so we make it a generated chunk
272
+ onChunk(chunk, generatedLine, generatedColumn, -1, -1, -1, -1);
273
+ } else {
274
+ // Pass through the chunk with mapping
275
+ let finalNameIndex = -1;
276
+ if (nameIndex >= 0 && nameIndex < nameIndexMapping.length) {
277
+ finalNameIndex = nameIndexMapping[nameIndex];
278
+ if (finalNameIndex === -2) {
279
+ const name = nameIndexValueMapping[nameIndex];
280
+ let globalIndex = nameMapping.get(name);
281
+ if (globalIndex === undefined) {
282
+ nameMapping.set(name, (globalIndex = nameMapping.size));
283
+ onName(globalIndex, name);
284
+ }
285
+ finalNameIndex = globalIndex;
286
+ nameIndexMapping[nameIndex] = finalNameIndex;
287
+ }
288
+ }
289
+ onChunk(
290
+ chunk,
291
+ generatedLine,
292
+ generatedColumn,
293
+ finalSourceIndex,
294
+ originalLine,
295
+ originalColumn,
296
+ finalNameIndex,
297
+ );
298
+ }
299
+ },
300
+ (i, source, sourceContent) => {
301
+ if (source === innerSourceName) {
302
+ outerSourceIndex = i;
303
+ if (innerSource !== undefined) sourceContent = innerSource;
304
+ else innerSource = /** @type {string} */ (sourceContent);
305
+ sourceIndexMapping[i] = -2;
306
+ streamChunksOfSourceMap(
307
+ /** @type {string} */
308
+ (sourceContent),
309
+ innerSourceMap,
310
+ (
311
+ chunk,
312
+ generatedLine,
313
+ generatedColumn,
314
+ sourceIndex,
315
+ originalLine,
316
+ originalColumn,
317
+ nameIndex,
318
+ ) => {
319
+ while (innerSourceMapLineData.length < generatedLine) {
320
+ innerSourceMapLineData.push({
321
+ mappingsData: [],
322
+ chunks: [],
323
+ });
324
+ }
325
+ const data = innerSourceMapLineData[generatedLine - 1];
326
+ data.mappingsData.push(
327
+ generatedColumn,
328
+ sourceIndex,
329
+ originalLine,
330
+ originalColumn,
331
+ nameIndex,
332
+ );
333
+ data.chunks.push(/** @type {string} */ (chunk));
334
+ },
335
+ (i, source, sourceContent) => {
336
+ innerSourceContents[i] = sourceContent;
337
+ innerSourceContentLines[i] = undefined;
338
+ innerSourceIndexMapping[i] = -2;
339
+ innerSourceIndexValueMapping[i] = [source, sourceContent];
340
+ },
341
+ (i, name) => {
342
+ innerNameIndexMapping[i] = -2;
343
+ innerNameIndexValueMapping[i] = name;
344
+ },
345
+ false,
346
+ columns,
347
+ );
348
+ } else {
349
+ let globalIndex = sourceMapping.get(source);
350
+ if (globalIndex === undefined) {
351
+ sourceMapping.set(source, (globalIndex = sourceMapping.size));
352
+ onSource(globalIndex, source, sourceContent);
353
+ }
354
+ sourceIndexMapping[i] = globalIndex;
355
+ }
356
+ },
357
+ (i, name) => {
358
+ nameIndexMapping[i] = -2;
359
+ nameIndexValueMapping[i] = name;
360
+ },
361
+ finalSource,
362
+ columns,
363
+ );
364
+ };
365
+
366
+ module.exports = streamChunksOfCombinedSourceMap;
@@ -0,0 +1,54 @@
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 getGeneratedSourceInfo = require("./getGeneratedSourceInfo");
9
+ const splitIntoLines = require("./splitIntoLines");
10
+
11
+ /** @typedef {import("./getGeneratedSourceInfo").GeneratedSourceInfo} GeneratedSourceInfo */
12
+ /** @typedef {import("./streamChunks").OnChunk} OnChunk */
13
+ /** @typedef {import("./streamChunks").OnName} OnName */
14
+ /** @typedef {import("./streamChunks").OnSource} OnSource */
15
+
16
+ /**
17
+ * @param {string} source source
18
+ * @param {OnChunk} onChunk on chunk
19
+ * @param {OnSource} _onSource on source
20
+ * @param {OnName} _onName on name
21
+ * @returns {GeneratedSourceInfo} source info
22
+ */
23
+ const streamChunksOfRawSource = (source, onChunk, _onSource, _onName) => {
24
+ let line = 1;
25
+ const matches = splitIntoLines(source);
26
+ /** @type {undefined | string} */
27
+ let match;
28
+ for (match of matches) {
29
+ onChunk(match, line, 0, -1, -1, -1, -1);
30
+ line++;
31
+ }
32
+ return matches.length === 0 || /** @type {string} */ (match).endsWith("\n")
33
+ ? {
34
+ generatedLine: matches.length + 1,
35
+ generatedColumn: 0,
36
+ }
37
+ : {
38
+ generatedLine: matches.length,
39
+ generatedColumn: /** @type {string} */ (match).length,
40
+ };
41
+ };
42
+
43
+ /**
44
+ * @param {string} source source
45
+ * @param {OnChunk} onChunk on chunk
46
+ * @param {OnSource} onSource on source
47
+ * @param {OnName} onName on name
48
+ * @param {boolean} finalSource is final source
49
+ * @returns {GeneratedSourceInfo} source info
50
+ */
51
+ module.exports = (source, onChunk, onSource, onName, finalSource) =>
52
+ finalSource
53
+ ? getGeneratedSourceInfo(source)
54
+ : streamChunksOfRawSource(source, onChunk, onSource, onName);