forked from leebenson/reactql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.ts
69 lines (56 loc) · 1.75 KB
/
stats.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
/*
A `Stats` instance wraps client/server Webpack stats to provide
helper functions to obtain chunk names, etc.
*/
// ----------------------------------------------------------------------------
// IMPORTS
/* NPM */
import * as lodash from "lodash";
// ----------------------------------------------------------------------------
export interface IStats {
assetsByChunkName?: {
main: string | string[];
};
}
// Config for `Stats` instances
const config = new WeakMap<Stats, IStats>();
export default class Stats {
// --------------------------------------------------------------------------
/* PUBLIC METHODS */
// --------------------------------------------------------------------------
/* CONSTRUCTOR */
public constructor(stats: IStats = {}) {
// Store a raw copy of the config
config.set(this, stats);
}
/* GETTERS */
// Get the full, raw stats
public get raw(): any {
return config.get(this)!;
}
// Get main built asset based on file extension
public main(ext: string): string | undefined {
const main: string | string[] = lodash.get(
config.get(this)!,
"assetsByChunkName.main",
[]
);
const file = (Array.isArray(main) ? main : [main]).find((c: string) =>
c.endsWith(`.${ext}`)
);
return file && `/${file}`;
}
public scripts(): string[] {
const initial = this.raw.chunks.find((chunk: any) => chunk.initial);
const scripts: string[] = initial.siblings
.map((sibling: any) =>
this.raw.chunks.find((chunk: any) => chunk.id === sibling)
)
.map((sibling: any) => sibling.files)
.concat(initial.files)
.flat()
.filter((file: string) => file.endsWith(".js"))
.map((file: string) => `/${file}`);
return scripts;
}
}