-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathexplore.ts
344 lines (272 loc) · 8.67 KB
/
explore.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import convert from 'convert-source-map';
import path from 'path';
import { BasicSourceMapConsumer, IndexedSourceMapConsumer, SourceMapConsumer } from 'source-map';
import gzipSize from 'gzip-size';
import { mapKeys } from 'lodash';
import { getBundleName } from './api';
import {
getFileContent,
getCommonPathPrefix,
getFirstRegexMatch,
detectEOL,
getOccurrencesCount,
isEOLAtPosition,
mergeRanges,
} from './helpers';
import { AppError } from './app-error';
import { setCoveredSizes } from './coverage';
import type {
Bundle,
CoverageRange,
ExploreBundleResult,
ExploreOptions,
File,
FileDataMap,
FileSizes,
MappingRange,
} from './types';
export const UNMAPPED_KEY = '[unmapped]';
export const SOURCE_MAP_COMMENT_KEY = '[sourceMappingURL]';
export const NO_SOURCE_KEY = '[no source]';
export const EOL_KEY = '[EOLs]';
export const SPECIAL_FILENAMES = [UNMAPPED_KEY, SOURCE_MAP_COMMENT_KEY, NO_SOURCE_KEY, EOL_KEY];
/**
* Analyze a bundle
*/
export async function exploreBundle(
bundle: Bundle,
options: ExploreOptions
): Promise<ExploreBundleResult> {
const { code, map, coverageRanges } = bundle;
const sourceMapData = await loadSourceMap(code, map);
const sizes = computeFileSizes(sourceMapData, options, coverageRanges);
const files = adjustSourcePaths(sizes.files, options);
// Free Wasm data
sourceMapData.consumer.destroy();
return {
bundleName: getBundleName(bundle),
...sizes,
files,
};
}
type Consumer = BasicSourceMapConsumer | IndexedSourceMapConsumer;
interface SourceMapData {
consumer: Consumer;
codeFileContent: string;
}
/**
* Get source map
*/
async function loadSourceMap(codeFile: File, sourceMapFile?: File): Promise<SourceMapData> {
const codeFileContent = getFileContent(codeFile);
let consumer: Consumer;
if (sourceMapFile) {
const sourceMapFileContent = getFileContent(sourceMapFile);
consumer = await new SourceMapConsumer(sourceMapFileContent);
} else {
// Try to read a source map from a 'sourceMappingURL' comment.
let converter = convert.fromSource(codeFileContent);
if (!converter && !Buffer.isBuffer(codeFile)) {
converter = convert.fromMapFileSource(codeFileContent, path.dirname(codeFile));
}
if (!converter) {
throw new AppError({ code: 'NoSourceMap' });
}
consumer = await new SourceMapConsumer(converter.toJSON());
}
if (!consumer) {
throw new AppError({ code: 'NoSourceMap' });
}
return {
consumer,
codeFileContent,
};
}
const COMMENT_REGEX = convert.commentRegex;
const MAP_FILE_COMMENT_REGEX = convert.mapFileCommentRegex;
/**
* Extract either source map comment/file
*/
function getSourceMapComment(fileContent: string): string {
const sourceMapComment =
getFirstRegexMatch(COMMENT_REGEX, fileContent) ||
getFirstRegexMatch(MAP_FILE_COMMENT_REGEX, fileContent) ||
'';
// Remove trailing EOLs
return sourceMapComment.trim();
}
interface ComputeFileSizesContext {
generatedLine: number;
generatedColumn: number;
line: string;
source: string | null;
consumer: Consumer;
mapReferenceEOLSources: Set<string>;
}
/**
* Check if source map references EOL (see https://github.com/microsoft/TypeScript/issues/34695)
*/
function isReferencingEOL(context: ComputeFileSizesContext, maxColumnIndex: number): boolean {
const { generatedLine, generatedColumn, source, consumer } = context;
// Ignore difference more than EOL max length (\r\n)
if (maxColumnIndex - generatedColumn > 2) {
return false;
}
// Ignore mapping w/o source
if (!source) {
return false;
}
// Don't check the same source twice. It covers most cases even though not 100% reliable
if (context.mapReferenceEOLSources.has(source)) {
return true;
}
const content = consumer.sourceContentFor(source, true);
// Content is needed to detect EOL
if (!content) {
return false;
}
const { line, column } = consumer.originalPositionFor({
line: generatedLine,
column: generatedColumn,
});
if (line === null || column === null) {
return false;
}
if (isEOLAtPosition(content, [line, column])) {
context.mapReferenceEOLSources.add(source);
return true;
}
return false;
}
function checkInvalidMappingColumn(context: ComputeFileSizesContext): void {
const { line, generatedLine, generatedColumn } = context;
const maxColumnIndex = line.length - 1;
if (generatedColumn > maxColumnIndex) {
if (isReferencingEOL(context, maxColumnIndex)) {
return;
}
throw new AppError({
code: 'InvalidMappingColumn',
generatedLine,
generatedColumn,
maxColumn: line.length,
});
}
}
/**
* Calculate the number of bytes contributed by each source file
*/
function computeFileSizes(
sourceMapData: SourceMapData,
options: ExploreOptions,
coverageRanges?: CoverageRange[][]
): FileSizes {
const { consumer, codeFileContent: fileContent } = sourceMapData;
const sourceMapComment = getSourceMapComment(fileContent);
// Remove inline source map comment, source map file comment and trailing EOLs
const sourceContent = fileContent.replace(sourceMapComment, '').trim();
const eol = detectEOL(fileContent);
// Assume only one type of EOL is used
const lines = sourceContent.split(eol);
const mappingRanges: MappingRange[][] = [];
const context: ComputeFileSizesContext = {
generatedLine: -1,
generatedColumn: -1,
line: '',
source: null,
consumer,
mapReferenceEOLSources: new Set(),
};
consumer.computeColumnSpans();
consumer.eachMapping(({ source, generatedLine, generatedColumn, lastGeneratedColumn }) => {
// Columns are 0-based, Lines are 1-based
const lineIndex = generatedLine - 1;
const line = lines[lineIndex];
if (line === undefined) {
if (options.noBorderChecks) {
return;
}
throw new AppError({
code: 'InvalidMappingLine',
generatedLine,
maxLine: lines.length,
});
}
context.generatedLine = generatedLine;
context.generatedColumn = lastGeneratedColumn || generatedColumn;
context.line = line;
context.source = source;
if (!options.noBorderChecks) {
checkInvalidMappingColumn(context);
}
const start = generatedColumn;
const end = lastGeneratedColumn === null ? line.length - 1 : lastGeneratedColumn;
const lineRanges = mappingRanges[lineIndex] || [];
lineRanges.push({
start,
end,
source: source === null ? NO_SOURCE_KEY : source,
});
mappingRanges[lineIndex] = lineRanges;
});
let files: FileDataMap = {};
let mappedBytes = 0;
// To account unicode measure byte length rather than symbols count
const getSize = options.gzip ? gzipSize.sync : Buffer.byteLength;
mappingRanges.forEach((lineRanges, lineIndex) => {
const line = lines[lineIndex];
const mergedRanges = mergeRanges(lineRanges);
mergedRanges.forEach(({ start, end, source }) => {
const rangeString = line.substring(start, end + 1);
const rangeByteLength = getSize(rangeString);
if (!files[source]) {
files[source] = { size: 0 };
}
files[source].size += rangeByteLength;
mappedBytes += rangeByteLength;
});
if (coverageRanges) {
files = setCoveredSizes(line, files, mergedRanges, coverageRanges[lineIndex]);
}
});
const sourceMapCommentBytes = getSize(sourceMapComment);
const eolBytes = getOccurrencesCount(eol, fileContent) * Buffer.byteLength(eol);
const totalBytes = getSize(fileContent);
let unmappedBytes: number | undefined;
if (!options.excludeSourceMapComment) {
files[SOURCE_MAP_COMMENT_KEY] = { size: sourceMapCommentBytes };
}
if (!options.onlyMapped) {
unmappedBytes = totalBytes - mappedBytes - sourceMapCommentBytes - eolBytes;
files[UNMAPPED_KEY] = { size: unmappedBytes };
}
if (eolBytes > 0) {
files[EOL_KEY] = { size: eolBytes };
}
return {
...(options.excludeSourceMapComment
? { totalBytes: totalBytes - sourceMapCommentBytes }
: { totalBytes }),
mappedBytes,
...(!options.onlyMapped && { unmappedBytes }),
eolBytes,
sourceMapCommentBytes,
files,
};
}
export function adjustSourcePaths(fileSizeMap: FileDataMap, options: ExploreOptions): FileDataMap {
if (!options.noRoot) {
const prefix = getCommonPathPrefix(Object.keys(fileSizeMap));
const length = prefix.length;
if (length) {
fileSizeMap = mapKeys(fileSizeMap, (size, source) => source.slice(length));
}
}
if (options.replaceMap) {
fileSizeMap = Object.entries(options.replaceMap).reduce((result, [before, after]) => {
const regexp = new RegExp(before, 'g');
return mapKeys(result, (size, source) => source.replace(regexp, after));
}, fileSizeMap);
}
return fileSizeMap;
}