@akanjs/devkit 3.2.0-rc.5 → 3.2.0-rc.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +20 -6
- package/apiReport/apiDeclarationPrinter.ts +120 -0
- package/apiReport/apiReport.test.ts +497 -0
- package/apiReport/apiReport.ts +232 -0
- package/apiReport/apiReportDiff.ts +126 -0
- package/apiReport/apiReportPolicy.ts +65 -0
- package/apiReport/apiTargets.ts +311 -0
- package/apiReport/changesetBump.ts +19 -0
- package/applicationBuildReporter.test.ts +21 -0
- package/applicationBuildReporter.ts +8 -0
- package/applicationBuildRunner.ts +5 -1
- package/artifact/implicitRootLayout.ts +12 -1
- package/frontendBuild/allRoutesBuilder.test.ts +16 -4
- package/frontendBuild/allRoutesBuilder.ts +24 -6
- package/frontendBuild/clientBuildTypes.ts +5 -1
- package/frontendBuild/clientEntryDiscovery.test.ts +28 -0
- package/frontendBuild/clientEntryDiscovery.ts +25 -6
- package/package.json +3 -3
- package/routeJsBudget.test.ts +47 -11
- package/routeJsBudget.ts +88 -25
|
@@ -4,7 +4,7 @@ import { SOURCE_EXTS } from "../akanApp/devHostPolicy";
|
|
|
4
4
|
import type { App } from "../commandDecorators";
|
|
5
5
|
import { BarrelAnalyzer, type BarrelExportMap } from "../transforms/barrelAnalyzer";
|
|
6
6
|
import { createTsconfigPackageResolver, rewriteBarrelImports } from "../transforms/barrelImportsPlugin";
|
|
7
|
-
import type { AkanConfig, ClientEntryDiscovery, ScannedImport } from "./clientBuildTypes";
|
|
7
|
+
import type { AkanConfig, ClientEntryDiscovery, DiscoverOptions, ScannedImport } from "./clientBuildTypes";
|
|
8
8
|
|
|
9
9
|
const USE_CLIENT_RE = /^\s*(?:\/\*[^*]*\*+(?:[^/*][^*]*\*+)*\/\s*|\/\/[^\n]*\n\s*)*["']use client["']/;
|
|
10
10
|
const NODE_MODULES_RE = /[\\/]node_modules[\\/]/;
|
|
@@ -31,6 +31,8 @@ interface DiscoveryWalk {
|
|
|
31
31
|
//? Every file's result within this walk, partial ones included: the file the cycle went back to is an ancestor here
|
|
32
32
|
//? and brings the rest itself, and re-walking each path through a dense cycle grows exponentially.
|
|
33
33
|
memo: Map<string, Set<string>>;
|
|
34
|
+
importsAs?: DiscoverOptions["importsAs"];
|
|
35
|
+
reachable: Map<string, Set<string>>;
|
|
34
36
|
}
|
|
35
37
|
|
|
36
38
|
const exportsKeyOf = (map: BarrelExportMap | null): string =>
|
|
@@ -50,6 +52,8 @@ export class GraphClientEntryDiscovery implements ClientEntryDiscovery {
|
|
|
50
52
|
#resolvedFileCache = new Map<string, Lookup<string | null>>();
|
|
51
53
|
#resolvedSpecifierCache = new Map<string, Lookup<string | null>>();
|
|
52
54
|
#reachableEntriesCache = new Map<string, Set<string>>();
|
|
55
|
+
//? Keyed by a walk's `importsAs`: a file's answer under swapped imports holds only for walks swapping the same ones.
|
|
56
|
+
#swappedEntriesCaches = new WeakMap<object, Map<string, Set<string>>>();
|
|
53
57
|
#readFiles = new Set<string>();
|
|
54
58
|
#barrelImporters = new Map<string, Set<string>>();
|
|
55
59
|
//? Barrels a save went beside, by the analysis before it: a save of any component re-reads its barrel, and only an
|
|
@@ -70,17 +74,29 @@ export class GraphClientEntryDiscovery implements ClientEntryDiscovery {
|
|
|
70
74
|
return new GraphClientEntryDiscovery(await app.getConfig(), await createTsconfigPackageResolver(app));
|
|
71
75
|
}
|
|
72
76
|
|
|
73
|
-
async discover(seeds: string[]): Promise<string[]> {
|
|
77
|
+
async discover(seeds: string[], { importsAs }: DiscoverOptions = {}): Promise<string[]> {
|
|
74
78
|
this.#settled = this.#settled.then(() => this.#dropRenarrowed());
|
|
75
79
|
await this.#settled;
|
|
76
80
|
const entries = new Set<string>();
|
|
81
|
+
const reachable = importsAs ? this.#swappedEntriesCache(importsAs) : this.#reachableEntriesCache;
|
|
77
82
|
for (const seed of seeds) {
|
|
78
|
-
const walk: DiscoveryWalk = { visiting: new Set(), partial: new Set(), memo: new Map() };
|
|
83
|
+
const walk: DiscoveryWalk = { visiting: new Set(), partial: new Set(), memo: new Map(), importsAs, reachable };
|
|
79
84
|
for (const entry of await this.#discoverFromFile(seed, walk)) entries.add(entry);
|
|
80
85
|
}
|
|
81
86
|
return Array.from(entries).sort();
|
|
82
87
|
}
|
|
83
88
|
|
|
89
|
+
async resolveImport(spec: string, importerDir: string): Promise<string | null> {
|
|
90
|
+
const resolved = await this.#resolveSpecifier(spec, importerDir);
|
|
91
|
+
return resolved ? path.resolve(resolved) : null;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
#swappedEntriesCache(importsAs: object): Map<string, Set<string>> {
|
|
95
|
+
const cache = this.#swappedEntriesCaches.get(importsAs) ?? new Map<string, Set<string>>();
|
|
96
|
+
this.#swappedEntriesCaches.set(importsAs, cache);
|
|
97
|
+
return cache;
|
|
98
|
+
}
|
|
99
|
+
|
|
84
100
|
invalidate(files: string[]): void {
|
|
85
101
|
if (files.length === 0) return;
|
|
86
102
|
let moved = false;
|
|
@@ -95,6 +111,7 @@ export class GraphClientEntryDiscovery implements ClientEntryDiscovery {
|
|
|
95
111
|
this.#epoch += 1;
|
|
96
112
|
// Reachable-entry results are transitive, so a changed child invalidates every ancestor.
|
|
97
113
|
this.#reachableEntriesCache.clear();
|
|
114
|
+
this.#swappedEntriesCaches = new WeakMap();
|
|
98
115
|
for (const [barrel, before] of this.#analyzer.forget(files))
|
|
99
116
|
if (!this.#reread.has(barrel)) this.#reread.set(barrel, before);
|
|
100
117
|
//? A file gone, or one never read, can leave a found resolution on its old target (`Chart.tsx` to
|
|
@@ -223,7 +240,8 @@ export class GraphClientEntryDiscovery implements ClientEntryDiscovery {
|
|
|
223
240
|
async #discoverFromFile(file: string, walk: DiscoveryWalk): Promise<Set<string>> {
|
|
224
241
|
const epoch = this.#epoch;
|
|
225
242
|
const absPath = path.resolve(file);
|
|
226
|
-
const
|
|
243
|
+
const importsAs = walk.importsAs?.get(absPath);
|
|
244
|
+
const cached = walk.reachable.get(absPath);
|
|
227
245
|
if (cached) return new Set(cached);
|
|
228
246
|
const memo = walk.memo.get(absPath);
|
|
229
247
|
if (memo) {
|
|
@@ -253,7 +271,8 @@ export class GraphClientEntryDiscovery implements ClientEntryDiscovery {
|
|
|
253
271
|
|
|
254
272
|
const importerDir = path.dirname(absPath);
|
|
255
273
|
for (const imp of facts.imports) {
|
|
256
|
-
const
|
|
274
|
+
const swapped = importsAs?.get(imp.path);
|
|
275
|
+
const spec = swapped === undefined ? imp.path : swapped;
|
|
257
276
|
if (!spec) continue;
|
|
258
277
|
if (NON_SOURCE_EXT_RE.test(spec)) continue;
|
|
259
278
|
const resolved = await this.#resolveSpecifier(spec, importerDir);
|
|
@@ -268,7 +287,7 @@ export class GraphClientEntryDiscovery implements ClientEntryDiscovery {
|
|
|
268
287
|
#finishDiscovery(absPath: string, walk: DiscoveryWalk, entries: Set<string>, epoch: number): Set<string> {
|
|
269
288
|
walk.visiting.delete(absPath);
|
|
270
289
|
walk.memo.set(absPath, entries);
|
|
271
|
-
if (epoch === this.#epoch && !walk.partial.has(absPath))
|
|
290
|
+
if (epoch === this.#epoch && !walk.partial.has(absPath)) walk.reachable.set(absPath, entries);
|
|
272
291
|
return new Set(entries);
|
|
273
292
|
}
|
|
274
293
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@akanjs/devkit",
|
|
3
|
-
"version": "3.2.0-rc.
|
|
3
|
+
"version": "3.2.0-rc.7",
|
|
4
4
|
"sourceType": "module",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"publishConfig": {
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
"./*": "./*.ts"
|
|
42
42
|
},
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@akanjs/language": "3.2.0-rc.
|
|
44
|
+
"@akanjs/language": "3.2.0-rc.7",
|
|
45
45
|
"@earendil-works/pi-agent-core": "1.3.0",
|
|
46
46
|
"@earendil-works/pi-ai": "1.3.0",
|
|
47
47
|
"@earendil-works/pi-codemode": "1.3.0",
|
|
@@ -50,7 +50,7 @@
|
|
|
50
50
|
"@earendil-works/pi-tui": "1.3.0",
|
|
51
51
|
"@inquirer/prompts": "^8.7.3",
|
|
52
52
|
"@tailwindcss/node": "^4.3.3",
|
|
53
|
-
"akanjs": "3.2.0-rc.
|
|
53
|
+
"akanjs": "3.2.0-rc.7",
|
|
54
54
|
"chalk": "^6.0.1",
|
|
55
55
|
"commander": "^15.0.0",
|
|
56
56
|
"dayjs": "^1.11.23",
|
package/routeJsBudget.test.ts
CHANGED
|
@@ -96,30 +96,50 @@ describe("RouteJsBudget", () => {
|
|
|
96
96
|
const setup = async (text?: string) => {
|
|
97
97
|
const budgetPath = path.join(await makeTempRoot(), "akan.budget.json");
|
|
98
98
|
if (text !== undefined) await write(budgetPath, text);
|
|
99
|
-
return { budgetPath, source: "apps/shop/akan.budget.json", environment: "local" };
|
|
99
|
+
return { app: "shop", budgetPath, source: "apps/shop/akan.budget.json", environment: "local" };
|
|
100
100
|
};
|
|
101
101
|
|
|
102
|
-
test("enforce fails a route over its limit
|
|
102
|
+
test("enforce fails a route over its limit, a route with no entry, a file it cannot read, and an app with no file", async () => {
|
|
103
103
|
const over = await setup(JSON.stringify({ env: "main", routeJsBrotliKB: { "/:lang/memo": 120 } }));
|
|
104
104
|
await expect(RouteJsBudget.check(sizes, { ...over, mode: "enforce" })).rejects.toThrow(
|
|
105
105
|
"1 route over the JS budget in apps/shop/akan.budget.json (recorded for main, built for local):",
|
|
106
106
|
);
|
|
107
|
+
await expect(RouteJsBudget.check(sizes, { ...over, mode: "enforce" })).rejects.toThrow(
|
|
108
|
+
"in apps/shop/akan.budget.json by hand.\n1 route with no entry in apps/shop/akan.budget.json",
|
|
109
|
+
);
|
|
110
|
+
const missing = await setup(JSON.stringify({ routeJsBrotliKB: { "/:lang/memo": 130 } }));
|
|
111
|
+
await expect(RouteJsBudget.check(sizes, { ...missing, mode: "enforce" })).rejects.toThrow(
|
|
112
|
+
[
|
|
113
|
+
"1 route with no entry in apps/shop/akan.budget.json, which --check-budget checks every route against:",
|
|
114
|
+
" /:lang/new",
|
|
115
|
+
"Record it in the same change: `akan build shop --update-budget` adds each new route at its measured size.",
|
|
116
|
+
].join("\n"),
|
|
117
|
+
);
|
|
107
118
|
const broken = await setup("{");
|
|
108
119
|
await expect(RouteJsBudget.check(sizes, { ...broken, mode: "enforce" })).rejects.toThrow("is not valid JSON");
|
|
120
|
+
await expect(RouteJsBudget.check(sizes, { ...(await setup()), mode: "enforce" })).rejects.toThrow(
|
|
121
|
+
"--check-budget found no apps/shop/akan.budget.json: record one with `akan build shop --update-budget`.",
|
|
122
|
+
);
|
|
109
123
|
});
|
|
110
124
|
|
|
111
|
-
test("report warns of an overage and a file it cannot read, and fails
|
|
125
|
+
test("report warns of an overage, a route with no entry and a file it cannot read, and fails none", async () => {
|
|
126
|
+
const unbudgetedWarning = {
|
|
127
|
+
file: "apps/shop/akan.budget.json",
|
|
128
|
+
message: "/:lang/new has no entry; akan build --check-budget fails on it",
|
|
129
|
+
fix: "record it with akan build shop --update-budget",
|
|
130
|
+
};
|
|
112
131
|
const over = await setup(JSON.stringify({ env: "main", routeJsBrotliKB: { "/:lang/memo": 120 } }));
|
|
113
132
|
expect(await RouteJsBudget.check(sizes, { ...over, mode: "report" })).toEqual({
|
|
114
133
|
summary:
|
|
115
|
-
"1 route(s) over apps/shop/akan.budget.json (recorded for main, built for local), reported only: /:lang/memo",
|
|
134
|
+
"1 route(s) over apps/shop/akan.budget.json (recorded for main, built for local), reported only: /:lang/memo; 1 route(s) without an entry",
|
|
116
135
|
warnings: [
|
|
117
136
|
{
|
|
118
137
|
file: "apps/shop/akan.budget.json",
|
|
119
138
|
message:
|
|
120
|
-
"/:lang/memo loads 133,120 bytes brotli, over its limit of 126,976 (recorded for main, built for local); akan build fails on it",
|
|
121
|
-
fix: "shrink what the route loads, or raise its entry
|
|
139
|
+
"/:lang/memo loads 133,120 bytes brotli, over its limit of 126,976 (recorded for main, built for local); akan build --check-budget fails on it",
|
|
140
|
+
fix: "shrink what the route loads, or raise its entry with akan build shop --raise-budget",
|
|
122
141
|
},
|
|
142
|
+
unbudgetedWarning,
|
|
123
143
|
],
|
|
124
144
|
});
|
|
125
145
|
const broken = await setup("{");
|
|
@@ -128,8 +148,8 @@ describe("RouteJsBudget", () => {
|
|
|
128
148
|
warnings: [
|
|
129
149
|
{
|
|
130
150
|
file: "apps/shop/akan.budget.json",
|
|
131
|
-
message: "apps/shop/akan.budget.json is not valid JSON; akan build fails on it",
|
|
132
|
-
fix: "correct it, or rewrite it with akan build
|
|
151
|
+
message: "apps/shop/akan.budget.json is not valid JSON; akan build --check-budget fails on it",
|
|
152
|
+
fix: "correct it, or rewrite it with akan build shop --update-budget",
|
|
133
153
|
},
|
|
134
154
|
],
|
|
135
155
|
});
|
|
@@ -138,7 +158,8 @@ describe("RouteJsBudget", () => {
|
|
|
138
158
|
});
|
|
139
159
|
const within = await setup(JSON.stringify({ routeJsBrotliKB: { "/:lang/memo": 130 } }));
|
|
140
160
|
expect(await RouteJsBudget.check(sizes, { ...within, mode: "report" })).toEqual({
|
|
141
|
-
summary: "within apps/shop/akan.budget.json
|
|
161
|
+
summary: "within apps/shop/akan.budget.json; 1 route(s) without an entry",
|
|
162
|
+
warnings: [unbudgetedWarning],
|
|
142
163
|
});
|
|
143
164
|
});
|
|
144
165
|
|
|
@@ -157,20 +178,35 @@ describe("RouteJsBudget", () => {
|
|
|
157
178
|
await expect(RouteJsBudget.check(sizes, { ...over, mode: "update" })).rejects.toThrow("/:lang/memo");
|
|
158
179
|
expect(RouteJsBudget.parse(await Bun.file(over.budgetPath).text()).kbByRoute.get("/:lang/memo")).toBe(120);
|
|
159
180
|
});
|
|
181
|
+
|
|
182
|
+
test("raise re-records every route at its measured size and names the entries it raised", async () => {
|
|
183
|
+
const over = await setup(
|
|
184
|
+
JSON.stringify({ env: "main", routeJsBrotliKB: { "/:lang/memo": 120, "/:lang/new": 90 } }),
|
|
185
|
+
);
|
|
186
|
+
expect(await RouteJsBudget.check(sizes, { ...over, mode: "raise" })).toEqual({
|
|
187
|
+
summary: "apps/shop/akan.budget.json re-recorded at each route's measured size, raised: /:lang/memo",
|
|
188
|
+
});
|
|
189
|
+
const written = RouteJsBudget.parse(await Bun.file(over.budgetPath).text());
|
|
190
|
+
expect(written.env).toBe("local");
|
|
191
|
+
expect(Object.fromEntries(written.kbByRoute)).toEqual({ "/:lang/memo": 130, "/:lang/new": 70 });
|
|
192
|
+
expect(await RouteJsBudget.check(sizes, { ...over, mode: "enforce" })).toEqual({
|
|
193
|
+
summary: "within apps/shop/akan.budget.json",
|
|
194
|
+
});
|
|
195
|
+
});
|
|
160
196
|
});
|
|
161
197
|
|
|
162
198
|
test("names each route over budget, its limit, and how to resolve it", () => {
|
|
163
199
|
expect(
|
|
164
200
|
RouteJsBudget.formatOverages(
|
|
165
201
|
[{ routeId: "/:lang/memo", brotliBytes: 128_000, budgetKB: 120, limitBytes: 126_976 }],
|
|
166
|
-
"apps/shop/akan.budget.json",
|
|
202
|
+
{ app: "shop", source: "apps/shop/akan.budget.json" },
|
|
167
203
|
" (recorded for main, built for local)",
|
|
168
204
|
),
|
|
169
205
|
).toBe(
|
|
170
206
|
[
|
|
171
207
|
"1 route over the JS budget in apps/shop/akan.budget.json (recorded for main, built for local):",
|
|
172
208
|
" /:lang/memo: 128,000 bytes brotli, over its limit of 126,976 (120KB + the larger of 1% and 4KB)",
|
|
173
|
-
|
|
209
|
+
'Shrink what the route loads. If the growth is intended, record it in the same change: `akan build shop --raise-budget` re-records every route at its measured size, or raise the route\'s entry under "routeJsBrotliKB" in apps/shop/akan.budget.json by hand.',
|
|
174
210
|
].join("\n"),
|
|
175
211
|
);
|
|
176
212
|
});
|
package/routeJsBudget.ts
CHANGED
|
@@ -17,7 +17,7 @@ export interface RouteJsOverage {
|
|
|
17
17
|
limitBytes: number;
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
export type RouteJsBudgetMode = "enforce" | "update" | "report" | "off";
|
|
20
|
+
export type RouteJsBudgetMode = "enforce" | "update" | "raise" | "report" | "off";
|
|
21
21
|
|
|
22
22
|
export interface RouteJsBudgetWarning {
|
|
23
23
|
file: string;
|
|
@@ -30,8 +30,20 @@ export interface RouteJsBudgetStatus {
|
|
|
30
30
|
warnings?: RouteJsBudgetWarning[];
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
+
export interface RouteJsBudgetFlags {
|
|
34
|
+
checkBudget?: boolean;
|
|
35
|
+
updateBudget?: boolean;
|
|
36
|
+
raiseBudget?: boolean;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
interface BudgetTarget {
|
|
40
|
+
app: string;
|
|
41
|
+
source: string;
|
|
42
|
+
}
|
|
43
|
+
|
|
33
44
|
export interface RouteJsBudgetCheck {
|
|
34
45
|
mode: Exclude<RouteJsBudgetMode, "off">;
|
|
46
|
+
app: string;
|
|
35
47
|
budgetPath: string;
|
|
36
48
|
source: string;
|
|
37
49
|
environment: string;
|
|
@@ -65,47 +77,85 @@ export class RouteJsBudget {
|
|
|
65
77
|
.sort((a, b) => b.brotliBytes - a.brotliBytes || RouteJsBudget.#byCodeUnit(a.routeId, b.routeId));
|
|
66
78
|
}
|
|
67
79
|
|
|
80
|
+
static modeOf({ checkBudget = false, updateBudget = false, raiseBudget = false }: RouteJsBudgetFlags) {
|
|
81
|
+
if ([checkBudget, updateBudget, raiseBudget].filter(Boolean).length > 1)
|
|
82
|
+
throw new Error("Pass at most one of --check-budget, --update-budget and --raise-budget.");
|
|
83
|
+
return checkBudget ? "enforce" : updateBudget ? "update" : raiseBudget ? "raise" : "report";
|
|
84
|
+
}
|
|
85
|
+
|
|
68
86
|
static async load(filePath: string, source = filePath): Promise<RouteJsBudget | null> {
|
|
69
87
|
const file = Bun.file(filePath);
|
|
70
88
|
if (!(await file.exists())) return null;
|
|
71
89
|
return RouteJsBudget.parse(await file.text(), source);
|
|
72
90
|
}
|
|
73
91
|
|
|
74
|
-
//?
|
|
75
|
-
static async check(
|
|
76
|
-
|
|
77
|
-
{ mode, budgetPath, source, environment }: RouteJsBudgetCheck,
|
|
78
|
-
): Promise<RouteJsBudgetStatus> {
|
|
92
|
+
//? A deploy build and `akan verify`'s edit loop only report: `enforce` (`--check-budget`, run by CI) is the gate.
|
|
93
|
+
static async check(sizes: RouteJsSize[], check: RouteJsBudgetCheck): Promise<RouteJsBudgetStatus> {
|
|
94
|
+
const { mode, app, budgetPath, source, environment } = check;
|
|
79
95
|
const { committed, unreadable } = await RouteJsBudget.#loadFor(mode, budgetPath, source);
|
|
80
|
-
|
|
81
|
-
|
|
96
|
+
if (mode === "enforce" && !committed)
|
|
97
|
+
throw new Error(`--check-budget found no ${source}: record one with \`akan build ${app} --update-budget\`.`);
|
|
98
|
+
const budget =
|
|
99
|
+
mode === "raise"
|
|
100
|
+
? new RouteJsBudget().tightenedBy(sizes, environment)
|
|
101
|
+
: mode === "update"
|
|
102
|
+
? (committed ?? new RouteJsBudget()).tightenedBy(sizes, environment)
|
|
103
|
+
: committed;
|
|
104
|
+
if ((mode === "update" || mode === "raise") && budget) await Bun.write(budgetPath, budget.stringify());
|
|
82
105
|
const overages = budget?.overages(sizes) ?? [];
|
|
106
|
+
const unbudgeted = budget?.unbudgeted(sizes) ?? [];
|
|
83
107
|
const envNote = committed?.envNote(environment) ?? "";
|
|
84
|
-
|
|
85
|
-
const
|
|
86
|
-
|
|
108
|
+
const target = { app, source };
|
|
109
|
+
const failures = [
|
|
110
|
+
...(overages.length && mode !== "report" ? [RouteJsBudget.formatOverages(overages, target, envNote)] : []),
|
|
111
|
+
...(unbudgeted.length && mode === "enforce" ? [RouteJsBudget.formatUnbudgeted(unbudgeted, target)] : []),
|
|
112
|
+
];
|
|
113
|
+
if (failures.length) throw new Error(failures.join("\n"));
|
|
114
|
+
if (mode === "update" || mode === "raise")
|
|
115
|
+
return { summary: RouteJsBudget.#writtenSummary(check, unreadable, committed, budget) };
|
|
87
116
|
if (unreadable)
|
|
88
117
|
return {
|
|
89
118
|
summary: `${unreadable}, not checked`,
|
|
90
119
|
warnings: [
|
|
91
120
|
{
|
|
92
121
|
file: source,
|
|
93
|
-
message: `${unreadable}; akan build fails on it`,
|
|
94
|
-
fix:
|
|
122
|
+
message: `${unreadable}; akan build --check-budget fails on it`,
|
|
123
|
+
fix: `correct it, or rewrite it with akan build ${app} --update-budget`,
|
|
95
124
|
},
|
|
96
125
|
],
|
|
97
126
|
};
|
|
98
127
|
if (!budget) return { summary: `no ${RouteJsBudget.fileName}` };
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
128
|
+
const warnings = [
|
|
129
|
+
...overages.map(({ routeId, brotliBytes, limitBytes }) => ({
|
|
130
|
+
file: source,
|
|
131
|
+
message: `${routeId} loads ${RouteJsBudget.#bytes(brotliBytes)} bytes brotli, over its limit of ${RouteJsBudget.#bytes(limitBytes)}${envNote}; akan build --check-budget fails on it`,
|
|
132
|
+
fix: `shrink what the route loads, or raise its entry with akan build ${app} --raise-budget`,
|
|
133
|
+
})),
|
|
134
|
+
...unbudgeted.map((routeId) => ({
|
|
135
|
+
file: source,
|
|
136
|
+
message: `${routeId} has no entry; akan build --check-budget fails on it`,
|
|
137
|
+
fix: `record it with akan build ${app} --update-budget`,
|
|
138
|
+
})),
|
|
139
|
+
];
|
|
140
|
+
const verdict = overages.length
|
|
141
|
+
? `${overages.length} route(s) over ${source}${envNote}, reported only: ${overages.map(({ routeId }) => routeId).join(", ")}`
|
|
142
|
+
: `within ${source}${envNote}`;
|
|
143
|
+
const unbudgetedNote = unbudgeted.length ? `; ${unbudgeted.length} route(s) without an entry` : "";
|
|
144
|
+
return { summary: `${verdict}${unbudgetedNote}`, ...(warnings.length ? { warnings } : {}) };
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
static #writtenSummary(
|
|
148
|
+
{ mode, source }: RouteJsBudgetCheck,
|
|
149
|
+
unreadable: string | null,
|
|
150
|
+
committed: RouteJsBudget | null,
|
|
151
|
+
written: RouteJsBudget | null,
|
|
152
|
+
) {
|
|
153
|
+
if (unreadable) return `${source} rewritten (${unreadable})`;
|
|
154
|
+
if (mode === "update") return `${source} updated`;
|
|
155
|
+
const raised = [...(written?.kbByRoute ?? [])].filter(
|
|
156
|
+
([routeId, kb]) => kb > (committed?.kbByRoute.get(routeId) ?? kb),
|
|
157
|
+
);
|
|
158
|
+
return `${source} re-recorded at each route's measured size${raised.length ? `, raised: ${raised.map(([routeId]) => routeId).join(", ")}` : ""}`;
|
|
109
159
|
}
|
|
110
160
|
|
|
111
161
|
static async #loadFor(mode: RouteJsBudgetCheck["mode"], budgetPath: string, source: string) {
|
|
@@ -158,18 +208,31 @@ export class RouteJsBudget {
|
|
|
158
208
|
];
|
|
159
209
|
}
|
|
160
210
|
|
|
161
|
-
static formatOverages(overages: RouteJsOverage[],
|
|
211
|
+
static formatOverages(overages: RouteJsOverage[], target: BudgetTarget, envNote = ""): string {
|
|
162
212
|
const bytes = RouteJsBudget.#bytes;
|
|
213
|
+
const { source } = target;
|
|
163
214
|
return [
|
|
164
215
|
`${overages.length} route${overages.length === 1 ? "" : "s"} over the JS budget in ${source}${envNote}:`,
|
|
165
216
|
...overages.map(
|
|
166
217
|
({ routeId, brotliBytes, budgetKB, limitBytes }) =>
|
|
167
218
|
` ${routeId}: ${bytes(brotliBytes)} bytes brotli, over its limit of ${bytes(limitBytes)} (${budgetKB}KB + the larger of 1% and 4KB)`,
|
|
168
219
|
),
|
|
169
|
-
|
|
220
|
+
RouteJsBudget.#overageFix(target),
|
|
221
|
+
].join("\n");
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
static formatUnbudgeted(routeIds: string[], { app, source }: BudgetTarget): string {
|
|
225
|
+
return [
|
|
226
|
+
`${routeIds.length} route${routeIds.length === 1 ? "" : "s"} with no entry in ${source}, which --check-budget checks every route against:`,
|
|
227
|
+
...routeIds.map((routeId) => ` ${routeId}`),
|
|
228
|
+
`Record ${routeIds.length === 1 ? "it" : "them"} in the same change: \`akan build ${app} --update-budget\` adds each new route at its measured size.`,
|
|
170
229
|
].join("\n");
|
|
171
230
|
}
|
|
172
231
|
|
|
232
|
+
static #overageFix({ app, source }: BudgetTarget) {
|
|
233
|
+
return `Shrink what the route loads. If the growth is intended, record it in the same change: \`akan build ${app} --raise-budget\` re-records every route at its measured size, or raise the route's entry under "${RouteJsBudget.section}" in ${source} by hand.`;
|
|
234
|
+
}
|
|
235
|
+
|
|
173
236
|
static #bytes(value: number) {
|
|
174
237
|
return value.toLocaleString("en-US");
|
|
175
238
|
}
|