@bymaksym/loadline 0.1.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/LICENSE +21 -0
- package/README.md +182 -0
- package/bin/loadline.js +50 -0
- package/dist/cli/cli/args.js +324 -0
- package/dist/cli/cli/args.types.js +3 -0
- package/dist/cli/cli/gates.js +173 -0
- package/dist/cli/cli/gates.types.js +3 -0
- package/dist/cli/cli/main.js +156 -0
- package/dist/cli/cli/read-build.js +382 -0
- package/dist/cli/cli/read-build.types.js +3 -0
- package/dist/cli/cli/render-json.js +116 -0
- package/dist/cli/cli/render-json.types.js +10 -0
- package/dist/cli/cli/render-markdown.js +30 -0
- package/dist/cli/cli/render-pr.js +76 -0
- package/dist/cli/cli/render-sarif.js +83 -0
- package/dist/cli/cli/render-text.js +388 -0
- package/dist/cli/cli/report.js +196 -0
- package/dist/cli/cli/report.types.js +3 -0
- package/dist/cli/cli/self-check.js +85 -0
- package/dist/cli/cli/text.js +180 -0
- package/dist/cli/cli/text.types.js +10 -0
- package/dist/cli/src/app/core/analysis/analysis.js +661 -0
- package/dist/cli/src/app/core/analysis/analysis.types.js +6 -0
- package/dist/cli/src/app/core/analysis/barrels.js +128 -0
- package/dist/cli/src/app/core/analysis/blast.js +172 -0
- package/dist/cli/src/app/core/analysis/cycles.js +110 -0
- package/dist/cli/src/app/core/analysis/delivery.js +143 -0
- package/dist/cli/src/app/core/analysis/entries.js +217 -0
- package/dist/cli/src/app/core/analysis/exclusive.js +91 -0
- package/dist/cli/src/app/core/analysis/importers.js +38 -0
- package/dist/cli/src/app/core/analysis/insights.js +202 -0
- package/dist/cli/src/app/core/analysis/insights.types.js +10 -0
- package/dist/cli/src/app/core/analysis/invariant.js +69 -0
- package/dist/cli/src/app/core/analysis/metafile.types.js +44 -0
- package/dist/cli/src/app/core/analysis/module-graph.js +192 -0
- package/dist/cli/src/app/core/analysis/sourcemap.js +231 -0
- package/dist/cli/src/app/core/analysis/sourcemap.types.js +3 -0
- package/dist/cli/src/app/core/analysis/split-drift.js +47 -0
- package/dist/cli/src/app/core/assets/assets.js +296 -0
- package/dist/cli/src/app/core/assets/assets.types.js +11 -0
- package/dist/cli/src/app/core/baseline/baseline.js +0 -0
- package/dist/cli/src/app/core/baseline/baseline.types.js +3 -0
- package/dist/cli/src/app/core/caching/caching.js +241 -0
- package/dist/cli/src/app/core/caching/caching.types.js +10 -0
- package/dist/cli/src/app/core/caching/from-analysis.js +73 -0
- package/dist/cli/src/app/core/config/loadline-config.js +110 -0
- package/dist/cli/src/app/core/config/loadline-config.types.js +12 -0
- package/dist/cli/src/app/core/criteria/criteria.js +228 -0
- package/dist/cli/src/app/core/criteria/criteria.types.js +3 -0
- package/dist/cli/src/app/core/deps/deps.js +225 -0
- package/dist/cli/src/app/core/deps/deps.types.js +15 -0
- package/dist/cli/src/app/core/export/markdown-table.utils.js +40 -0
- package/dist/cli/src/app/core/export/summary.js +72 -0
- package/dist/cli/src/app/core/findings/actions.js +59 -0
- package/dist/cli/src/app/core/findings/assets.js +130 -0
- package/dist/cli/src/app/core/findings/caching.js +165 -0
- package/dist/cli/src/app/core/findings/compose.js +41 -0
- package/dist/cli/src/app/core/findings/deps.js +66 -0
- package/dist/cli/src/app/core/findings/effort.js +117 -0
- package/dist/cli/src/app/core/findings/finding-html.js +13 -0
- package/dist/cli/src/app/core/findings/finding-plain.js +26 -0
- package/dist/cli/src/app/core/findings/finding-text.js +1231 -0
- package/dist/cli/src/app/core/findings/finding.types.js +80 -0
- package/dist/cli/src/app/core/findings/findings.js +514 -0
- package/dist/cli/src/app/core/findings/graph.js +167 -0
- package/dist/cli/src/app/core/findings/own-in-boot.js +88 -0
- package/dist/cli/src/app/core/findings/page.js +49 -0
- package/dist/cli/src/app/core/findings/requests.js +185 -0
- package/dist/cli/src/app/core/findings/scan.js +126 -0
- package/dist/cli/src/app/core/findings/shipped.js +244 -0
- package/dist/cli/src/app/core/findings/situation.js +116 -0
- package/dist/cli/src/app/core/format/format.utils.js +204 -0
- package/dist/cli/src/app/core/i18n/en.js +819 -0
- package/dist/cli/src/app/core/i18n/es.js +822 -0
- package/dist/cli/src/app/core/i18n/ui-strings.js +11 -0
- package/dist/cli/src/app/core/i18n/ui.js +7 -0
- package/dist/cli/src/app/core/intake/bundle-graph.js +346 -0
- package/dist/cli/src/app/core/intake/bundle-graph.types.js +3 -0
- package/dist/cli/src/app/core/intake/dist-files.js +160 -0
- package/dist/cli/src/app/core/intake/index-html.js +292 -0
- package/dist/cli/src/app/core/json/json.utils.js +55 -0
- package/dist/cli/src/app/core/measurement/measurement.types.js +14 -0
- package/dist/cli/src/app/core/project/budget-advice.js +76 -0
- package/dist/cli/src/app/core/project/project-context.js +394 -0
- package/dist/cli/src/app/core/project/project-context.types.js +3 -0
- package/dist/cli/src/app/core/scan/catalog.js +67 -0
- package/dist/cli/src/app/core/scan/scan.js +280 -0
- package/dist/cli/src/app/core/scan/scan.types.js +10 -0
- package/dist/cli/src/app/core/scan/secrets.js +0 -0
- package/dist/cli/src/app/core/situation/situation.js +261 -0
- package/dist/cli/src/app/core/situation/situation.types.js +19 -0
- package/dist/cli/src/app/core/timing/timing.js +87 -0
- package/dist/cli/src/app/core/timing/timing.types.js +3 -0
- package/dist/cli/src/app/core/whatif/defer.js +68 -0
- package/dist/cli/src/app/state/report-messages.utils.js +73 -0
- package/docs/HOW-IT-WORKS.md +1087 -0
- package/loadline.html +78 -0
- package/package.json +154 -0
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* What `index.html` tells the browser to fetch before it has parsed a single chunk: the entry
|
|
4
|
+
* script and the `modulepreload` links.
|
|
5
|
+
*
|
|
6
|
+
* It matters because the import graph cannot tell the two apart. A bootstrap chunk named by this
|
|
7
|
+
* page is asked for at once, together with the rest; one that is not is only discovered when the
|
|
8
|
+
* chunk importing it has arrived and been parsed, which is another round trip. Same bytes, later.
|
|
9
|
+
*
|
|
10
|
+
* Read with regular expressions rather than `DOMParser` so the command line reads it the same way
|
|
11
|
+
* the page does: the terminal has no DOM, and two readers would drift apart.
|
|
12
|
+
*/
|
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
exports.originsIn = exports.stylesIn = exports.assetsIn = exports.announcedIn = exports.scriptsIn = exports.indexHtmlOf = void 0;
|
|
15
|
+
/** Angular writes `index.html`; with server-side rendering the browser one is `index.csr.html`. */
|
|
16
|
+
const INDEX_NAME = /^index(\.[\w-]+)?\.html$/i;
|
|
17
|
+
const TAG = /<(script|link)\b([^>]*)>/gi;
|
|
18
|
+
/** Images written into the page itself: they are asked for as soon as the tag is parsed. */
|
|
19
|
+
const IMG = /<img\b([^>]*)>/gi;
|
|
20
|
+
/** `<base href>`: one tag that moves every relative URL of the page somewhere else. */
|
|
21
|
+
const BASE = /<(base)\b([^>]*)>/gi;
|
|
22
|
+
/**
|
|
23
|
+
* An inline `<script>` with its body, which is the other way a page starts an application: rather
|
|
24
|
+
* than `<script type="module" src>`, SvelteKit writes the chunk names into an `import()` inside the
|
|
25
|
+
* page and calls `start()` with the result. Nothing outside that script names those chunks, so
|
|
26
|
+
* without reading it the build has no entry at all and cannot be read — which is what it did.
|
|
27
|
+
*/
|
|
28
|
+
const INLINE_SCRIPT = /<script\b([^>]*)>([\S\s]*?)<\/script>/gi;
|
|
29
|
+
/** `import("./chunk.js")` as it is written in a page: a literal specifier, never a variable. */
|
|
30
|
+
const IMPORT_CALL = /\bimport\s*\(\s*(["'`])([^"'`]+)\1\s*\)/g;
|
|
31
|
+
const ATTRIBUTE = (name) => new RegExp(String.raw `\b${name}\s*=\s*("([^"]*)"|'([^']*)'|([^\s>]+))`, 'i');
|
|
32
|
+
const attribute = (tag, name) => {
|
|
33
|
+
const found = ATTRIBUTE(name).exec(tag);
|
|
34
|
+
return found ? (found[2] ?? found[3] ?? found[4] ?? '') : null;
|
|
35
|
+
};
|
|
36
|
+
/** The file name of a URL, with the query and the hash off: how the metafile keys its outputs. */
|
|
37
|
+
const fileNameOf = (url) => {
|
|
38
|
+
const path = /^[^?#]*/.exec(url)?.[0] ?? '';
|
|
39
|
+
return path.slice(path.lastIndexOf('/') + 1);
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Whichever of the dropped files is the page. `null` when the folder does not carry one.
|
|
43
|
+
*
|
|
44
|
+
* A build whose page is not called `index.html` used to fall through here and lose the round trips
|
|
45
|
+
* of the first load with no explanation. When nothing matches the name, a folder holding exactly
|
|
46
|
+
* one HTML file leaves no room for doubt about which page it is.
|
|
47
|
+
*/
|
|
48
|
+
const indexHtmlOf = (files) => {
|
|
49
|
+
const named = files.find(file => INDEX_NAME.test(file.name));
|
|
50
|
+
if (named) {
|
|
51
|
+
return named;
|
|
52
|
+
}
|
|
53
|
+
const pages = files.filter(file => /\.html?$/i.test(file.name));
|
|
54
|
+
return pages.length === 1 ? (pages[0] ?? null) : null;
|
|
55
|
+
};
|
|
56
|
+
exports.indexHtmlOf = indexHtmlOf;
|
|
57
|
+
/**
|
|
58
|
+
* The chunks an inline script of the page imports by hand. They are entries in the same sense a
|
|
59
|
+
* `<script src>` is: the browser runs that script as soon as it parses the page, so execution
|
|
60
|
+
* starts there.
|
|
61
|
+
*/
|
|
62
|
+
const startedInline = (html) => {
|
|
63
|
+
const started = [];
|
|
64
|
+
for (const [, attributes = '', body = ''] of html.matchAll(INLINE_SCRIPT)) {
|
|
65
|
+
// A `<script src>` has no body worth reading, and its file is already counted as the entry.
|
|
66
|
+
if (attribute(attributes, 'src') !== null) {
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
for (const call of body.matchAll(IMPORT_CALL)) {
|
|
70
|
+
started.push(fileNameOf(call[2] ?? ''));
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return started;
|
|
74
|
+
};
|
|
75
|
+
/**
|
|
76
|
+
* The two ways a page names a script, kept apart because they mean different things. A `<script>`
|
|
77
|
+
* is where execution starts; a `modulepreload` is a file fetched early that some other file will
|
|
78
|
+
* import. Only the first one is an entry of the build.
|
|
79
|
+
*/
|
|
80
|
+
const scriptsIn = (html) => {
|
|
81
|
+
const names = new Set();
|
|
82
|
+
const entries = new Set();
|
|
83
|
+
for (const [, tag = '', rest = ''] of html.matchAll(TAG)) {
|
|
84
|
+
const isScript = tag.toLowerCase() === 'script';
|
|
85
|
+
const rel = new Set((attribute(rest, 'rel') ?? '').toLowerCase().split(/\s+/));
|
|
86
|
+
// A plain `preload` only counts when it is a script: the same tag preloads fonts and images.
|
|
87
|
+
const preloads = rel.has('modulepreload') || (rel.has('preload') && attribute(rest, 'as') === 'script');
|
|
88
|
+
if (!isScript && !preloads) {
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
const name = fileNameOf(attribute(rest, isScript ? 'src' : 'href') ?? '');
|
|
92
|
+
if (/\.m?js$/.test(name)) {
|
|
93
|
+
names.add(name);
|
|
94
|
+
if (isScript) {
|
|
95
|
+
entries.add(name);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
// After the tags, so the order stays the order the page names things in: the links come first
|
|
100
|
+
// in the document and the script that starts the application is at the end of it.
|
|
101
|
+
for (const name of startedInline(html)) {
|
|
102
|
+
if (!/\.m?js$/.test(name)) {
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
names.add(name);
|
|
106
|
+
entries.add(name);
|
|
107
|
+
}
|
|
108
|
+
return { names: [...names], entries: [...entries] };
|
|
109
|
+
};
|
|
110
|
+
exports.scriptsIn = scriptsIn;
|
|
111
|
+
/** The JavaScript files the page names, as bare file names, in the order the page names them. */
|
|
112
|
+
const announcedIn = (html) => (0, exports.scriptsIn)(html).names;
|
|
113
|
+
exports.announcedIn = announcedIn;
|
|
114
|
+
/**
|
|
115
|
+
* Everything else the page names before anything is painted: the images written into the HTML, and
|
|
116
|
+
* whatever it preloads.
|
|
117
|
+
*
|
|
118
|
+
* Fonts are the reason this exists. A `<link rel="preload" as="font">` is a file the browser fetches
|
|
119
|
+
* at once, before it has parsed a stylesheet, and a page preloading seven weights of one family
|
|
120
|
+
* pays for all seven on the first load. A font the page does **not** preload is fetched later, once
|
|
121
|
+
* the CSS that names it has arrived, and belongs in a different figure — so the two are kept apart
|
|
122
|
+
* rather than added together.
|
|
123
|
+
*
|
|
124
|
+
* `<img src>` is included and `srcset` is not: the browser picks one candidate from a `srcset` and
|
|
125
|
+
* counting them all would overstate the first load, which is the mistake this whole group exists
|
|
126
|
+
* to stop making in the other direction.
|
|
127
|
+
*/
|
|
128
|
+
const assetsIn = (html) => {
|
|
129
|
+
const referenced = new Set();
|
|
130
|
+
const preloaded = new Set();
|
|
131
|
+
/**
|
|
132
|
+
* Kept apart from both, because it is neither. A `prefetch` is not part of the first load — the
|
|
133
|
+
* report is right not to add it to the bootstrap — but "the browser gets to it when it is idle"
|
|
134
|
+
* turned out to be a comfortable half-truth: measured against a real Nuxt build, Chrome fetched
|
|
135
|
+
* all three of them during the first page load. Counting them nowhere at all meant the one
|
|
136
|
+
* finding that matters most on that framework was the one nothing said.
|
|
137
|
+
*/
|
|
138
|
+
const prefetched = new Set();
|
|
139
|
+
// The URLs as written, query and all. Everything else here works on bare file names, which is
|
|
140
|
+
// how the metafile keys things — but `app.js?v=3` is a *caching* fact that only survives in the
|
|
141
|
+
// full string, and stripping it first would make that check impossible.
|
|
142
|
+
const hrefs = new Set();
|
|
143
|
+
for (const [, tag = '', rest = ''] of html.matchAll(TAG)) {
|
|
144
|
+
const lower = tag.toLowerCase();
|
|
145
|
+
const rel = new Set((attribute(rest, 'rel') ?? '').toLowerCase().split(/\s+/));
|
|
146
|
+
const preloads = rel.has('preload') || rel.has('prefetch') || rel.has('modulepreload');
|
|
147
|
+
const url = attribute(rest, lower === 'link' ? 'href' : 'src') ?? '';
|
|
148
|
+
const source = fileNameOf(url);
|
|
149
|
+
if (!source) {
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
referenced.add(source);
|
|
153
|
+
hrefs.add(url);
|
|
154
|
+
// `prefetch` is deliberately not a preload: it is what the browser fetches when it is idle,
|
|
155
|
+
// which is the opposite of "before anything appears".
|
|
156
|
+
if (lower === 'link' && preloads && !rel.has('prefetch')) {
|
|
157
|
+
preloaded.add(source);
|
|
158
|
+
}
|
|
159
|
+
if (lower === 'link' && rel.has('prefetch')) {
|
|
160
|
+
prefetched.add(source);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
// An `<img>` is not in `TAG`, which only matches the two tags the scripts and styles use. It is
|
|
164
|
+
// matched on its own so a hero image written into the page is counted where it is paid for.
|
|
165
|
+
for (const [, rest = ''] of html.matchAll(IMG)) {
|
|
166
|
+
const url = attribute(rest, 'src') ?? '';
|
|
167
|
+
const source = fileNameOf(url);
|
|
168
|
+
if (source) {
|
|
169
|
+
referenced.add(source);
|
|
170
|
+
hrefs.add(url);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return {
|
|
174
|
+
referenced: [...referenced],
|
|
175
|
+
preloaded: [...preloaded],
|
|
176
|
+
prefetched: [...prefetched],
|
|
177
|
+
hrefs: [...hrefs],
|
|
178
|
+
};
|
|
179
|
+
};
|
|
180
|
+
exports.assetsIn = assetsIn;
|
|
181
|
+
/**
|
|
182
|
+
* The stylesheets the page asks for, which is the other half of the first load and the half this
|
|
183
|
+
* tool has never counted.
|
|
184
|
+
*
|
|
185
|
+
* It matters more than it looks. A `<link rel="stylesheet">` in the head is **render-blocking**:
|
|
186
|
+
* the browser will not paint until it has arrived, which is a stronger claim than the one made
|
|
187
|
+
* about any script. Loadline's own build reports a bootstrap of 156 kB and asks for a 69 kB
|
|
188
|
+
* stylesheet in the same page — a headline understated by 31 % on the one figure the tool is
|
|
189
|
+
* named after, and its own sentence said "index.html announces the only bootstrap chunk".
|
|
190
|
+
*
|
|
191
|
+
* A `media="print"` link is left out on purpose: that is the trick for loading a stylesheet
|
|
192
|
+
* without blocking the paint, and counting it would punish exactly the thing worth doing. The
|
|
193
|
+
* same file usually appears twice in an Angular page — once with the trick and once without — and
|
|
194
|
+
* a set keeps it one file.
|
|
195
|
+
*/
|
|
196
|
+
const stylesIn = (html) => {
|
|
197
|
+
const names = new Set();
|
|
198
|
+
for (const [, tag = '', rest = ''] of html.matchAll(TAG)) {
|
|
199
|
+
if (tag.toLowerCase() !== 'link') {
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
const rel = new Set((attribute(rest, 'rel') ?? '').toLowerCase().split(/\s+/));
|
|
203
|
+
const isSheet = rel.has('stylesheet') || (rel.has('preload') && attribute(rest, 'as') === 'style');
|
|
204
|
+
if (!isSheet || (attribute(rest, 'media') ?? '').toLowerCase() === 'print') {
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
const name = fileNameOf(attribute(rest, 'href') ?? '');
|
|
208
|
+
if (/\.css$/i.test(name)) {
|
|
209
|
+
names.add(name);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
return [...names];
|
|
213
|
+
};
|
|
214
|
+
exports.stylesIn = stylesIn;
|
|
215
|
+
/**
|
|
216
|
+
* An absolute URL's origin — scheme and host — or `null` when the URL is relative.
|
|
217
|
+
*
|
|
218
|
+
* A protocol-relative `//cdn.example.com/main.js` counts: it is a different host, which is the
|
|
219
|
+
* whole of what matters here, and the scheme it inherits changes nothing about the handshake.
|
|
220
|
+
*/
|
|
221
|
+
const ABSOLUTE = /^(?:(https?:)?\/\/)([^/?#]+)/i;
|
|
222
|
+
const originOf = (url) => {
|
|
223
|
+
const found = ABSOLUTE.exec(url.trim());
|
|
224
|
+
if (!found) {
|
|
225
|
+
return null;
|
|
226
|
+
}
|
|
227
|
+
return `${(found[1] ?? '').toLowerCase()}//${(found[2] ?? '').toLowerCase()}`;
|
|
228
|
+
};
|
|
229
|
+
/**
|
|
230
|
+
* Where the page fetches from, when that is not itself.
|
|
231
|
+
*
|
|
232
|
+
* This is a fact about the first load that the import graph cannot hold and that moves the
|
|
233
|
+
* arithmetic at the root. A chunk on another host is not one request later than the page: it is a
|
|
234
|
+
* DNS lookup, a TCP connection and a TLS handshake — or a fresh QUIC handshake — **before the
|
|
235
|
+
* first byte of the first chunk**, and under HTTP/1.1 it is also a second pool of six connections,
|
|
236
|
+
* which cuts the other way and helps. Neither half is visible in a file listing.
|
|
237
|
+
*
|
|
238
|
+
* `<base href>` is read because it moves every relative URL on the page at once, which is the one
|
|
239
|
+
* way a build with nothing but relative paths still ends up served from somewhere else.
|
|
240
|
+
*
|
|
241
|
+
* `preconnect` and `dns-prefetch` are read because they are the mitigation. A page already warming
|
|
242
|
+
* the connection to its CDN has paid attention to this and should not be told about it as if it
|
|
243
|
+
* had not.
|
|
244
|
+
*/
|
|
245
|
+
const originsIn = (html) => {
|
|
246
|
+
const byOrigin = new Map();
|
|
247
|
+
const hinted = new Set();
|
|
248
|
+
let base = null;
|
|
249
|
+
const count = (url, isScript) => {
|
|
250
|
+
const origin = originOf(url);
|
|
251
|
+
if (!origin) {
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
const row = byOrigin.get(origin) ?? { origin, files: 0, scripts: 0 };
|
|
255
|
+
row.files += 1;
|
|
256
|
+
row.scripts += isScript ? 1 : 0;
|
|
257
|
+
byOrigin.set(origin, row);
|
|
258
|
+
};
|
|
259
|
+
for (const [, tag = '', rest = ''] of html.matchAll(TAG)) {
|
|
260
|
+
const lower = tag.toLowerCase();
|
|
261
|
+
const rel = new Set((attribute(rest, 'rel') ?? '').toLowerCase().split(/\s+/));
|
|
262
|
+
const url = attribute(rest, lower === 'link' ? 'href' : 'src') ?? '';
|
|
263
|
+
// A hint names an origin and fetches nothing: it belongs on the other side of the answer.
|
|
264
|
+
if (lower === 'link' && (rel.has('preconnect') || rel.has('dns-prefetch'))) {
|
|
265
|
+
const origin = originOf(url);
|
|
266
|
+
if (origin) {
|
|
267
|
+
hinted.add(origin);
|
|
268
|
+
}
|
|
269
|
+
continue;
|
|
270
|
+
}
|
|
271
|
+
if (!url) {
|
|
272
|
+
continue;
|
|
273
|
+
}
|
|
274
|
+
count(url, lower === 'script' || rel.has('modulepreload') || attribute(rest, 'as') === 'script');
|
|
275
|
+
}
|
|
276
|
+
for (const [, rest = ''] of html.matchAll(IMG)) {
|
|
277
|
+
count(attribute(rest, 'src') ?? '', false);
|
|
278
|
+
}
|
|
279
|
+
// Last, and on its own: a `<base>` is not a file the page fetches, it is where every relative
|
|
280
|
+
// URL of the page resolves against, so one tag can move the whole build to another host.
|
|
281
|
+
for (const [, tag = '', rest = ''] of html.matchAll(BASE)) {
|
|
282
|
+
if (tag.toLowerCase() === 'base') {
|
|
283
|
+
base = originOf(attribute(rest, 'href') ?? '');
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
return {
|
|
287
|
+
origins: [...byOrigin.values()].toSorted((a, b) => b.scripts - a.scripts || b.files - a.files),
|
|
288
|
+
hinted: [...hinted],
|
|
289
|
+
base,
|
|
290
|
+
};
|
|
291
|
+
};
|
|
292
|
+
exports.originsIn = originsIn;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Reading what `JSON.parse` hands back.
|
|
4
|
+
*
|
|
5
|
+
* `JSON.parse` is typed `any`, so every file read from disk, every dropped file and every
|
|
6
|
+
* `localStorage` key enters the program as a value the compiler will agree to anything about. The
|
|
7
|
+
* habit this project already had was to annotate the result `: unknown` and then assert the shape
|
|
8
|
+
* it was hoped to be, which moves the lie one line down rather than removing it: an `as AuditJson`
|
|
9
|
+
* over `JSON.parse('null')` type-checks and then throws on the first property read.
|
|
10
|
+
*
|
|
11
|
+
* These are the two checks that make the assertion true instead of hopeful. They are deliberately
|
|
12
|
+
* shallow — one level, no schema language, no dependency — because the shapes being read are
|
|
13
|
+
* other tools' output, and the parts of them this project uses are one or two fields deep.
|
|
14
|
+
*/
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.asKeyOf = exports.asMember = exports.asText = exports.asArray = exports.asRecord = void 0;
|
|
17
|
+
/**
|
|
18
|
+
* The value as an object with string keys, or `null` when it is not one.
|
|
19
|
+
*
|
|
20
|
+
* `null`, a list, a string and a number all fail: each of them is valid JSON and none of them has
|
|
21
|
+
* the fields the caller is about to read. Lists are rejected on purpose — `Object.entries` of one
|
|
22
|
+
* succeeds and yields its indices, which is how `{"vulnerabilities": "oops"}` used to turn into
|
|
23
|
+
* four findings named `0`, `1`, `2` and `3`.
|
|
24
|
+
*
|
|
25
|
+
* The assertion after the check is the sound kind: `typeof value === 'object'` plus the two
|
|
26
|
+
* exclusions is exactly the evidence `Record<string, unknown>` claims.
|
|
27
|
+
*/
|
|
28
|
+
const asRecord = (value) => typeof value === 'object' && value !== null && !Array.isArray(value) ? value : null;
|
|
29
|
+
exports.asRecord = asRecord;
|
|
30
|
+
/** The value as a list of unknowns, or `null` when it is not a list. */
|
|
31
|
+
const asArray = (value) => (Array.isArray(value) ? value : null);
|
|
32
|
+
exports.asArray = asArray;
|
|
33
|
+
/** The value as a string, or `null`. For fields another tool may have left out or written wrong. */
|
|
34
|
+
const asText = (value) => (typeof value === 'string' ? value : null);
|
|
35
|
+
exports.asText = asText;
|
|
36
|
+
/**
|
|
37
|
+
* One member of a union of string literals, checked against the union's own members.
|
|
38
|
+
*
|
|
39
|
+
* This is what replaces `value as Mode` at the edges where a flag, a URL parameter or a stored key
|
|
40
|
+
* arrives as a `string` and is wanted as one of a handful of literals. The set carries the literal
|
|
41
|
+
* type, so adding a member to the union without adding it to the set is a compile error rather
|
|
42
|
+
* than a value that silently stops being recognised.
|
|
43
|
+
*/
|
|
44
|
+
const asMember = (value, members) => members.has(value) ? value : null;
|
|
45
|
+
exports.asMember = asMember;
|
|
46
|
+
/**
|
|
47
|
+
* One key of an object whose keys ARE the union, checked against that object.
|
|
48
|
+
*
|
|
49
|
+
* The difference from `asMember` is where the list of allowed values comes from: this takes the
|
|
50
|
+
* `Record<T, ...>` the code already has -- a table of column widths, a dictionary of labels -- so
|
|
51
|
+
* there is no second list to keep in step with the first. A union that gains a member gains it
|
|
52
|
+
* here too, because the record would not compile without it.
|
|
53
|
+
*/
|
|
54
|
+
const asKeyOf = (value, shape) => Object.hasOwn(shape, value) ? value : null;
|
|
55
|
+
exports.asKeyOf = asKeyOf;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/** The shapes of a browser measurement contrasted against the computed report. */
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.NO_PAGE = void 0;
|
|
5
|
+
/** Nothing known about the page: what an older paste, or a scraped list of names, gives. */
|
|
6
|
+
exports.NO_PAGE = {
|
|
7
|
+
origin: null,
|
|
8
|
+
ttfbMs: null,
|
|
9
|
+
protocol: null,
|
|
10
|
+
serviceWorker: null,
|
|
11
|
+
caches: null,
|
|
12
|
+
modulepreloads: null,
|
|
13
|
+
takenAt: null,
|
|
14
|
+
};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* The budget that should be in `angular.json`, and the block to paste.
|
|
4
|
+
*
|
|
5
|
+
* The tool already catches a budget that cannot fire and one that only warns. The natural next
|
|
6
|
+
* question — "then what should it say?" — is where people get stuck, and it is answerable from what
|
|
7
|
+
* is already on screen: the bootstrap as built, and the thresholds in Criteria.
|
|
8
|
+
*
|
|
9
|
+
* This is as far as "diagnose, do not fix" goes: the block is shown, and whoever decides pastes it.
|
|
10
|
+
* Nothing here writes a file.
|
|
11
|
+
*
|
|
12
|
+
* **Which figure it is against.** Angular's `initial` budget counts the raw, uncompressed initial
|
|
13
|
+
* JavaScript **and CSS**, whatever unit the report is being shown in. Comparing a suggestion
|
|
14
|
+
* against a gzip figure would produce a budget about a third of the size of the thing it guards,
|
|
15
|
+
* which would fail the next build for a reason having nothing to do with the build.
|
|
16
|
+
*/
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.screenBudgetAdvice = exports.budgetAdvice = void 0;
|
|
19
|
+
const KB = 1024;
|
|
20
|
+
/** Rounded to something a person would write down. Nobody puts `maximumError: 731 kB` in a file. */
|
|
21
|
+
const roundKb = (bytes) => {
|
|
22
|
+
const kb = bytes / KB;
|
|
23
|
+
const step = kb >= 1000 ? 100 : kb >= 200 ? 50 : 10;
|
|
24
|
+
return Math.ceil(kb / step) * step;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* @param initialRawBytes what the first load costs today in raw bytes — the bootstrap JavaScript
|
|
28
|
+
* plus the stylesheets the page asks for, since Angular counts both.
|
|
29
|
+
* @param headroom how much room the warning leaves above today. A budget with no headroom
|
|
30
|
+
* fires on the next feature and gets raised, which is how a budget stops
|
|
31
|
+
* being read; too much and it never fires, which the report already flags.
|
|
32
|
+
*/
|
|
33
|
+
const budgetAdvice = (initialRawBytes, headroom = 0.15) => {
|
|
34
|
+
if (initialRawBytes <= 0) {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
const warningKb = roundKb(initialRawBytes * (1 + headroom));
|
|
38
|
+
// The error is the line nobody wants to cross, not the next size up from the warning: it exists
|
|
39
|
+
// to stop a pipeline, so it sits far enough above that crossing it is a decision, not a sprint.
|
|
40
|
+
const errorKb = roundKb(initialRawBytes * (1 + headroom) * 1.3);
|
|
41
|
+
const snippet = [
|
|
42
|
+
'"budgets": [',
|
|
43
|
+
' {',
|
|
44
|
+
' "type": "initial",',
|
|
45
|
+
` "maximumWarning": "${warningKb}kB",`,
|
|
46
|
+
` "maximumError": "${errorKb}kB"`,
|
|
47
|
+
' }',
|
|
48
|
+
']',
|
|
49
|
+
].join('\n');
|
|
50
|
+
return { warningKb, errorKb, currentBytes: initialRawBytes, snippet };
|
|
51
|
+
};
|
|
52
|
+
exports.budgetAdvice = budgetAdvice;
|
|
53
|
+
/**
|
|
54
|
+
* The same for one screen: the budget on what a lazy chunk may weigh.
|
|
55
|
+
*
|
|
56
|
+
* Angular's `anyComponentStyle` and `bundle` budgets are per named bundle, which hashed chunk names
|
|
57
|
+
* make useless. What is worth writing is `anyScript`, applied to every emitted script: the figure
|
|
58
|
+
* to base it on is the heaviest screen's own code, not the average, or the budget fires on the
|
|
59
|
+
* screen that was already the largest the day it was written.
|
|
60
|
+
*/
|
|
61
|
+
const screenBudgetAdvice = (heaviestOwnRawBytes, headroom = 0.25) => {
|
|
62
|
+
if (heaviestOwnRawBytes <= 0) {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
const warningKb = roundKb(heaviestOwnRawBytes * (1 + headroom));
|
|
66
|
+
const errorKb = roundKb(heaviestOwnRawBytes * (1 + headroom) * 1.5);
|
|
67
|
+
const snippet = [
|
|
68
|
+
'{',
|
|
69
|
+
' "type": "anyScript",',
|
|
70
|
+
` "maximumWarning": "${warningKb}kB",`,
|
|
71
|
+
` "maximumError": "${errorKb}kB"`,
|
|
72
|
+
'}',
|
|
73
|
+
].join('\n');
|
|
74
|
+
return { warningKb, errorKb, currentBytes: heaviestOwnRawBytes, snippet };
|
|
75
|
+
};
|
|
76
|
+
exports.screenBudgetAdvice = screenBudgetAdvice;
|