forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbudget-stats.ts
83 lines (73 loc) · 2.35 KB
/
budget-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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import type { Metafile } from 'esbuild';
import type { BudgetStats } from '../../utils/bundle-calculator';
import {
type BuildOutputFile,
BuildOutputFileType,
type InitialFileRecord,
} from './bundler-context';
import { getChunkNameFromMetafile } from './utils';
/**
* Generates a bundle budget calculator compatible stats object that provides
* the necessary information for the Webpack-based bundle budget code to
* interoperate with the esbuild-based builders.
* @param metafile The esbuild metafile of a build to use.
* @param initialFiles The records of all initial files of a build.
* @returns A bundle budget compatible stats object.
*/
export function generateBudgetStats(
metafile: Metafile,
outputFiles: BuildOutputFile[],
initialFiles: Map<string, InitialFileRecord>,
): BudgetStats {
const stats: Required<BudgetStats> = {
chunks: [],
assets: [],
};
for (const { path: file, size, type } of outputFiles) {
if (!file.endsWith('.js') && !file.endsWith('.css')) {
continue;
}
// Exclude server bundles
if (type === BuildOutputFileType.ServerApplication || type === BuildOutputFileType.ServerRoot) {
continue;
}
const initialRecord = initialFiles.get(file);
const name = initialRecord?.name ?? getChunkNameFromMetafile(metafile, file);
stats.chunks.push({
files: [file],
initial: !!initialRecord,
names: name ? [name] : undefined,
});
stats.assets.push({
name: file,
size,
});
}
// Add component styles from metafile
// TODO: Provide this information directly from the AOT compiler
for (const [file, entry] of Object.entries(metafile.outputs)) {
if (!file.endsWith('.css')) {
continue;
}
// 'ng-component' is set by the angular plugin's component stylesheet bundler
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const componentStyle: boolean = (entry as any)['ng-component'];
if (!componentStyle) {
continue;
}
stats.assets.push({
// Component styles use the input file
name: Object.keys(entry.inputs)[0],
size: entry.bytes,
componentStyle,
});
}
return stats;
}