-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
batchWarnings.ts
321 lines (269 loc) · 8.29 KB
/
batchWarnings.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
import tc from 'turbocolor';
import { RollupWarning } from '../../../src/rollup/types';
import relativeId from '../../../src/utils/relativeId';
import { stderr } from '../logging';
export interface BatchWarnings {
add: (warning: string | RollupWarning) => void;
readonly count: number;
flush: () => void;
}
export default function batchWarnings() {
let allWarnings = new Map<string, RollupWarning[]>();
let count = 0;
return {
get count() {
return count;
},
add: (warning: string | RollupWarning) => {
if (typeof warning === 'string') {
warning = { code: 'UNKNOWN', message: warning };
}
if ((warning.code as string) in immediateHandlers) {
immediateHandlers[warning.code as string](warning);
return;
}
if (!allWarnings.has(warning.code as string)) allWarnings.set(warning.code as string, []);
(allWarnings.get(warning.code as string) as RollupWarning[]).push(warning);
count += 1;
},
flush: () => {
if (count === 0) return;
const codes = Array.from(allWarnings.keys()).sort((a, b) => {
if (deferredHandlers[a] && deferredHandlers[b]) {
return deferredHandlers[a].priority - deferredHandlers[b].priority;
}
if (deferredHandlers[a]) return -1;
if (deferredHandlers[b]) return 1;
return (
(allWarnings.get(b) as RollupWarning[]).length -
(allWarnings.get(a) as RollupWarning[]).length
);
});
codes.forEach(code => {
const handler = deferredHandlers[code];
const warnings = allWarnings.get(code);
if (handler) {
handler.fn(warnings as RollupWarning[]);
} else {
(warnings as RollupWarning[]).forEach(warning => {
title(warning.message);
if (warning.url) info(warning.url);
const id = (warning.loc && warning.loc.file) || warning.id;
if (id) {
const loc = warning.loc
? `${relativeId(id)}: (${warning.loc.line}:${warning.loc.column})`
: relativeId(id);
stderr(tc.bold(relativeId(loc)));
}
if (warning.frame) info(warning.frame);
});
}
});
allWarnings = new Map();
count = 0;
}
};
}
const immediateHandlers: {
[code: string]: (warning: RollupWarning) => void;
} = {
UNKNOWN_OPTION: warning => {
title(`You have passed an unrecognized option`);
stderr(warning.message);
},
MISSING_NODE_BUILTINS: warning => {
title(`Missing shims for Node.js built-ins`);
const detail =
(warning.modules as string[]).length === 1
? `'${(warning.modules as string[])[0]}'`
: `${(warning.modules as string[])
.slice(0, -1)
.map((name: string) => `'${name}'`)
.join(', ')} and '${(warning.modules as string[]).slice(-1)}'`;
stderr(
`Creating a browser bundle that depends on ${detail}. You might need to include https://www.npmjs.com/package/rollup-plugin-node-builtins`
);
},
MIXED_EXPORTS: () => {
title('Mixing named and default exports');
stderr(
`Consumers of your bundle will have to use bundle['default'] to access the default export, which may not be what you want. Use \`output.exports: 'named'\` to disable this warning`
);
},
EMPTY_BUNDLE: () => {
title(`Generated an empty bundle`);
}
};
// TODO select sensible priorities
const deferredHandlers: {
[code: string]: {
fn: (warnings: RollupWarning[]) => void;
priority: number;
};
} = {
UNUSED_EXTERNAL_IMPORT: {
fn: warnings => {
title('Unused external imports');
warnings.forEach(warning => {
stderr(`${warning.names} imported from external module '${warning.source}' but never used`);
});
},
priority: 1
},
UNRESOLVED_IMPORT: {
fn: warnings => {
title('Unresolved dependencies');
info('https://rollupjs.org/guide/en/#warning-treating-module-as-external-dependency');
const dependencies = new Map();
warnings.forEach(warning => {
if (!dependencies.has(warning.source)) dependencies.set(warning.source, []);
dependencies.get(warning.source).push(warning.importer);
});
Array.from(dependencies.keys()).forEach(dependency => {
const importers = dependencies.get(dependency);
stderr(`${tc.bold(dependency)} (imported by ${importers.join(', ')})`);
});
},
priority: 1
},
MISSING_EXPORT: {
fn: warnings => {
title('Missing exports');
info('https://rollupjs.org/guide/en/#error-name-is-not-exported-by-module-');
warnings.forEach(warning => {
stderr(tc.bold(warning.importer as string));
stderr(`${warning.missing} is not exported by ${warning.exporter}`);
stderr(tc.gray(warning.frame as string));
});
},
priority: 1
},
THIS_IS_UNDEFINED: {
fn: warnings => {
title('`this` has been rewritten to `undefined`');
info('https://rollupjs.org/guide/en/#error-this-is-undefined');
showTruncatedWarnings(warnings);
},
priority: 1
},
EVAL: {
fn: warnings => {
title('Use of eval is strongly discouraged');
info('https://rollupjs.org/guide/en/#avoiding-eval');
showTruncatedWarnings(warnings);
},
priority: 1
},
NON_EXISTENT_EXPORT: {
fn: warnings => {
title(`Import of non-existent ${warnings.length > 1 ? 'exports' : 'export'}`);
showTruncatedWarnings(warnings);
},
priority: 1
},
NAMESPACE_CONFLICT: {
fn: warnings => {
title(`Conflicting re-exports`);
warnings.forEach(warning => {
stderr(
`${tc.bold(relativeId(warning.reexporter as string))} re-exports '${
warning.name
}' from both ${relativeId((warning.sources as string[])[0])} and ${relativeId(
(warning.sources as string[])[1]
)} (will be ignored)`
);
});
},
priority: 1
},
MISSING_GLOBAL_NAME: {
fn: warnings => {
title(`Missing global variable ${warnings.length > 1 ? 'names' : 'name'}`);
stderr(
`Use output.globals to specify browser global variable names corresponding to external modules`
);
warnings.forEach(warning => {
stderr(`${tc.bold(warning.source as string)} (guessing '${warning.guess}')`);
});
},
priority: 1
},
SOURCEMAP_BROKEN: {
fn: warnings => {
title(`Broken sourcemap`);
info('https://rollupjs.org/guide/en/#warning-sourcemap-is-likely-to-be-incorrect');
const plugins = Array.from(new Set(warnings.map(w => w.plugin).filter(Boolean)));
const detail =
plugins.length === 0
? ''
: plugins.length > 1
? ` (such as ${plugins
.slice(0, -1)
.map(p => `'${p}'`)
.join(', ')} and '${plugins.slice(-1)}')`
: ` (such as '${plugins[0]}')`;
stderr(`Plugins that transform code${detail} should generate accompanying sourcemaps`);
},
priority: 1
},
PLUGIN_WARNING: {
fn: warnings => {
const nestedByPlugin = nest(warnings, 'plugin');
nestedByPlugin.forEach(({ key: plugin, items }) => {
const nestedByMessage = nest(items, 'message');
let lastUrl: string;
nestedByMessage.forEach(({ key: message, items }) => {
title(`Plugin ${plugin}: ${message}`);
items.forEach(warning => {
if (warning.url !== lastUrl) info((lastUrl = warning.url as string));
if (warning.id) {
let loc = relativeId(warning.id);
if (warning.loc) {
loc += `: (${warning.loc.line}:${warning.loc.column})`;
}
stderr(tc.bold(loc));
}
if (warning.frame) info(warning.frame);
});
});
});
},
priority: 1
}
};
function title(str: string) {
stderr(`${tc.bold.yellow('(!)')} ${tc.bold.yellow(str)}`);
}
function info(url: string) {
stderr(tc.gray(url));
}
function nest<T>(array: T[], prop: string) {
const nested: { items: T[]; key: string }[] = [];
const lookup = new Map<string, { items: T[]; key: string }>();
array.forEach(item => {
const key = (item as any)[prop];
if (!lookup.has(key)) {
lookup.set(key, {
items: [],
key
});
nested.push(lookup.get(key) as { items: T[]; key: string });
}
(lookup.get(key) as { items: T[]; key: string }).items.push(item);
});
return nested;
}
function showTruncatedWarnings(warnings: RollupWarning[]) {
const nestedByModule = nest(warnings, 'id');
const sliced = nestedByModule.length > 5 ? nestedByModule.slice(0, 3) : nestedByModule;
sliced.forEach(({ key: id, items }) => {
stderr(tc.bold(relativeId(id)));
stderr(tc.gray(items[0].frame as string));
if (items.length > 1) {
stderr(`...and ${items.length - 1} other ${items.length > 2 ? 'occurrences' : 'occurrence'}`);
}
});
if (nestedByModule.length > sliced.length) {
stderr(`\n...and ${nestedByModule.length - sliced.length} other files`);
}
}