/
util.ts
40 lines (35 loc) · 1.09 KB
/
util.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
import type { Plugin as VitePlugin } from 'vite';
// eslint-disable-next-line @typescript-eslint/ban-types
type OutputOptionsHook = Extract<VitePlugin['outputOptions'], Function>;
type OutputOptions = Parameters<OutputOptionsHook>[0];
type ExtendManualChunksHooks = {
before?: (id: string, meta: any) => string | undefined;
after?: (id: string, meta: any) => string | undefined;
};
export function extendManualChunks(outputOptions: OutputOptions, hooks: ExtendManualChunksHooks) {
const manualChunks = outputOptions.manualChunks;
outputOptions.manualChunks = function (id, meta) {
if (hooks.before) {
let value = hooks.before(id, meta);
if (value) {
return value;
}
}
// Defer to user-provided `manualChunks`, if it was provided.
if (typeof manualChunks == 'object') {
if (id in manualChunks) {
let value = manualChunks[id];
return value[0];
}
} else if (typeof manualChunks === 'function') {
const outid = manualChunks.call(this, id, meta);
if (outid) {
return outid;
}
}
if (hooks.after) {
return hooks.after(id, meta) || null;
}
return null;
};
}