@a.incantalupo/web-tools 0.1.0 → 0.3.0
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 +45 -12
- package/dist/audit/lighthouse-cli.d.ts +1 -0
- package/dist/audit/lighthouse-cli.js +112 -0
- package/dist/audit/lighthouse.d.ts +6 -1
- package/dist/audit/lighthouse.js +142 -57
- package/dist/audit/types.d.ts +2 -0
- package/dist/browser/index.js +3 -7
- package/dist/cli.js +4 -3
- package/dist/refs/cli.js +1 -1
- package/dist/refs/glyphs.js +25 -10
- package/dist/refs/images.js +34 -32
- package/dist/refs/text.js +282 -35
- package/dist/report.d.ts +70 -0
- package/dist/report.js +148 -0
- package/package.json +2 -7
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @a.incantalupo/web-tools
|
|
2
2
|
|
|
3
|
-
Checks and captures for any website, as one command: `stack-tools`.
|
|
3
|
+
Checks and captures for any website, as one command: `stack-tools`.
|
|
4
4
|
|
|
5
5
|
```sh
|
|
6
6
|
stack-tools # the list of commands
|
|
@@ -8,20 +8,57 @@ stack-tools browser [url] # your own Chromium window: sign in by hand, to
|
|
|
8
8
|
stack-tools refs <url...> [--full] [--mine] [--out dir | --out-root dir] # desktop + mobile screenshots -> ./design-refs/<date>
|
|
9
9
|
stack-tools snap <dir> <url...> # computed styles + screenshots of pages, in every theme
|
|
10
10
|
stack-tools diff <before> <after>
|
|
11
|
-
stack-tools text <url...> [--verbose] [--login <email>] # reading ease (Gulpease), text size, contrast, fields, tap targets
|
|
12
|
-
stack-tools glyphs <url...>
|
|
13
|
-
stack-tools images <url...>
|
|
11
|
+
stack-tools text <url...> [--verbose] [--login <email>] [--brief | --json] # reading ease (Gulpease), text size, contrast, fields, tap targets, sideways scroll at 320 px, the phone with text at 130-200%
|
|
12
|
+
stack-tools glyphs <url...> [--brief | --json] # characters the page fonts don't cover
|
|
13
|
+
stack-tools images <url...> [--budget 600] [--brief | --json] # which image file each slot downloads (3x phone, 2x laptop, 1x monitor)
|
|
14
|
+
stack-tools lighthouse <url...> [--budget] [--min-perf <n>] [--runs <n>] [--out dir] [--json] [--header 'Name: value'] # Lighthouse on phone and desktop; use it instead of the Lighthouse CLI (on WSL that one leaves Chrome's profile in your folder)
|
|
14
15
|
stack-tools audit <url> [--profile file.ts] [--render] [--teaser] [--reviews a.json...] [--preview] [--header 'Name: value'] [--keys .env] [--out dir | --out-root dir]
|
|
15
16
|
stack-tools audit-rerender <dir> [--profile file.ts] [--teaser] # rebuild a report from its findings.json, no new crawl
|
|
16
17
|
```
|
|
17
18
|
|
|
18
19
|
Outputs go where you run the command (`./design-refs`, `./site-eval/<host>/<date>`) unless `--out` or `--out-root` says otherwise. A project keeps its own outputs; nothing is written inside this package.
|
|
19
20
|
|
|
21
|
+
## Short output, for agents and scripts
|
|
22
|
+
|
|
23
|
+
`text`, `images` and `glyphs` print in one of three forms. What is measured and the exit code (1 when something failed) are the same in all three.
|
|
24
|
+
|
|
25
|
+
| Form | Chosen by | Prints |
|
|
26
|
+
| --- | --- | --- |
|
|
27
|
+
| full | the default, or `--full` | every finding with its detail |
|
|
28
|
+
| brief | `--brief`, or `STACK_TOOLS_OUTPUT=brief` in the environment | one line per page (`ok`, `warn` or `FAIL`, the page, its key numbers), every failure in full under it, the warnings as a count with their kinds, the totals last |
|
|
29
|
+
| JSON | `--json`, or `STACK_TOOLS_OUTPUT=json` | the same counts and findings, for a script |
|
|
30
|
+
|
|
31
|
+
A flag wins over the variable, and `--full` wins over everything. Set the variable once where an AI coding agent runs its commands (its settings' environment): the agent then reads a few lines per check, and you still get the full form in your own terminal.
|
|
32
|
+
|
|
33
|
+
```text
|
|
34
|
+
$ stack-tools text https://example.com/ https://example.com/contact --brief
|
|
35
|
+
ok / reading ease 68
|
|
36
|
+
FAIL /contact reading ease 61 · 1 fail · 2 warn (small-text, target-44)
|
|
37
|
+
phone 390: 1 field(s) under 16 px: "email" 14 px
|
|
38
|
+
2 pages · 1 fail · 2 warn · --full lists the warnings
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Speed against a budget
|
|
42
|
+
|
|
43
|
+
`stack-tools lighthouse <url...> --budget` judges every run (phone and desktop) against the limits a public page should hold, and exits 1 when one is passed:
|
|
44
|
+
|
|
45
|
+
| Number | Limit |
|
|
46
|
+
| --- | --- |
|
|
47
|
+
| Largest contentful paint | 2.5 s |
|
|
48
|
+
| Layout shift (CLS) | 0.1 |
|
|
49
|
+
| The server's first byte | 800 ms |
|
|
50
|
+
| Scripts, as sent | 100 kB |
|
|
51
|
+
|
|
52
|
+
- A `FAIL` line names the number and its limit; for the largest paint, also the element that was timed.
|
|
53
|
+
- `--min-perf <n>` adds a floor on the performance score (with or without `--budget`).
|
|
54
|
+
- `--runs <n>` runs each page n times and keeps the middle run: one lab run proves little, and the phone run simulates a slow network.
|
|
55
|
+
- Without these flags the command prints what it always did.
|
|
56
|
+
|
|
20
57
|
## Setup
|
|
21
58
|
|
|
22
|
-
- In a project: `pnpm add -D @a.incantalupo/web-tools`, then scripts like `"check:text": "stack-tools text"
|
|
23
|
-
- Chromium: the tools use Playwright's (`CHROME_PATH`, default `~/.cache/ms-playwright/chromium-1194/chrome-linux/chrome`).
|
|
24
|
-
- `stack-tools browser` keeps its profile in `~/.cache/stack-tools/chrome-profile` (
|
|
59
|
+
- In a project: `pnpm add -D @a.incantalupo/web-tools`, then scripts like `"check:text": "stack-tools text"` and `"check:speed": "stack-tools lighthouse --budget"`.
|
|
60
|
+
- Chromium: the tools use Playwright's (`CHROME_PATH`, default `~/.cache/ms-playwright/chromium-1194/chrome-linux/chrome`). Install it with `pnpm dlx playwright@1.56.1 install chromium`.
|
|
61
|
+
- `stack-tools browser` keeps its profile in `~/.cache/stack-tools/chrome-profile` (`MY_CHROME_PROFILE` to change it) and listens on `127.0.0.1:9222` only (`MY_CHROME_PORT`).
|
|
25
62
|
|
|
26
63
|
## The audit and its profiles
|
|
27
64
|
|
|
@@ -30,7 +67,7 @@ Outputs go where you run the command (`./design-refs`, `./site-eval/<host>/<date
|
|
|
30
67
|
A **profile** adapts it to a niche and writes that niche's reports. It lives in the project that sells to the niche, so its wording and sales notes stay private:
|
|
31
68
|
|
|
32
69
|
```ts
|
|
33
|
-
//
|
|
70
|
+
// niches/dealer/profile.ts in your own (private) repo
|
|
34
71
|
import type { AuditProfile } from '@a.incantalupo/web-tools/audit';
|
|
35
72
|
export default {
|
|
36
73
|
name: 'dealer',
|
|
@@ -40,7 +77,3 @@ export default {
|
|
|
40
77
|
```
|
|
41
78
|
|
|
42
79
|
`@a.incantalupo/web-tools/audit` exports the result types, `computeScores`, `buildFindings`, `renderSummary`. The package root exports the own-browser connection (`connectMyChrome`, `openMyChrome`, `isOpen`) and the polite fetch helpers (`timedFetch`, `tryFetch`, `mapLimit`) for a project's own scrapers.
|
|
43
|
-
|
|
44
|
-
## Releasing
|
|
45
|
-
|
|
46
|
-
Bump `version`, add a line to the KB's `CHANGELOG.md`, commit, then `npm publish --access public` here (it builds first; npm asks for the passkey). Renovate brings it to every project.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
// stack-tools lighthouse <url...>: Lighthouse on a phone and on a desktop, for each URL.
|
|
2
|
+
// Use this instead of the Lighthouse CLI: on WSL the CLI leaves Chrome's profile in the
|
|
3
|
+
// folder you run it from, as a folder literally named "C:\Users\...\lighthouse.*".
|
|
4
|
+
// Here the profile sits in the system temp folder and is removed at the end.
|
|
5
|
+
//
|
|
6
|
+
// --budget judges every run against the limits a public page should hold: LCP 2.5 s,
|
|
7
|
+
// CLS 0.1, the server's first byte 800 ms, scripts 100 KB as sent. --min-perf <n> adds a
|
|
8
|
+
// floor on the performance score. Past a limit: a FAIL line with the number, the limit
|
|
9
|
+
// and, for LCP, the element that was timed; exit code 1. --runs <n> keeps the middle
|
|
10
|
+
// run of n, since one lab run proves little.
|
|
11
|
+
import { mkdir, mkdtemp } from "node:fs/promises";
|
|
12
|
+
import { tmpdir } from "node:os";
|
|
13
|
+
import { join, resolve } from "node:path";
|
|
14
|
+
import { parseArgs } from "node:util";
|
|
15
|
+
import { extraHeaders } from "./fetch.js";
|
|
16
|
+
import { runLighthouse } from "./lighthouse.js";
|
|
17
|
+
const { values, positionals } = parseArgs({
|
|
18
|
+
allowPositionals: true,
|
|
19
|
+
options: {
|
|
20
|
+
// Where the HTML reports go (default: a new folder in the system temp folder).
|
|
21
|
+
out: { type: "string" },
|
|
22
|
+
json: { type: "boolean", default: false },
|
|
23
|
+
// "Name: value", sent with every request: the site owner's firewall allowlist.
|
|
24
|
+
header: { type: "string", multiple: true, default: [] },
|
|
25
|
+
budget: { type: "boolean", default: false },
|
|
26
|
+
"min-perf": { type: "string" },
|
|
27
|
+
runs: { type: "string", default: "1" },
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
const minPerf = values["min-perf"] == null ? null : Number(values["min-perf"]);
|
|
31
|
+
const times = Number(values.runs);
|
|
32
|
+
if (!positionals.length ||
|
|
33
|
+
(minPerf != null && !(minPerf >= 0 && minPerf <= 100)) ||
|
|
34
|
+
!(Number.isInteger(times) && times >= 1 && times <= 9)) {
|
|
35
|
+
console.error("Usage: stack-tools lighthouse <url...> [--budget] [--min-perf 0-100] [--runs 1-9] [--out dir] [--json] [--header 'Name: value']");
|
|
36
|
+
process.exit(positionals.length ? 2 : 1);
|
|
37
|
+
}
|
|
38
|
+
for (const h of values.header ?? []) {
|
|
39
|
+
const i = h.indexOf(":");
|
|
40
|
+
if (i > 0)
|
|
41
|
+
extraHeaders[h.slice(0, i).trim()] = h.slice(i + 1).trim();
|
|
42
|
+
}
|
|
43
|
+
const outDir = values.out
|
|
44
|
+
? resolve(process.env.INIT_CWD ?? process.cwd(), values.out)
|
|
45
|
+
: await mkdtemp(join(tmpdir(), "stack-tools-lighthouse-"));
|
|
46
|
+
await mkdir(outDir, { recursive: true });
|
|
47
|
+
const runs = await runLighthouse(positionals, outDir, times);
|
|
48
|
+
// The limits of a public page. Lab numbers on a simulated mid-range phone and on a
|
|
49
|
+
// desktop: limits, not goals.
|
|
50
|
+
const LIMITS = { lcpMs: 2500, cls: 0.1, ttfbMs: 800, scriptBytes: 100 * 1024 };
|
|
51
|
+
const judging = values.budget || minPerf != null;
|
|
52
|
+
const over = [];
|
|
53
|
+
for (const r of runs) {
|
|
54
|
+
if (!judging || r.error)
|
|
55
|
+
continue;
|
|
56
|
+
const add = (what, note) => over.push({ url: r.url, formFactor: r.formFactor, what, note });
|
|
57
|
+
const m = r.metrics;
|
|
58
|
+
const perf = r.scores.performance;
|
|
59
|
+
if (minPerf != null && perf != null && perf < minPerf)
|
|
60
|
+
add("performance", `performance ${perf} under ${minPerf}`);
|
|
61
|
+
if (!values.budget)
|
|
62
|
+
continue;
|
|
63
|
+
if (m.lcpMs != null && m.lcpMs > LIMITS.lcpMs)
|
|
64
|
+
add("lcp", `LCP ${(m.lcpMs / 1000).toFixed(1)} s over ${LIMITS.lcpMs / 1000} s${r.lcpElement ? `: ${r.lcpElement}` : ""}`);
|
|
65
|
+
if (m.cls != null && m.cls > LIMITS.cls)
|
|
66
|
+
add("cls", `CLS ${m.cls.toFixed(3)} over ${LIMITS.cls}`);
|
|
67
|
+
if (m.ttfbMs != null && m.ttfbMs > LIMITS.ttfbMs)
|
|
68
|
+
add("ttfb", `first byte ${Math.round(m.ttfbMs)} ms over ${LIMITS.ttfbMs} ms`);
|
|
69
|
+
if (r.scriptBytes != null && r.scriptBytes > LIMITS.scriptBytes)
|
|
70
|
+
add("js", `scripts ${Math.round(r.scriptBytes / 1024)} kB over ${LIMITS.scriptBytes / 1024} kB`);
|
|
71
|
+
}
|
|
72
|
+
if (values.json) {
|
|
73
|
+
console.log(JSON.stringify(judging
|
|
74
|
+
? {
|
|
75
|
+
outDir,
|
|
76
|
+
runs,
|
|
77
|
+
budget: { limits: values.budget ? LIMITS : {}, minPerf, over },
|
|
78
|
+
}
|
|
79
|
+
: { outDir, runs }, null, 2));
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
const score = (n) => n == null ? " -" : String(n).padStart(3);
|
|
83
|
+
const seconds = (ms) => ms == null ? "-" : `${(ms / 1000).toFixed(1)} s`;
|
|
84
|
+
for (const r of runs) {
|
|
85
|
+
const m = r.metrics;
|
|
86
|
+
console.log([
|
|
87
|
+
r.formFactor.padEnd(7),
|
|
88
|
+
`perf ${score(r.scores.performance)}`,
|
|
89
|
+
`a11y ${score(r.scores.accessibility)}`,
|
|
90
|
+
`best ${score(r.scores["best-practices"])}`,
|
|
91
|
+
`seo ${score(r.scores.seo)}`,
|
|
92
|
+
`LCP ${seconds(m.lcpMs)}`,
|
|
93
|
+
`CLS ${m.cls == null ? "-" : m.cls.toFixed(3)}`,
|
|
94
|
+
`TBT ${m.tbtMs == null ? "-" : `${Math.round(m.tbtMs)} ms`}`,
|
|
95
|
+
r.totalBytes == null ? "" : `${Math.round(r.totalBytes / 1024)} kB`,
|
|
96
|
+
r.url,
|
|
97
|
+
].join(" "));
|
|
98
|
+
if (r.error)
|
|
99
|
+
console.log(` ${r.error}`);
|
|
100
|
+
for (const o of over)
|
|
101
|
+
if (o.url === r.url && o.formFactor === r.formFactor)
|
|
102
|
+
console.log(` FAIL ${o.note}`);
|
|
103
|
+
}
|
|
104
|
+
if (judging)
|
|
105
|
+
console.log(`\n${runs.length} run(s)${times > 1 ? `, the middle of ${times} each` : ""}: ${over.length ? `${over.length} over the limits` : "all within the limits"}.`);
|
|
106
|
+
console.log(`${judging ? "" : "\n"}Reports: ${outDir}`);
|
|
107
|
+
}
|
|
108
|
+
// One run per URL and form factor: fewer means Lighthouse returned nothing for one.
|
|
109
|
+
const failed = runs.length < positionals.length * 2 ||
|
|
110
|
+
runs.some((r) => r.error) ||
|
|
111
|
+
over.length > 0;
|
|
112
|
+
process.exit(failed ? 1 : 0);
|
|
@@ -1,2 +1,7 @@
|
|
|
1
1
|
import type { LighthouseRun } from "./types.ts";
|
|
2
|
-
|
|
2
|
+
/**
|
|
3
|
+
* Lighthouse on a phone and on a desktop for each URL; the HTML report and the final
|
|
4
|
+
* screenshot go to `outDir`. With `times` > 1 each one is run that many times and the
|
|
5
|
+
* middle run is kept (its report and screenshot too).
|
|
6
|
+
*/
|
|
7
|
+
export declare function runLighthouse(urls: string[], outDir: string, times?: number): Promise<LighthouseRun[]>;
|
package/dist/audit/lighthouse.js
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import * as chromeLauncher from "chrome-launcher";
|
|
2
2
|
import lighthouse from "lighthouse";
|
|
3
3
|
import desktopConfig from "lighthouse/core/config/desktop-config.js";
|
|
4
|
-
import {
|
|
4
|
+
import { spawn } from "node:child_process";
|
|
5
|
+
import { existsSync } from "node:fs";
|
|
6
|
+
import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
|
5
7
|
import { tmpdir } from "node:os";
|
|
6
8
|
import { join } from "node:path";
|
|
9
|
+
import { executablePath } from "../browser/index.js";
|
|
7
10
|
import { extraHeaders } from "./fetch.js";
|
|
8
11
|
const METRICS = {
|
|
9
12
|
lcpMs: "largest-contentful-paint",
|
|
@@ -13,72 +16,154 @@ const METRICS = {
|
|
|
13
16
|
speedIndexMs: "speed-index",
|
|
14
17
|
ttfbMs: "server-response-time",
|
|
15
18
|
};
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
19
|
+
/** The node of the "Largest Contentful Paint element" audit: a list whose first table holds it. */
|
|
20
|
+
function lcpElementOf(details) {
|
|
21
|
+
const lists = details?.items ??
|
|
22
|
+
[];
|
|
23
|
+
const node = lists.flatMap((l) => l.items ?? []).find((i) => i.node)?.node;
|
|
24
|
+
if (!node?.selector && !node?.snippet)
|
|
25
|
+
return null;
|
|
26
|
+
const snippet = (node.snippet ?? "").replace(/\s+/g, " ");
|
|
27
|
+
return [
|
|
28
|
+
node.selector,
|
|
29
|
+
snippet.length > 120 ? `${snippet.slice(0, 120)}…` : snippet,
|
|
30
|
+
]
|
|
31
|
+
.filter(Boolean)
|
|
32
|
+
.join(" ");
|
|
33
|
+
}
|
|
34
|
+
/** The middle run of several: by performance score, then by LCP. Lab runs vary; one run proves little. */
|
|
35
|
+
function median(tries) {
|
|
36
|
+
const sorted = [...tries].sort((a, b) => (a.run.scores.performance ?? -1) - (b.run.scores.performance ?? -1) ||
|
|
37
|
+
(b.run.metrics.lcpMs ?? Infinity) - (a.run.metrics.lcpMs ?? Infinity));
|
|
38
|
+
return sorted[Math.floor((sorted.length - 1) / 2)];
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Starts a headless Chrome with its profile in the system temp folder and returns the
|
|
42
|
+
* DevTools port; `stop()` closes it and removes the profile.
|
|
43
|
+
*
|
|
44
|
+
* Not `chromeLauncher.launch()`: on WSL it asks Windows for a temp folder even for a
|
|
45
|
+
* Linux Chrome. With its own profile, Chrome gets a Windows path it reads as a relative
|
|
46
|
+
* one, and a folder named "C:\Users\...\lighthouse.*" appears where you run the
|
|
47
|
+
* command (what the Lighthouse CLI does). With `userDataDir: false` it still leaves a
|
|
48
|
+
* folder of logs in Windows' AppData\Local at every run.
|
|
49
|
+
*/
|
|
50
|
+
async function startChrome() {
|
|
51
|
+
const chromePath = process.env.CHROME_PATH ??
|
|
52
|
+
(existsSync(executablePath)
|
|
53
|
+
? executablePath
|
|
54
|
+
: chromeLauncher.getChromePath());
|
|
55
|
+
const profile = await mkdtemp(join(tmpdir(), "stack-tools-chrome-"));
|
|
56
|
+
const child = spawn(chromePath, [
|
|
57
|
+
...chromeLauncher.Launcher.defaultFlags(),
|
|
58
|
+
"--headless=new",
|
|
59
|
+
"--no-first-run",
|
|
60
|
+
"--remote-debugging-port=0",
|
|
61
|
+
`--user-data-dir=${profile}`,
|
|
62
|
+
"about:blank",
|
|
63
|
+
], { stdio: "ignore" });
|
|
64
|
+
let gone = false;
|
|
65
|
+
const exited = new Promise((done) => {
|
|
66
|
+
const end = () => {
|
|
67
|
+
gone = true;
|
|
68
|
+
done();
|
|
69
|
+
};
|
|
70
|
+
child.once("exit", end);
|
|
71
|
+
child.once("error", end);
|
|
28
72
|
});
|
|
73
|
+
const stop = async () => {
|
|
74
|
+
if (!gone)
|
|
75
|
+
child.kill();
|
|
76
|
+
await exited;
|
|
77
|
+
await rm(profile, {
|
|
78
|
+
recursive: true,
|
|
79
|
+
force: true,
|
|
80
|
+
maxRetries: 10,
|
|
81
|
+
retryDelay: 200,
|
|
82
|
+
});
|
|
83
|
+
};
|
|
84
|
+
// Chrome writes the port it picked on the first line of this file.
|
|
85
|
+
for (let i = 0; i < 80 && !gone; i++) {
|
|
86
|
+
const text = await readFile(join(profile, "DevToolsActivePort"), "utf8").catch(() => "");
|
|
87
|
+
const port = Number(text.split("\n")[0]);
|
|
88
|
+
if (port)
|
|
89
|
+
return { port, stop };
|
|
90
|
+
await new Promise((r) => setTimeout(r, 250));
|
|
91
|
+
}
|
|
92
|
+
await stop();
|
|
93
|
+
throw new Error(`Chrome didn't start (${chromePath})`);
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Lighthouse on a phone and on a desktop for each URL; the HTML report and the final
|
|
97
|
+
* screenshot go to `outDir`. With `times` > 1 each one is run that many times and the
|
|
98
|
+
* middle run is kept (its report and screenshot too).
|
|
99
|
+
*/
|
|
100
|
+
export async function runLighthouse(urls, outDir, times = 1) {
|
|
101
|
+
const chrome = await startChrome();
|
|
29
102
|
const runs = [];
|
|
30
103
|
try {
|
|
31
104
|
for (const [i, url] of urls.entries()) {
|
|
32
105
|
for (const formFactor of ["mobile", "desktop"]) {
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
106
|
+
const tries = [];
|
|
107
|
+
for (let t = 0; t < Math.max(1, times); t++) {
|
|
108
|
+
const audit = () => lighthouse(url, {
|
|
109
|
+
port: chrome.port,
|
|
110
|
+
output: "html",
|
|
111
|
+
logLevel: "error",
|
|
112
|
+
extraHeaders,
|
|
113
|
+
}, formFactor === "desktop" ? desktopConfig : undefined);
|
|
114
|
+
let result = await audit();
|
|
115
|
+
// Trace errors like NO_NAVSTART are often transient; one retry is usually enough
|
|
116
|
+
if (result?.lhr.runtimeError)
|
|
117
|
+
result = (await audit()) ?? result;
|
|
118
|
+
if (!result)
|
|
119
|
+
continue;
|
|
120
|
+
const { lhr } = result;
|
|
121
|
+
const reportFile = `lighthouse-${i + 1}-${formFactor}.html`;
|
|
122
|
+
const shot = lhr.audits["final-screenshot"]?.details?.data?.match(/^data:image\/jpeg;base64,(.+)$/)?.[1];
|
|
123
|
+
const screenshot = shot ? `screen-${i + 1}-${formFactor}.jpg` : null;
|
|
124
|
+
const resources = lhr.audits["resource-summary"]?.details?.items ?? [];
|
|
125
|
+
const bytesOf = (type) => resources.find((r) => r.resourceType === type)?.transferSize ??
|
|
126
|
+
null;
|
|
127
|
+
const metricError = Object.values(METRICS)
|
|
128
|
+
.map((id) => lhr.audits[id]?.errorMessage)
|
|
129
|
+
.find(Boolean);
|
|
130
|
+
tries.push({
|
|
131
|
+
report: result.report,
|
|
132
|
+
shot,
|
|
133
|
+
run: {
|
|
134
|
+
url,
|
|
135
|
+
formFactor,
|
|
136
|
+
scores: Object.fromEntries(Object.entries(lhr.categories).map(([k, c]) => [
|
|
137
|
+
k,
|
|
138
|
+
c.score === null ? null : Math.round(c.score * 100),
|
|
139
|
+
])),
|
|
140
|
+
metrics: Object.fromEntries(Object.entries(METRICS).map(([k, id]) => [
|
|
141
|
+
k,
|
|
142
|
+
lhr.audits[id]?.numericValue ?? null,
|
|
143
|
+
])),
|
|
144
|
+
totalBytes: bytesOf("total"),
|
|
145
|
+
scriptBytes: bytesOf("script"),
|
|
146
|
+
imageBytes: bytesOf("image"),
|
|
147
|
+
thirdPartyBytes: bytesOf("third-party"),
|
|
148
|
+
error: lhr.runtimeError?.message ?? metricError ?? null,
|
|
149
|
+
reportFile,
|
|
150
|
+
screenshot,
|
|
151
|
+
lcpElement: lcpElementOf(lhr.audits["largest-contentful-paint-element"]?.details),
|
|
152
|
+
},
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
if (!tries.length)
|
|
44
156
|
continue;
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
if (shot && screenshot)
|
|
51
|
-
await writeFile(join(outDir, screenshot), Buffer.from(shot, "base64"));
|
|
52
|
-
const resources = lhr.audits["resource-summary"]?.details?.items ?? [];
|
|
53
|
-
const bytesOf = (type) => resources.find((r) => r.resourceType === type)?.transferSize ?? null;
|
|
54
|
-
const metricError = Object.values(METRICS)
|
|
55
|
-
.map((id) => lhr.audits[id]?.errorMessage)
|
|
56
|
-
.find(Boolean);
|
|
57
|
-
runs.push({
|
|
58
|
-
url,
|
|
59
|
-
formFactor,
|
|
60
|
-
scores: Object.fromEntries(Object.entries(lhr.categories).map(([k, c]) => [
|
|
61
|
-
k,
|
|
62
|
-
c.score === null ? null : Math.round(c.score * 100),
|
|
63
|
-
])),
|
|
64
|
-
metrics: Object.fromEntries(Object.entries(METRICS).map(([k, id]) => [
|
|
65
|
-
k,
|
|
66
|
-
lhr.audits[id]?.numericValue ?? null,
|
|
67
|
-
])),
|
|
68
|
-
totalBytes: bytesOf("total"),
|
|
69
|
-
scriptBytes: bytesOf("script"),
|
|
70
|
-
imageBytes: bytesOf("image"),
|
|
71
|
-
thirdPartyBytes: bytesOf("third-party"),
|
|
72
|
-
error: lhr.runtimeError?.message ?? metricError ?? null,
|
|
73
|
-
reportFile,
|
|
74
|
-
screenshot,
|
|
75
|
-
});
|
|
157
|
+
const kept = median(tries);
|
|
158
|
+
await writeFile(join(outDir, kept.run.reportFile), kept.report);
|
|
159
|
+
if (kept.shot && kept.run.screenshot)
|
|
160
|
+
await writeFile(join(outDir, kept.run.screenshot), Buffer.from(kept.shot, "base64"));
|
|
161
|
+
runs.push(kept.run);
|
|
76
162
|
}
|
|
77
163
|
}
|
|
78
164
|
}
|
|
79
165
|
finally {
|
|
80
|
-
chrome.
|
|
81
|
-
await rm(userDataDir, { recursive: true, force: true });
|
|
166
|
+
await chrome.stop();
|
|
82
167
|
}
|
|
83
168
|
return runs;
|
|
84
169
|
}
|
package/dist/audit/types.d.ts
CHANGED
|
@@ -63,6 +63,8 @@ export type LighthouseRun = {
|
|
|
63
63
|
reportFile: string;
|
|
64
64
|
/** What the page looked like at the end of the load (Lighthouse's final screenshot), a JPEG next to the report. */
|
|
65
65
|
screenshot: string | null;
|
|
66
|
+
/** The element Lighthouse timed as the largest contentful paint: its selector and the start of its HTML. */
|
|
67
|
+
lcpElement?: string | null;
|
|
66
68
|
};
|
|
67
69
|
/** Real visitors' numbers from the Chrome UX Report (75th percentile, last 28 days), phones. */
|
|
68
70
|
export type Crux = {
|
package/dist/browser/index.js
CHANGED
|
@@ -5,17 +5,13 @@
|
|
|
5
5
|
// It is started as a plain program, not by Playwright, so it has no automation
|
|
6
6
|
// flags and no "controlled by automated test software" bar; the tools attach over
|
|
7
7
|
// the DevTools protocol on 127.0.0.1 only. Nothing here disguises the browser:
|
|
8
|
-
// a site that still refuses it is respected
|
|
8
|
+
// a site that still refuses it is respected.
|
|
9
9
|
import { spawn } from "node:child_process";
|
|
10
|
-
import { existsSync } from "node:fs";
|
|
11
10
|
import { homedir } from "node:os";
|
|
12
11
|
import { join } from "node:path";
|
|
13
12
|
import { chromium } from "playwright-core";
|
|
14
|
-
// The profile keeps your sign-ins.
|
|
15
|
-
|
|
16
|
-
const legacyProfile = join(homedir(), ".cache/dealer-platform/chrome-profile");
|
|
17
|
-
export const PROFILE = process.env.MY_CHROME_PROFILE ??
|
|
18
|
-
(existsSync(legacyProfile) ? legacyProfile : join(homedir(), ".cache/stack-tools/chrome-profile"));
|
|
13
|
+
// The profile keeps your sign-ins.
|
|
14
|
+
export const PROFILE = process.env.MY_CHROME_PROFILE ?? join(homedir(), ".cache/stack-tools/chrome-profile");
|
|
19
15
|
export const PORT = Number(process.env.MY_CHROME_PORT ?? 9222);
|
|
20
16
|
export const executablePath = process.env.CHROME_PATH ??
|
|
21
17
|
join(homedir(), ".cache/ms-playwright/chromium-1194/chrome-linux/chrome");
|
package/dist/cli.js
CHANGED
|
@@ -14,9 +14,10 @@ const commands = {
|
|
|
14
14
|
refs: { file: "./refs/cli.js", about: "desktop + mobile screenshots of sites: <url...> [--full] [--mine] [--out dir | --out-root dir]" },
|
|
15
15
|
snap: { file: "./refs/snapshot.js", keepName: true, about: "computed styles + screenshots of pages: <dir> <url...>" },
|
|
16
16
|
diff: { file: "./refs/snapshot.js", keepName: true, about: "what changed between two snaps: <before> <after>" },
|
|
17
|
-
text: { file: "./refs/text.js", about: "readability, text size, contrast, fields, tap targets
|
|
18
|
-
glyphs: { file: "./refs/glyphs.js", about: "characters the page fonts do not cover: <url...>" },
|
|
19
|
-
images: { file: "./refs/images.js", about: "which image file each slot downloads, weights, lazy LCP: <url...>" },
|
|
17
|
+
text: { file: "./refs/text.js", about: "readability, text size, contrast, fields, tap targets, text at 200%: <url...> [--verbose] [--login email] [--brief|--json]" },
|
|
18
|
+
glyphs: { file: "./refs/glyphs.js", about: "characters the page fonts do not cover: <url...> [--brief|--json]" },
|
|
19
|
+
images: { file: "./refs/images.js", about: "which image file each slot downloads, weights, lazy LCP: <url...> [--brief|--json]" },
|
|
20
|
+
lighthouse: { file: "./audit/lighthouse-cli.js", about: "Lighthouse on phone and desktop, judged against the budgets with --budget: <url...> [--budget] [--min-perf n] [--runs n] [--out dir] [--json]" },
|
|
20
21
|
audit: { file: "./audit/cli.js", about: "audit a site (SEO, AI visibility, speed, contacts): <url> [--profile file.ts] [--render] [--teaser]" },
|
|
21
22
|
"audit-rerender": { file: "./audit/rerender.js", about: "rebuild an audit report from its findings.json: <dir> [--profile file.ts] [--teaser]" }
|
|
22
23
|
};
|
package/dist/refs/cli.js
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
// Cloudflare challenge). We report that and move on; we don't try to get past
|
|
8
8
|
// it. With --mine the shots are taken in a new tab of Alessandro's own Chrome
|
|
9
9
|
// (`stack-tools browser`), as he would see the page; a site that still
|
|
10
|
-
// refuses is captured by hand
|
|
10
|
+
// refuses is captured by hand.
|
|
11
11
|
import { connectMyChrome } from "../browser/index.js";
|
|
12
12
|
import { chromium } from "playwright-core";
|
|
13
13
|
import { mkdir, writeFile } from "node:fs/promises";
|
package/dist/refs/glyphs.js
CHANGED
|
@@ -4,21 +4,33 @@
|
|
|
4
4
|
// draws it from a system font, with other metrics and baseline (the "→" that sat low on
|
|
5
5
|
// some phones). Exit code 1 when something is missing, so it can gate CI.
|
|
6
6
|
//
|
|
7
|
-
// Usage: stack-tools glyphs <url...>
|
|
7
|
+
// Usage: stack-tools glyphs <url...> [--brief | --full | --json]
|
|
8
|
+
// (e.g. the home, the catalog, an item page, the about page)
|
|
9
|
+
//
|
|
10
|
+
// --brief (or STACK_TOOLS_OUTPUT=brief): one line per page with its missing characters;
|
|
11
|
+
// --json: the same as JSON; --full: one list for all the pages (the default).
|
|
8
12
|
import { chromium } from "playwright-core";
|
|
9
13
|
import { homedir } from "node:os";
|
|
10
14
|
import { join } from "node:path";
|
|
11
|
-
|
|
15
|
+
import { parseArgs } from "node:util";
|
|
16
|
+
import { outputMode, outputOptions, outputUsage, Report } from "../report.js";
|
|
17
|
+
const { values, positionals: urls } = parseArgs({
|
|
18
|
+
allowPositionals: true,
|
|
19
|
+
options: outputOptions,
|
|
20
|
+
});
|
|
12
21
|
if (!urls.length) {
|
|
13
|
-
console.error(
|
|
22
|
+
console.error(`Usage: stack-tools glyphs <url...> ${outputUsage}`);
|
|
14
23
|
process.exit(1);
|
|
15
24
|
}
|
|
25
|
+
const out = new Report("glyphs", urls, outputMode(values));
|
|
16
26
|
const executablePath = process.env.CHROME_PATH ??
|
|
17
27
|
join(homedir(), ".cache/ms-playwright/chromium-1194/chrome-linux/chrome");
|
|
18
28
|
const browser = await chromium.launch({ executablePath });
|
|
19
29
|
const all = new Map();
|
|
20
30
|
try {
|
|
21
31
|
for (const url of urls) {
|
|
32
|
+
const pg = out.page(url);
|
|
33
|
+
const here = new Set();
|
|
22
34
|
for (const width of [390, 1440]) {
|
|
23
35
|
const page = await browser.newPage({ viewport: { width, height: 900 } });
|
|
24
36
|
await page.goto(url, { waitUntil: "networkidle" });
|
|
@@ -79,23 +91,26 @@ try {
|
|
|
79
91
|
});
|
|
80
92
|
for (const m of missing) {
|
|
81
93
|
const key = `${m.family} ${m.code}`;
|
|
94
|
+
if (!here.has(key)) {
|
|
95
|
+
here.add(key);
|
|
96
|
+
pg.fail("missing-glyph", `${m.char} ${m.code} in ${m.family} e.g. "${m.sample}"`);
|
|
97
|
+
}
|
|
82
98
|
const entry = all.get(key) ?? { ...m, pages: new Set() };
|
|
83
99
|
entry.pages.add(url);
|
|
84
100
|
all.set(key, entry);
|
|
85
101
|
}
|
|
86
102
|
await page.close();
|
|
87
103
|
}
|
|
104
|
+
pg.end();
|
|
88
105
|
}
|
|
89
106
|
}
|
|
90
107
|
finally {
|
|
91
108
|
await browser.close();
|
|
92
109
|
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
}
|
|
96
|
-
else {
|
|
97
|
-
console.log(`${all.size} character(s) drawn from a system font:`);
|
|
110
|
+
// The full form: one list for all the pages, each character once.
|
|
111
|
+
if (all.size) {
|
|
112
|
+
out.say(`${all.size} character(s) drawn from a system font:`);
|
|
98
113
|
for (const m of all.values())
|
|
99
|
-
|
|
100
|
-
process.exitCode = 1;
|
|
114
|
+
out.say(` ${m.char} ${m.code} in ${m.family} e.g. "${m.sample}" (${[...m.pages].length} page(s))`);
|
|
101
115
|
}
|
|
116
|
+
out.done(all.size ? "" : `All characters covered by our fonts on ${urls.length} page(s).`);
|