trivium 0.0.0 → 0.1.1
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/README.md +156 -2
- package/docs/agents.md +71 -0
- package/docs/config.md +81 -0
- package/docs/glossary.md +105 -0
- package/docs/measuring.md +46 -0
- package/docs/name.md +62 -0
- package/index.d.ts +44 -0
- package/package.json +34 -3
- package/src/agent.ts +45 -0
- package/src/args.ts +297 -0
- package/src/bundle.ts +319 -0
- package/src/cache.ts +297 -0
- package/src/cli.ts +4 -0
- package/src/compare.ts +378 -0
- package/src/config.ts +49 -0
- package/src/env.ts +16 -0
- package/src/errors.ts +46 -0
- package/src/format.ts +314 -0
- package/src/help.ts +172 -0
- package/src/index.ts +2 -0
- package/src/interactions.ts +398 -0
- package/src/load-config.ts +208 -0
- package/src/load.ts +300 -0
- package/src/main.ts +241 -0
- package/src/page.ts +263 -0
- package/src/progress.ts +48 -0
- package/src/serve.ts +58 -0
- package/src/stats.ts +37 -0
- package/src/suggest.ts +44 -0
- package/src/vitals.ts +304 -0
- package/index.ts +0 -1
package/src/agent.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { Format } from "./format.ts";
|
|
2
|
+
|
|
3
|
+
// The variables a coding agent sets (cli.md, Detecting an agent). Each
|
|
4
|
+
// tool keeps its own copy, since a package has no runtime dependencies.
|
|
5
|
+
const AGENT_ENV = [
|
|
6
|
+
"AGENT",
|
|
7
|
+
"AI_AGENT",
|
|
8
|
+
"CLAUDECODE",
|
|
9
|
+
"CODEX_CI",
|
|
10
|
+
"CODEX_THREAD_ID",
|
|
11
|
+
] as const;
|
|
12
|
+
|
|
13
|
+
type Env = Readonly<Record<string, string | undefined>>;
|
|
14
|
+
|
|
15
|
+
// Empty or `0` is a person saying "not an agent" without unsetting it.
|
|
16
|
+
export function agentVariable(env: Env): string | undefined {
|
|
17
|
+
return AGENT_ENV.find((name) => {
|
|
18
|
+
const value = env[name];
|
|
19
|
+
return value !== undefined && value !== "" && value !== "0";
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// The format when no flag names one: TRIVIUM_FORMAT, else `agent` for a
|
|
24
|
+
// coding agent, with a stderr line saying why, else `human`.
|
|
25
|
+
export function defaultFormat(
|
|
26
|
+
env: Env,
|
|
27
|
+
formats: readonly Format[],
|
|
28
|
+
): { format: Format; notice?: string } | { error: string } {
|
|
29
|
+
const chosen = env.TRIVIUM_FORMAT;
|
|
30
|
+
if (chosen !== undefined && chosen !== "") {
|
|
31
|
+
const format = formats.find((f) => f === chosen);
|
|
32
|
+
if (format === undefined) {
|
|
33
|
+
return {
|
|
34
|
+
error: `trivium: TRIVIUM_FORMAT takes ${formats.join(", ")}, not ${JSON.stringify(chosen)}`,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
return { format };
|
|
38
|
+
}
|
|
39
|
+
const agent = agentVariable(env);
|
|
40
|
+
if (agent === undefined) return { format: "human" };
|
|
41
|
+
return {
|
|
42
|
+
format: "agent",
|
|
43
|
+
notice: `trivium: ${agent} is set, so --format agent; pass --format or set TRIVIUM_FORMAT for another.`,
|
|
44
|
+
};
|
|
45
|
+
}
|
package/src/args.ts
ADDED
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
import { TriviumError } from "./errors.ts";
|
|
2
|
+
import { type Format, FORMATS } from "./format.ts";
|
|
3
|
+
import { type Browser, type Throttle, THROTTLES } from "./page.ts";
|
|
4
|
+
import { didYouMean } from "./suggest.ts";
|
|
5
|
+
|
|
6
|
+
const COMMANDS = [
|
|
7
|
+
"load",
|
|
8
|
+
"compare",
|
|
9
|
+
"interactions",
|
|
10
|
+
"bundle",
|
|
11
|
+
"cache",
|
|
12
|
+
] as const;
|
|
13
|
+
|
|
14
|
+
export type Command = (typeof COMMANDS)[number];
|
|
15
|
+
|
|
16
|
+
const BROWSERS: readonly Browser[] = ["chrome", "webkit"];
|
|
17
|
+
const BROWSER_COMMANDS: readonly Command[] = [
|
|
18
|
+
"load",
|
|
19
|
+
"compare",
|
|
20
|
+
"interactions",
|
|
21
|
+
];
|
|
22
|
+
const ALL: readonly Command[] = COMMANDS;
|
|
23
|
+
|
|
24
|
+
interface FlagSpec {
|
|
25
|
+
type: "boolean" | "string";
|
|
26
|
+
commands: readonly Command[];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const FLAGS = new Map<string, FlagSpec>([
|
|
30
|
+
["help", { type: "boolean", commands: ALL }],
|
|
31
|
+
["version", { type: "boolean", commands: ALL }],
|
|
32
|
+
["json", { type: "boolean", commands: ALL }],
|
|
33
|
+
["format", { type: "string", commands: ALL }],
|
|
34
|
+
["config", { type: "string", commands: ALL }],
|
|
35
|
+
["color", { type: "boolean", commands: ALL }],
|
|
36
|
+
["no-color", { type: "boolean", commands: ALL }],
|
|
37
|
+
["runs", { type: "string", commands: BROWSER_COMMANDS }],
|
|
38
|
+
["throttle", { type: "string", commands: BROWSER_COMMANDS }],
|
|
39
|
+
["browser", { type: "string", commands: BROWSER_COMMANDS }],
|
|
40
|
+
["repeat", { type: "boolean", commands: ["load", "compare"] }],
|
|
41
|
+
["immutable", { type: "boolean", commands: ["compare"] }],
|
|
42
|
+
["early", { type: "boolean", commands: ["interactions"] }],
|
|
43
|
+
["base", { type: "string", commands: ["bundle"] }],
|
|
44
|
+
["all", { type: "boolean", commands: ["bundle"] }],
|
|
45
|
+
]);
|
|
46
|
+
|
|
47
|
+
const SHORT = new Map([
|
|
48
|
+
["-h", "help"],
|
|
49
|
+
["-v", "version"],
|
|
50
|
+
]);
|
|
51
|
+
|
|
52
|
+
const FLAG_NAMES = [...FLAGS.keys()].map((name) => `--${name}`);
|
|
53
|
+
const WHOLE_NUMBER = /^[1-9]\d*$/;
|
|
54
|
+
|
|
55
|
+
export interface Args {
|
|
56
|
+
command: Command | undefined;
|
|
57
|
+
help: boolean;
|
|
58
|
+
version: boolean;
|
|
59
|
+
// Undefined when no flag names one, so the agent default can apply.
|
|
60
|
+
format: Format | undefined;
|
|
61
|
+
config: string | undefined;
|
|
62
|
+
// Undefined unless a flag forces color on or off.
|
|
63
|
+
color: boolean | undefined;
|
|
64
|
+
positionals: string[];
|
|
65
|
+
runs: number | undefined;
|
|
66
|
+
throttle: Throttle | undefined;
|
|
67
|
+
browser: Browser;
|
|
68
|
+
repeat: boolean;
|
|
69
|
+
immutable: boolean;
|
|
70
|
+
early: boolean;
|
|
71
|
+
base: string | undefined;
|
|
72
|
+
all: boolean;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const isOneOf = <T extends string>(
|
|
76
|
+
options: readonly T[],
|
|
77
|
+
value: string,
|
|
78
|
+
): value is T => options.some((option) => option === value);
|
|
79
|
+
|
|
80
|
+
// The command named in `argv`, if any, for an error raised before the rest
|
|
81
|
+
// parses, so it can say which command it's about.
|
|
82
|
+
export function commandIn(argv: readonly string[]): Command | undefined {
|
|
83
|
+
for (const arg of argv) {
|
|
84
|
+
if (isOneOf(COMMANDS, arg)) return arg;
|
|
85
|
+
}
|
|
86
|
+
return undefined;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function usage(command: Command | undefined, message: string): TriviumError {
|
|
90
|
+
const where = command === undefined ? "trivium" : `trivium ${command}`;
|
|
91
|
+
return new TriviumError(
|
|
92
|
+
"TV_USAGE",
|
|
93
|
+
`${where}: ${message}; see ${where} --help`,
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function parseBoolean(
|
|
98
|
+
command: Command | undefined,
|
|
99
|
+
flag: string,
|
|
100
|
+
value: string,
|
|
101
|
+
): boolean {
|
|
102
|
+
if (value === "true" || value === "1") return true;
|
|
103
|
+
if (value === "false" || value === "0") return false;
|
|
104
|
+
throw usage(
|
|
105
|
+
command,
|
|
106
|
+
`--${flag} takes true or false, not ${JSON.stringify(value)}`,
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Flags in any position, as `--flag value` or `--flag=value`. `-h` and
|
|
111
|
+
// `--help` win over every other problem, so help is never out of reach.
|
|
112
|
+
export function parseArgs(argv: readonly string[]): Args {
|
|
113
|
+
const command = commandIn(argv);
|
|
114
|
+
const strings = new Map<string, string>();
|
|
115
|
+
const booleans = new Map<string, boolean>();
|
|
116
|
+
const positionals: string[] = [];
|
|
117
|
+
const problems: string[] = [];
|
|
118
|
+
let seenCommand = false;
|
|
119
|
+
for (let i = 0; i < argv.length; i++) {
|
|
120
|
+
const arg = argv[i] ?? "";
|
|
121
|
+
const short = SHORT.get(arg);
|
|
122
|
+
if (short !== undefined) {
|
|
123
|
+
booleans.set(short, true);
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
if (!arg.startsWith("-") || arg === "-") {
|
|
127
|
+
if (!seenCommand && arg === command) seenCommand = true;
|
|
128
|
+
else positionals.push(arg);
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
const eq = arg.indexOf("=");
|
|
132
|
+
const name = (eq === -1 ? arg : arg.slice(0, eq)).replace(/^--?/, "");
|
|
133
|
+
const spec = arg.startsWith("--") ? FLAGS.get(name) : undefined;
|
|
134
|
+
if (spec === undefined) {
|
|
135
|
+
const shown = eq === -1 ? arg : arg.slice(0, eq);
|
|
136
|
+
problems.push(`unknown option ${shown}${didYouMean(shown, FLAG_NAMES)}`);
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
if (spec.type === "boolean") {
|
|
140
|
+
booleans.set(
|
|
141
|
+
name,
|
|
142
|
+
eq === -1 ? true : parseBoolean(command, name, arg.slice(eq + 1)),
|
|
143
|
+
);
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
const value = eq === -1 ? argv[++i] : arg.slice(eq + 1);
|
|
147
|
+
if (
|
|
148
|
+
value === undefined ||
|
|
149
|
+
value === "" ||
|
|
150
|
+
(eq === -1 && value.startsWith("--"))
|
|
151
|
+
) {
|
|
152
|
+
problems.push(`--${name} needs a value`);
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
strings.set(name, value);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const help = booleans.get("help") === true;
|
|
159
|
+
const version = booleans.get("version") === true;
|
|
160
|
+
if (help || version) return { ...EMPTY, command, help, version };
|
|
161
|
+
if (problems.length > 0) throw usage(command, problems.join("; "));
|
|
162
|
+
|
|
163
|
+
const first = positionals[0];
|
|
164
|
+
if (command === undefined) {
|
|
165
|
+
if (first !== undefined) {
|
|
166
|
+
throw usage(
|
|
167
|
+
undefined,
|
|
168
|
+
`unknown command ${JSON.stringify(first)}${didYouMean(first, COMMANDS)}`,
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
throw usage(undefined, `name a command: ${COMMANDS.join(", ")}`);
|
|
172
|
+
}
|
|
173
|
+
for (const name of [...strings.keys(), ...booleans.keys()]) {
|
|
174
|
+
const spec = FLAGS.get(name);
|
|
175
|
+
if (spec && !spec.commands.includes(command)) {
|
|
176
|
+
problems.push(
|
|
177
|
+
`--${name} doesn't apply to ${command}; it's for ${spec.commands.join(", ")}`,
|
|
178
|
+
);
|
|
179
|
+
// Its value is moot, so it isn't checked as well.
|
|
180
|
+
strings.delete(name);
|
|
181
|
+
booleans.delete(name);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
let format: Format | undefined;
|
|
186
|
+
const formatName = strings.get("format");
|
|
187
|
+
if (formatName !== undefined) {
|
|
188
|
+
if (isOneOf(FORMATS, formatName)) format = formatName;
|
|
189
|
+
else
|
|
190
|
+
problems.push(
|
|
191
|
+
`--format takes ${FORMATS.join(", ")}, not ${JSON.stringify(formatName)}${didYouMean(formatName, FORMATS)}`,
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
const json = booleans.get("json");
|
|
195
|
+
if (json === true) {
|
|
196
|
+
if (format !== undefined && format !== "json") {
|
|
197
|
+
problems.push(`--json and --format ${format} disagree: pass one`);
|
|
198
|
+
}
|
|
199
|
+
format = "json";
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
let runs: number | undefined;
|
|
203
|
+
const runsText = strings.get("runs");
|
|
204
|
+
if (runsText !== undefined) {
|
|
205
|
+
if (WHOLE_NUMBER.test(runsText)) runs = Number(runsText);
|
|
206
|
+
else
|
|
207
|
+
problems.push(
|
|
208
|
+
`--runs takes a whole number of 1 or more, not ${JSON.stringify(runsText)}`,
|
|
209
|
+
);
|
|
210
|
+
}
|
|
211
|
+
let throttle: Throttle | undefined;
|
|
212
|
+
const throttleName = strings.get("throttle");
|
|
213
|
+
if (throttleName !== undefined) {
|
|
214
|
+
if (isOneOf(THROTTLES, throttleName)) throttle = throttleName;
|
|
215
|
+
else
|
|
216
|
+
problems.push(
|
|
217
|
+
`--throttle takes ${THROTTLES.join(", ")}, not ${JSON.stringify(throttleName)}${didYouMean(throttleName, THROTTLES)}`,
|
|
218
|
+
);
|
|
219
|
+
}
|
|
220
|
+
let browser: Browser = "chrome";
|
|
221
|
+
const browserName = strings.get("browser");
|
|
222
|
+
if (browserName !== undefined) {
|
|
223
|
+
if (isOneOf(BROWSERS, browserName)) browser = browserName;
|
|
224
|
+
else
|
|
225
|
+
problems.push(
|
|
226
|
+
`--browser takes ${BROWSERS.join(" or ")}, not ${JSON.stringify(browserName)}${didYouMean(browserName, BROWSERS)}`,
|
|
227
|
+
);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
const most = command === "compare" ? 2 : 1;
|
|
231
|
+
if (positionals.length > most) {
|
|
232
|
+
problems.push(
|
|
233
|
+
`takes at most ${most === 1 ? "one argument" : "two arguments"}, got ${positionals.map((p) => JSON.stringify(p)).join(" ")}`,
|
|
234
|
+
);
|
|
235
|
+
}
|
|
236
|
+
if (command === "compare" && positionals.length < 2) {
|
|
237
|
+
problems.push("needs two builds or URLs to compare, A then B");
|
|
238
|
+
}
|
|
239
|
+
const colorOn = booleans.get("color") === true;
|
|
240
|
+
const colorOff = booleans.get("no-color") === true;
|
|
241
|
+
if (colorOn && colorOff)
|
|
242
|
+
problems.push("--color and --no-color disagree: pass one");
|
|
243
|
+
if (problems.length > 0) throw usage(command, problems.join("; "));
|
|
244
|
+
|
|
245
|
+
return {
|
|
246
|
+
color: colorOn ? true : colorOff ? false : undefined,
|
|
247
|
+
command,
|
|
248
|
+
help,
|
|
249
|
+
version,
|
|
250
|
+
format,
|
|
251
|
+
config: strings.get("config"),
|
|
252
|
+
positionals,
|
|
253
|
+
runs,
|
|
254
|
+
throttle,
|
|
255
|
+
browser,
|
|
256
|
+
repeat: booleans.get("repeat") === true,
|
|
257
|
+
immutable: booleans.get("immutable") === true,
|
|
258
|
+
early: booleans.get("early") === true,
|
|
259
|
+
base: strings.get("base"),
|
|
260
|
+
all: booleans.get("all") === true,
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
const EMPTY: Args = {
|
|
265
|
+
command: undefined,
|
|
266
|
+
help: false,
|
|
267
|
+
version: false,
|
|
268
|
+
format: undefined,
|
|
269
|
+
config: undefined,
|
|
270
|
+
color: undefined,
|
|
271
|
+
positionals: [],
|
|
272
|
+
runs: undefined,
|
|
273
|
+
throttle: undefined,
|
|
274
|
+
browser: "chrome",
|
|
275
|
+
repeat: false,
|
|
276
|
+
immutable: false,
|
|
277
|
+
early: false,
|
|
278
|
+
base: undefined,
|
|
279
|
+
all: false,
|
|
280
|
+
};
|
|
281
|
+
|
|
282
|
+
// The format named in `argv` even when the rest doesn't parse, so a usage
|
|
283
|
+
// error under `--json` is JSON too.
|
|
284
|
+
export function formatIn(argv: readonly string[]): Format | undefined {
|
|
285
|
+
for (const [i, arg] of argv.entries()) {
|
|
286
|
+
if (arg === "--json" || arg === "--json=true" || arg === "--json=1")
|
|
287
|
+
return "json";
|
|
288
|
+
const value =
|
|
289
|
+
arg === "--format"
|
|
290
|
+
? argv[i + 1]
|
|
291
|
+
: arg.startsWith("--format=")
|
|
292
|
+
? arg.slice(9)
|
|
293
|
+
: undefined;
|
|
294
|
+
if (value !== undefined && isOneOf(FORMATS, value)) return value;
|
|
295
|
+
}
|
|
296
|
+
return undefined;
|
|
297
|
+
}
|
package/src/bundle.ts
ADDED
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { join, resolve } from "node:path";
|
|
3
|
+
import { brotliCompressSync, constants } from "node:zlib";
|
|
4
|
+
|
|
5
|
+
import { nowMs } from "./env.ts";
|
|
6
|
+
import { TriviumError } from "./errors.ts";
|
|
7
|
+
import {
|
|
8
|
+
byteSize,
|
|
9
|
+
closingLine,
|
|
10
|
+
elapsed,
|
|
11
|
+
fitted,
|
|
12
|
+
headerRow,
|
|
13
|
+
type Outcome,
|
|
14
|
+
type Paint,
|
|
15
|
+
paintChange,
|
|
16
|
+
plural,
|
|
17
|
+
RULE,
|
|
18
|
+
SCHEMA_VERSION,
|
|
19
|
+
SEPARATOR,
|
|
20
|
+
signed,
|
|
21
|
+
truncatePath,
|
|
22
|
+
} from "./format.ts";
|
|
23
|
+
import type { ResolvedConfig } from "./load-config.ts";
|
|
24
|
+
|
|
25
|
+
interface BundleInput {
|
|
26
|
+
dir: string;
|
|
27
|
+
// The build to compare against; hashes are ignored when matching assets.
|
|
28
|
+
base: string | undefined;
|
|
29
|
+
cwd: string;
|
|
30
|
+
config: ResolvedConfig;
|
|
31
|
+
// Every file, not only the largest FILE_LIMIT.
|
|
32
|
+
all: boolean;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const FILE_LIMIT = 20;
|
|
36
|
+
|
|
37
|
+
// Only `<script src>` and `<link rel=modulepreload|stylesheet|icon>` load
|
|
38
|
+
// with the page; a prefetch or a dynamic import waits, so it's lazy.
|
|
39
|
+
const LINKED =
|
|
40
|
+
/<(?:script[^>]*\ssrc|link[^>]*\srel="?(?:modulepreload|stylesheet|icon)"?[^>]*\shref)="?([^"\s>]+)/g;
|
|
41
|
+
const INLINE = /<(style|script)\b[^>]*>([\s\S]*?)<\/\1>/g;
|
|
42
|
+
const LEADING_SLASH = /^\.?\//;
|
|
43
|
+
const LOAD_ORDER: readonly (string | null)[] = ["initial", "lazy", null];
|
|
44
|
+
// An inline block under this is a one-liner, so it gets no row.
|
|
45
|
+
const INLINE_MIN_BYTES = 100;
|
|
46
|
+
|
|
47
|
+
interface Sizes {
|
|
48
|
+
rawBytes: number;
|
|
49
|
+
gzipBytes: number;
|
|
50
|
+
brotliBytes: number;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
interface Asset extends Sizes {
|
|
54
|
+
path: string;
|
|
55
|
+
initial: boolean;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const sizesOf = (bytes: Uint8Array<ArrayBuffer>): Sizes => ({
|
|
59
|
+
rawBytes: bytes.length,
|
|
60
|
+
gzipBytes: Bun.gzipSync(bytes, { level: 9 }).length,
|
|
61
|
+
brotliBytes: brotliCompressSync(bytes, {
|
|
62
|
+
params: { [constants.BROTLI_PARAM_QUALITY]: 11 },
|
|
63
|
+
}).length,
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
function checkBuild(dir: string, shown: string): void {
|
|
67
|
+
if (!existsSync(join(dir, "index.html"))) {
|
|
68
|
+
throw new TriviumError(
|
|
69
|
+
"TV_INPUT",
|
|
70
|
+
`trivium bundle: no index.html in ${JSON.stringify(shown)}: pass the build's output directory`,
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async function measure(dir: string, hash: RegExp) {
|
|
76
|
+
const html = await Bun.file(join(dir, "index.html")).text();
|
|
77
|
+
const linked = new Set(
|
|
78
|
+
[...html.matchAll(LINKED)].map((m) =>
|
|
79
|
+
(m[1] ?? "").replace(LEADING_SLASH, ""),
|
|
80
|
+
),
|
|
81
|
+
);
|
|
82
|
+
const byKey = new Map<string, Asset>();
|
|
83
|
+
const paths = await Array.fromAsync(new Bun.Glob("**/*").scan(dir));
|
|
84
|
+
for (const path of paths.sort()) {
|
|
85
|
+
const bytes = await Bun.file(join(dir, path)).bytes();
|
|
86
|
+
byKey.set(path.replace(hash, ""), {
|
|
87
|
+
path,
|
|
88
|
+
...sizesOf(bytes),
|
|
89
|
+
initial: path === "index.html" || linked.has(path),
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
// Inline styles and scripts cost bytes no asset row shows, so they get
|
|
93
|
+
// their own; the prerendered markup doesn't.
|
|
94
|
+
const inline = [...html.matchAll(INLINE)]
|
|
95
|
+
.map(([, tag, body]) => ({
|
|
96
|
+
tag: tag ?? "",
|
|
97
|
+
...sizesOf(new TextEncoder().encode(body ?? "")),
|
|
98
|
+
}))
|
|
99
|
+
.filter((block) => block.rawBytes >= INLINE_MIN_BYTES);
|
|
100
|
+
return { byKey, inline };
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const totalOf = (assets: Iterable<Asset>, initial: boolean): Sizes => {
|
|
104
|
+
const sum = { rawBytes: 0, gzipBytes: 0, brotliBytes: 0 };
|
|
105
|
+
for (const a of assets) {
|
|
106
|
+
if (a.initial !== initial) continue;
|
|
107
|
+
sum.rawBytes += a.rawBytes;
|
|
108
|
+
sum.gzipBytes += a.gzipBytes;
|
|
109
|
+
sum.brotliBytes += a.brotliBytes;
|
|
110
|
+
}
|
|
111
|
+
return sum;
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
export async function runBundle(input: BundleInput): Promise<Outcome> {
|
|
115
|
+
const startMs = nowMs();
|
|
116
|
+
const dir = resolve(input.cwd, input.dir);
|
|
117
|
+
checkBuild(dir, input.dir);
|
|
118
|
+
const baseDir =
|
|
119
|
+
input.base === undefined ? undefined : resolve(input.cwd, input.base);
|
|
120
|
+
if (baseDir !== undefined) checkBuild(baseDir, input.base ?? "");
|
|
121
|
+
const build = await measure(dir, input.config.assets.hash);
|
|
122
|
+
const before = baseDir
|
|
123
|
+
? await measure(baseDir, input.config.assets.hash)
|
|
124
|
+
: undefined;
|
|
125
|
+
const keys = [
|
|
126
|
+
...new Set([...build.byKey.keys(), ...(before?.byKey.keys() ?? [])]),
|
|
127
|
+
];
|
|
128
|
+
const assets = keys
|
|
129
|
+
.map((key) => {
|
|
130
|
+
const now = build.byKey.get(key);
|
|
131
|
+
const was = before?.byKey.get(key);
|
|
132
|
+
const change =
|
|
133
|
+
before === undefined
|
|
134
|
+
? null
|
|
135
|
+
: !now
|
|
136
|
+
? "removed"
|
|
137
|
+
: !was
|
|
138
|
+
? "new"
|
|
139
|
+
: now.gzipBytes === was.gzipBytes
|
|
140
|
+
? "same"
|
|
141
|
+
: "changed";
|
|
142
|
+
return {
|
|
143
|
+
path: now?.path ?? was?.path ?? key,
|
|
144
|
+
load: now ? (now.initial ? "initial" : "lazy") : null,
|
|
145
|
+
rawBytes: now?.rawBytes ?? 0,
|
|
146
|
+
gzipBytes: now?.gzipBytes ?? 0,
|
|
147
|
+
brotliBytes: now?.brotliBytes ?? 0,
|
|
148
|
+
baseGzipBytes: before === undefined ? null : (was?.gzipBytes ?? 0),
|
|
149
|
+
change,
|
|
150
|
+
};
|
|
151
|
+
})
|
|
152
|
+
// Initial files first, then lazy, then removed, each largest first,
|
|
153
|
+
// since the first load is what a reader checks.
|
|
154
|
+
.sort(
|
|
155
|
+
(x, y) =>
|
|
156
|
+
LOAD_ORDER.indexOf(x.load) - LOAD_ORDER.indexOf(y.load) ||
|
|
157
|
+
y.gzipBytes - x.gzipBytes ||
|
|
158
|
+
x.path.localeCompare(y.path),
|
|
159
|
+
);
|
|
160
|
+
const totals = {
|
|
161
|
+
initial: totalOf(build.byKey.values(), true),
|
|
162
|
+
lazy: totalOf(build.byKey.values(), false),
|
|
163
|
+
};
|
|
164
|
+
const baseTotals = before && {
|
|
165
|
+
initial: totalOf(before.byKey.values(), true),
|
|
166
|
+
lazy: totalOf(before.byKey.values(), false),
|
|
167
|
+
};
|
|
168
|
+
const initialCount = assets.filter((a) => a.load === "initial").length;
|
|
169
|
+
const headerOf = (paint: Paint) =>
|
|
170
|
+
[
|
|
171
|
+
`${byteSize(totals.initial.gzipBytes)} gzip on first load, ${initialCount} of ${plural(build.byKey.size, "file")}${SEPARATOR}${byteSize(totals.lazy.gzipBytes)} lazy`,
|
|
172
|
+
paint.dim(
|
|
173
|
+
input.base === undefined
|
|
174
|
+
? input.dir
|
|
175
|
+
: `${input.dir} against ${input.base}`,
|
|
176
|
+
),
|
|
177
|
+
].join("\n");
|
|
178
|
+
const delta = (now: number, was: number | undefined, paint: Paint) =>
|
|
179
|
+
was === undefined
|
|
180
|
+
? []
|
|
181
|
+
: [
|
|
182
|
+
now === was
|
|
183
|
+
? ""
|
|
184
|
+
: paintChange(paint, now - was)(signed(now - was, byteSize)),
|
|
185
|
+
];
|
|
186
|
+
const elapsedMs = nowMs() - startMs;
|
|
187
|
+
return {
|
|
188
|
+
exitCode: 0,
|
|
189
|
+
json: {
|
|
190
|
+
schemaVersion: SCHEMA_VERSION,
|
|
191
|
+
ok: true,
|
|
192
|
+
command: "bundle",
|
|
193
|
+
dir: input.dir,
|
|
194
|
+
base: input.base ?? null,
|
|
195
|
+
config: input.config.file ?? null,
|
|
196
|
+
summary: {
|
|
197
|
+
files: build.byKey.size,
|
|
198
|
+
initialFiles: initialCount,
|
|
199
|
+
initialGzipBytes: totals.initial.gzipBytes,
|
|
200
|
+
lazyGzipBytes: totals.lazy.gzipBytes,
|
|
201
|
+
},
|
|
202
|
+
assets,
|
|
203
|
+
totals,
|
|
204
|
+
baseTotals: baseTotals ?? null,
|
|
205
|
+
inline: build.inline,
|
|
206
|
+
elapsedMs,
|
|
207
|
+
},
|
|
208
|
+
text(format, paint) {
|
|
209
|
+
const header = headerOf(paint);
|
|
210
|
+
if (format === "agent") {
|
|
211
|
+
const lines = [header];
|
|
212
|
+
for (const a of assets) {
|
|
213
|
+
const change =
|
|
214
|
+
a.change === null || a.change === "same"
|
|
215
|
+
? ""
|
|
216
|
+
: a.change === "changed"
|
|
217
|
+
? `, gzip ${signed(a.gzipBytes - (a.baseGzipBytes ?? 0), byteSize)}`
|
|
218
|
+
: ` (${a.change})`;
|
|
219
|
+
lines.push(
|
|
220
|
+
a.load === null
|
|
221
|
+
? `${a.path} (removed)`
|
|
222
|
+
: `${a.path} ${a.load} raw ${byteSize(a.rawBytes)} gzip ${byteSize(a.gzipBytes)} brotli ${byteSize(a.brotliBytes)}${change}`,
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
for (const block of build.inline) {
|
|
226
|
+
lines.push(
|
|
227
|
+
`inline <${block.tag}> in index.html raw ${byteSize(block.rawBytes)} gzip ${byteSize(block.gzipBytes)}`,
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
return lines.join("\n");
|
|
231
|
+
}
|
|
232
|
+
const head = ["", "raw", "gzip", "brotli"];
|
|
233
|
+
if (before) head.push("change");
|
|
234
|
+
head.push("file");
|
|
235
|
+
const rows = [headerRow(paint, head)];
|
|
236
|
+
const change = (now: number, was: number) =>
|
|
237
|
+
paintChange(paint, now - was)(signed(now - was, byteSize));
|
|
238
|
+
// The largest files, unless --all: cli.md asks that a cut say how
|
|
239
|
+
// to see the rest, so the last row does.
|
|
240
|
+
const shown = input.all ? assets : assets.slice(0, FILE_LIMIT);
|
|
241
|
+
for (const a of shown) {
|
|
242
|
+
const row =
|
|
243
|
+
a.load === null
|
|
244
|
+
? ["", "", "", ""]
|
|
245
|
+
: [
|
|
246
|
+
a.load === "lazy" ? paint.dim(a.load) : a.load,
|
|
247
|
+
byteSize(a.rawBytes),
|
|
248
|
+
byteSize(a.gzipBytes),
|
|
249
|
+
byteSize(a.brotliBytes),
|
|
250
|
+
];
|
|
251
|
+
if (before) {
|
|
252
|
+
row.push(
|
|
253
|
+
a.change === "removed"
|
|
254
|
+
? paint.good("removed")
|
|
255
|
+
: a.change === "same"
|
|
256
|
+
? ""
|
|
257
|
+
: change(a.gzipBytes, a.baseGzipBytes ?? 0),
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
row.push(`${a.path}${a.change === "new" ? " (new)" : ""}`);
|
|
261
|
+
rows.push(row);
|
|
262
|
+
}
|
|
263
|
+
const hidden = assets.slice(shown.length);
|
|
264
|
+
if (hidden.length > 0) {
|
|
265
|
+
const gzip = hidden.reduce((sum, a) => sum + a.gzipBytes, 0);
|
|
266
|
+
rows.push([
|
|
267
|
+
paint.dim("…"),
|
|
268
|
+
"",
|
|
269
|
+
"",
|
|
270
|
+
"",
|
|
271
|
+
...(before ? [""] : []),
|
|
272
|
+
paint.dim(
|
|
273
|
+
`${hidden.length} more, ${byteSize(gzip)} gzip; --all lists them`,
|
|
274
|
+
),
|
|
275
|
+
]);
|
|
276
|
+
}
|
|
277
|
+
rows.push(["", RULE, RULE, RULE, ...(before ? [RULE] : []), ""]);
|
|
278
|
+
for (const which of ["initial", "lazy"] as const) {
|
|
279
|
+
const t = totals[which];
|
|
280
|
+
const count = assets.filter((a) => a.load === which).length;
|
|
281
|
+
rows.push([
|
|
282
|
+
which === "lazy" ? paint.dim(which) : which,
|
|
283
|
+
byteSize(t.rawBytes),
|
|
284
|
+
byteSize(t.gzipBytes),
|
|
285
|
+
byteSize(t.brotliBytes),
|
|
286
|
+
...delta(t.gzipBytes, baseTotals?.[which].gzipBytes, paint),
|
|
287
|
+
plural(count, "file"),
|
|
288
|
+
]);
|
|
289
|
+
}
|
|
290
|
+
for (const block of build.inline) {
|
|
291
|
+
rows.push([
|
|
292
|
+
"",
|
|
293
|
+
paint.dim(byteSize(block.rawBytes)),
|
|
294
|
+
paint.dim(byteSize(block.gzipBytes)),
|
|
295
|
+
paint.dim(byteSize(block.brotliBytes)),
|
|
296
|
+
...(before ? [""] : []),
|
|
297
|
+
paint.dim(`inline <${block.tag}> in index.html`),
|
|
298
|
+
]);
|
|
299
|
+
}
|
|
300
|
+
return [
|
|
301
|
+
header,
|
|
302
|
+
"",
|
|
303
|
+
// A path that doesn't fit is cut in the middle, keeping its
|
|
304
|
+
// directory and its hashed name.
|
|
305
|
+
...fitted(
|
|
306
|
+
rows,
|
|
307
|
+
before ? [1, 2, 3, 4] : [1, 2, 3],
|
|
308
|
+
truncatePath,
|
|
309
|
+
paint.dim,
|
|
310
|
+
),
|
|
311
|
+
"",
|
|
312
|
+
closingLine(
|
|
313
|
+
paint,
|
|
314
|
+
`sized ${plural(build.byKey.size, "file")} in ${elapsed(elapsedMs)}`,
|
|
315
|
+
),
|
|
316
|
+
].join("\n");
|
|
317
|
+
},
|
|
318
|
+
};
|
|
319
|
+
}
|