-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathname-to-size-map.ts
More file actions
66 lines (63 loc) · 1.82 KB
/
name-to-size-map.ts
File metadata and controls
66 lines (63 loc) · 1.82 KB
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
import {StatsCompilation} from 'webpack'
import type {Sizes, ChunkSizes} from './types'
export function assetNameToSizeMap(
statAssets: StatsCompilation['assets'] = []
): Map<string, Sizes> {
return new Map(
statAssets
// when Webpack's stats.excludeAssets is used, assets which are excluded will be grouped into an asset with type 'hidden assets'
.filter(it => !!it.name && it.type !== 'hidden assets')
.map(asset => {
let gzipSize: number | null = null
if (asset.related && Array.isArray(asset.related)) {
const gzipAsset = asset.related.find(
related => related.type === 'gzipped'
)
if (gzipAsset) {
gzipSize = gzipAsset.size
}
}
return [
asset.name,
{
size: asset.size,
gzipSize
}
]
})
)
}
export function chunkModuleNameToSizeMap(
statChunks: StatsCompilation['chunks'] = []
): Map<string, ChunkSizes> {
return new Map(
statChunks.flatMap(chunk => {
if (!chunk.modules) return []
return chunk.modules.flatMap(module => {
// If a module doesn't have any submodules beneath it, then just return its own size
// Otherwise, break each module into its submodules with their own sizes
if (module.modules) {
return module.modules.map(submodule => [
submodule.name ?? '',
{
initial: chunk.initial,
size: submodule.size ?? 0,
gzipSize: null
}
])
} else {
return [
[
module.name ?? '',
{
initial: chunk.initial,
size: module.size ?? 0,
gzipSize: null
}
]
]
}
})
})
)
}