-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
hookActions.ts
45 lines (40 loc) · 1.2 KB
/
hookActions.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
import process from 'process';
const unfulfilledActions = new Set<[string, string, Parameters<any>]>();
export function addUnresolvedAction(actionTuple: [string, string, Parameters<any>]): void {
unfulfilledActions.add(actionTuple);
}
export function resolveAction(actionTuple: [string, string, Parameters<any>]): void {
unfulfilledActions.delete(actionTuple);
}
function formatAction([pluginName, hookName, args]: [string, string, Parameters<any>]): string {
let action = `(${pluginName}) ${hookName}`;
const s = JSON.stringify;
switch (hookName) {
case 'resolveId':
action += ` ${s(args[0])} ${s(args[1])}`;
break;
case 'load':
action += ` ${s(args[0])}`;
break;
case 'transform':
action += ` ${s(args[1])}`;
break;
case 'shouldTransformCachedModule':
action += ` ${s((args[0] as { id: string }).id)}`;
break;
case 'moduleParsed':
action += ` ${s((args[0] as { id: string }).id)}`;
break;
}
return action;
}
process.on('exit', () => {
if (unfulfilledActions.size) {
let err = '[!] Error: unfinished hook action(s) on exit:\n';
for (const action of unfulfilledActions) {
err += formatAction(action) + '\n';
}
console.error('%s', err);
process.exit(1);
}
});