-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathapi.ts
More file actions
158 lines (129 loc) · 4.15 KB
/
api.ts
File metadata and controls
158 lines (129 loc) · 4.15 KB
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
import glob from 'glob';
import { partition, flatMap, isString } from 'lodash';
import { exploreBundle, UNMAPPED_KEY, SPECIAL_FILENAMES } from './explore';
import { AppError, getErrorMessage } from './app-error';
import {
BundlesAndFileTokens,
ExploreOptions,
ExploreResult,
Bundle,
ExploreErrorResult,
ExploreBundleResult,
} from './index';
import { formatOutput, saveOutputToFile } from './output';
import { addCoverageRanges } from './coverage';
/**
* Analyze bundle(s)
*/
export async function explore(
bundlesAndFileTokens: BundlesAndFileTokens,
options: ExploreOptions = {}
): Promise<ExploreResult> {
bundlesAndFileTokens = Array.isArray(bundlesAndFileTokens)
? bundlesAndFileTokens
: [bundlesAndFileTokens];
if (bundlesAndFileTokens.length === 0) {
throw new AppError({ code: 'NoBundles' });
}
// Separate bundles from file tokens
const [fileTokens, bundles] = partition(bundlesAndFileTokens, isString);
// Get bundles from file tokens
bundles.push(...getBundles(fileTokens));
addCoverageRanges(bundles, options.coverage);
const results = await Promise.all(
bundles.map(bundle =>
exploreBundle(bundle, options).catch<ExploreErrorResult>(error =>
onExploreError(bundle, error)
)
)
);
const exploreResult = getExploreResult(results, options);
// Reject if none of results is successful
if (exploreResult.bundles.length === 0) {
return Promise.reject(exploreResult);
}
saveOutputToFile(exploreResult, options);
return exploreResult;
}
/**
* Expand list of file tokens into a list of bundles
*/
export function getBundles(fileTokens: string[]): Bundle[] {
const filenames = flatMap(fileTokens, filePath =>
glob.hasMagic(filePath) ? expandGlob(filePath) : filePath
);
const [mapFilenames, codeFilenames] = partition(filenames, filename => filename.endsWith('.map'));
return codeFilenames.map<Bundle>(code => ({
code,
map: mapFilenames.find(filename => filename === `${code}.map`),
}));
}
function expandGlob(pattern: string): string[] {
// Make sure pattern match `.map` files as well
if (pattern.endsWith('.js')) {
pattern = `${pattern}?(.map)`;
}
return glob.sync(pattern);
}
export function getBundleName(bundle: Bundle): string {
return Buffer.isBuffer(bundle.code) ? 'Buffer' : bundle.code;
}
/**
* Handle error during bundle processing
*/
function onExploreError(bundle: Bundle, error: NodeJS.ErrnoException): ExploreErrorResult {
return {
bundleName: getBundleName(bundle),
code: error.code || 'Unknown',
message: error.message,
error,
};
}
export function getExploreResult(
results: (ExploreBundleResult | ExploreErrorResult)[],
options: ExploreOptions
): ExploreResult {
const [bundles, errors] = partition(
results,
(result): result is ExploreBundleResult => 'files' in result
);
errors.push(...getPostExploreErrors(bundles));
return {
bundles,
errors,
...(bundles.length > 0 && { output: formatOutput(bundles, options) }),
};
}
function getPostExploreErrors(exploreBundleResults: ExploreBundleResult[]): ExploreErrorResult[] {
const errors: ExploreErrorResult[] = [];
const isSingleBundle = exploreBundleResults.length === 1;
for (const result of exploreBundleResults) {
const { bundleName, files, totalBytes } = result;
// Check if source map contains only one file - this make result useless when exploring single bundle
if (isSingleBundle) {
const filenames = Object.keys(files).filter(
filename => !SPECIAL_FILENAMES.includes(filename)
);
if (filenames.length === 1) {
errors.push({
bundleName,
isWarning: true,
code: 'OneSourceSourceMap',
message: getErrorMessage({ code: 'OneSourceSourceMap', filename: filenames[0] }),
});
}
}
if (files[UNMAPPED_KEY] !== undefined) {
const { size: unmappedBytes } = files[UNMAPPED_KEY];
if (unmappedBytes) {
errors.push({
bundleName,
isWarning: true,
code: 'UnmappedBytes',
message: getErrorMessage({ code: 'UnmappedBytes', unmappedBytes, totalBytes }),
});
}
}
}
return errors;
}