-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
batchWarnings.ts
299 lines (251 loc) · 8.17 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
import { RollupWarning } from '../../src/rollup/types';
import { bold, gray, yellow } from '../../src/utils/colors';
import { getOrCreate } from '../../src/utils/getOrCreate';
import { printQuotedStringList } from '../../src/utils/printStringList';
import relativeId from '../../src/utils/relativeId';
import { stderr } from '../logging';
export interface BatchWarnings {
add: (warning: RollupWarning) => void;
readonly count: number;
flush: () => void;
readonly warningOccurred: boolean;
}
export default function batchWarnings(): BatchWarnings {
let count = 0;
let deferredWarnings = new Map<keyof typeof deferredHandlers, RollupWarning[]>();
let warningOccurred = false;
return {
add: (warning: RollupWarning) => {
count += 1;
warningOccurred = true;
if (warning.code! in deferredHandlers) {
getOrCreate(deferredWarnings, warning.code!, () => []).push(warning);
} else if (warning.code! in immediateHandlers) {
immediateHandlers[warning.code!](warning);
} else {
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(bold(relativeId(loc)));
}
if (warning.frame) info(warning.frame);
}
},
get count() {
return count;
},
flush: () => {
if (count === 0) return;
const codes = Array.from(deferredWarnings.keys()).sort(
(a, b) => deferredWarnings.get(b)!.length - deferredWarnings.get(a)!.length
);
for (const code of codes) {
deferredHandlers[code](deferredWarnings.get(code)!);
}
deferredWarnings = new Map();
count = 0;
},
get warningOccurred() {
return warningOccurred;
}
};
}
const immediateHandlers: {
[code: string]: (warning: RollupWarning) => void;
} = {
MISSING_NODE_BUILTINS: warning => {
title(`Missing shims for Node.js built-ins`);
stderr(
`Creating a browser bundle that depends on ${printQuotedStringList(
warning.modules!
)}. You might need to include https://github.com/snowpackjs/rollup-plugin-polyfill-node`
);
},
UNKNOWN_OPTION: warning => {
title(`You have passed an unrecognized option`);
stderr(warning.message);
}
};
const deferredHandlers: {
[code: string]: (warnings: RollupWarning[]) => void;
} = {
CIRCULAR_DEPENDENCY(warnings) {
title(`Circular dependenc${warnings.length > 1 ? 'ies' : 'y'}`);
const displayed = warnings.length > 5 ? warnings.slice(0, 3) : warnings;
for (const warning of displayed) {
stderr(warning.cycle!.join(' -> '));
}
if (warnings.length > displayed.length) {
stderr(`...and ${warnings.length - displayed.length} more`);
}
},
EMPTY_BUNDLE(warnings) {
title(
`Generated${warnings.length === 1 ? ' an' : ''} empty ${
warnings.length > 1 ? 'chunks' : 'chunk'
}`
);
stderr(warnings.map(warning => warning.chunkName!).join(', '));
},
EVAL(warnings) {
title('Use of eval is strongly discouraged');
info('https://rollupjs.org/guide/en/#avoiding-eval');
showTruncatedWarnings(warnings);
},
MISSING_EXPORT(warnings) {
title('Missing exports');
info('https://rollupjs.org/guide/en/#error-name-is-not-exported-by-module');
for (const warning of warnings) {
stderr(bold(warning.importer!));
stderr(`${warning.missing} is not exported by ${warning.exporter}`);
stderr(gray(warning.frame!));
}
},
MISSING_GLOBAL_NAME(warnings) {
title(`Missing global variable ${warnings.length > 1 ? 'names' : 'name'}`);
stderr(
`Use output.globals to specify browser global variable names corresponding to external modules`
);
for (const warning of warnings) {
stderr(`${bold(warning.source!)} (guessing '${warning.guess}')`);
}
},
MIXED_EXPORTS: warnings => {
title('Mixing named and default exports');
info(`https://rollupjs.org/guide/en/#outputexports`);
stderr(bold('The following entry modules are using named and default exports together:'));
const displayedWarnings = warnings.length > 5 ? warnings.slice(0, 3) : warnings;
for (const warning of displayedWarnings) {
stderr(relativeId(warning.id!));
}
if (displayedWarnings.length < warnings.length) {
stderr(`...and ${warnings.length - displayedWarnings.length} other entry modules`);
}
stderr(
`\nConsumers of your bundle will have to use chunk['default'] to access their default export, which may not be what you want. Use \`output.exports: 'named'\` to disable this warning`
);
},
NAMESPACE_CONFLICT(warnings) {
title(`Conflicting re-exports`);
for (const warning of warnings) {
stderr(
`"${bold(relativeId(warning.reexporter!))}" re-exports "${
warning.name
}" from both "${relativeId(warning.sources![0])}" and "${relativeId(
warning.sources![1]
)}" (will be ignored)`
);
}
},
NON_EXISTENT_EXPORT(warnings) {
title(`Import of non-existent ${warnings.length > 1 ? 'exports' : 'export'}`);
showTruncatedWarnings(warnings);
},
PLUGIN_WARNING(warnings) {
const nestedByPlugin = nest(warnings, 'plugin');
for (const { key: plugin, items } of nestedByPlugin) {
const nestedByMessage = nest(items, 'message');
let lastUrl = '';
for (const { key: message, items } of nestedByMessage) {
title(`Plugin ${plugin}: ${message}`);
for (const warning of items) {
if (warning.url && warning.url !== lastUrl) info((lastUrl = warning.url));
const id = warning.id || warning.loc?.file;
if (id) {
let loc = relativeId(id);
if (warning.loc) {
loc += `: (${warning.loc.line}:${warning.loc.column})`;
}
stderr(bold(loc));
}
if (warning.frame) info(warning.frame);
}
}
}
},
SOURCEMAP_BROKEN(warnings) {
title(`Broken sourcemap`);
info('https://rollupjs.org/guide/en/#warning-sourcemap-is-likely-to-be-incorrect');
const plugins = [
...new Set(warnings.map(warning => warning.plugin).filter(Boolean))
] as string[];
stderr(
`Plugins that transform code (such as ${printQuotedStringList(
plugins
)}) should generate accompanying sourcemaps`
);
},
THIS_IS_UNDEFINED(warnings) {
title('`this` has been rewritten to `undefined`');
info('https://rollupjs.org/guide/en/#error-this-is-undefined');
showTruncatedWarnings(warnings);
},
UNRESOLVED_IMPORT(warnings) {
title('Unresolved dependencies');
info('https://rollupjs.org/guide/en/#warning-treating-module-as-external-dependency');
const dependencies = new Map();
for (const warning of warnings) {
getOrCreate(dependencies, warning.source, () => []).push(warning.importer);
}
for (const dependency of dependencies.keys()) {
const importers = dependencies.get(dependency);
stderr(`${bold(dependency)} (imported by ${importers.join(', ')})`);
}
},
UNUSED_EXTERNAL_IMPORT(warnings) {
title('Unused external imports');
for (const warning of warnings) {
stderr(
warning.names +
' imported from external module "' +
warning.source +
'" but never used in ' +
printQuotedStringList((warning.sources as string[]).map(id => relativeId(id)))
);
}
}
};
function title(str: string): void {
stderr(bold(yellow(`(!) ${str}`)));
}
function info(url: string): void {
stderr(gray(url));
}
interface Nested<T> {
items: T[];
key: string;
}
function nest<T>(array: readonly T[], prop: string): Nested<T>[] {
const nested: Nested<T>[] = [];
const lookup = new Map<string, Nested<T>>();
for (const item of array) {
const key = (item as any)[prop];
getOrCreate(lookup, key, () => {
const items = {
items: [],
key
};
nested.push(items);
return items;
}).items.push(item);
}
return nested;
}
function showTruncatedWarnings(warnings: readonly RollupWarning[]): void {
const nestedByModule = nest(warnings, 'id');
const displayedByModule = nestedByModule.length > 5 ? nestedByModule.slice(0, 3) : nestedByModule;
for (const { key: id, items } of displayedByModule) {
stderr(bold(relativeId(id)));
stderr(gray(items[0].frame!));
if (items.length > 1) {
stderr(`...and ${items.length - 1} other ${items.length > 2 ? 'occurrences' : 'occurrence'}`);
}
}
if (nestedByModule.length > displayedByModule.length) {
stderr(`\n...and ${nestedByModule.length - displayedByModule.length} other files`);
}
}