@bymaksym/loadline 0.1.0 → 1.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/README.md +272 -45
- package/dist/cli/cli/args.js +97 -116
- package/dist/cli/cli/gates.js +80 -14
- package/dist/cli/cli/main.js +275 -31
- package/dist/cli/cli/memory.js +114 -0
- package/dist/cli/cli/read/declared.js +52 -0
- package/dist/cli/cli/read/locate.js +214 -0
- package/dist/cli/cli/{read-build.js → read/read-build.js} +156 -87
- package/dist/cli/cli/read/read-config.js +120 -0
- package/dist/cli/cli/render/html-report.js +111 -0
- package/dist/cli/cli/render/render-agent.js +114 -0
- package/dist/cli/cli/render/render-asked.js +104 -0
- package/dist/cli/cli/render/render-badge.js +140 -0
- package/dist/cli/cli/{render-json.js → render/render-json.js} +26 -10
- package/dist/cli/cli/{render-markdown.js → render/render-markdown.js} +6 -6
- package/dist/cli/cli/{render-pr.js → render/render-pr.js} +37 -15
- package/dist/cli/cli/{render-sarif.js → render/render-sarif.js} +11 -6
- package/dist/cli/cli/{render-text.js → render/render-text.js} +169 -127
- package/dist/cli/cli/render/terminal.js +55 -0
- package/dist/cli/cli/report.js +76 -29
- package/dist/cli/cli/self-check.js +19 -23
- package/dist/cli/cli/since-last.js +45 -0
- package/dist/cli/cli/text/text-errors.js +124 -0
- package/dist/cli/cli/text/text.js +343 -0
- package/dist/cli/cli/text/usage.js +240 -0
- package/dist/cli/src/app/core/analysis/analysis.js +68 -24
- package/dist/cli/src/app/core/analysis/{barrels.js → graph/barrels.js} +10 -3
- package/dist/cli/src/app/core/analysis/{blast.js → graph/blast.js} +56 -17
- package/dist/cli/src/app/core/analysis/{cycles.js → graph/cycles.js} +1 -1
- package/dist/cli/src/app/core/analysis/{importers.js → graph/importers.js} +1 -1
- package/dist/cli/src/app/core/analysis/{insights.js → graph/insights.js} +10 -4
- package/dist/cli/src/app/core/analysis/invariant.js +1 -1
- package/dist/cli/src/app/core/analysis/metafile.types.js +21 -7
- package/dist/cli/src/app/core/analysis/{delivery.js → screens/delivery.js} +1 -1
- package/dist/cli/src/app/core/analysis/{entries.js → screens/entries.js} +87 -11
- package/dist/cli/src/app/core/analysis/screens/marks.js +32 -0
- package/dist/cli/src/app/core/analysis/screens/route-files.js +116 -0
- package/dist/cli/src/app/core/analysis/{sourcemap.js → sourcemap/sourcemap.js} +106 -7
- package/dist/cli/src/app/core/analysis/{split-drift.js → sourcemap/split-drift.js} +13 -7
- package/dist/cli/src/app/core/analysis/tool.js +83 -0
- package/dist/cli/src/app/core/assets/assets.js +137 -16
- package/dist/cli/src/app/core/baseline/baseline.js +0 -0
- package/dist/cli/src/app/core/{intake → build-text}/index-html.js +234 -29
- package/dist/cli/src/app/core/build-text/route-table.js +178 -0
- package/dist/cli/src/app/core/build-text/tool-marks.js +54 -0
- package/dist/cli/src/app/core/caching/caching.js +123 -45
- package/dist/cli/src/app/core/caching/from-analysis.js +20 -4
- package/dist/cli/src/app/core/config/config-text.js +90 -0
- package/dist/cli/src/app/core/config/config-values.js +358 -0
- package/dist/cli/src/app/core/config/loadline-config.js +81 -16
- package/dist/cli/src/app/core/config/merge-config.js +98 -0
- package/dist/cli/src/app/core/deps/deps.js +141 -23
- package/dist/cli/src/app/core/deps/pins.js +63 -0
- package/dist/cli/src/app/core/export/summary.js +54 -12
- package/dist/cli/src/app/core/findings/actions.js +65 -6
- package/dist/cli/src/app/core/findings/bundle/boot-single.js +76 -0
- package/dist/cli/src/app/core/findings/bundle/dupes.js +68 -0
- package/dist/cli/src/app/core/findings/{graph.js → bundle/graph.js} +32 -20
- package/dist/cli/src/app/core/findings/{own-in-boot.js → bundle/own-in-boot.js} +9 -5
- package/dist/cli/src/app/core/findings/{requests.js → bundle/requests.js} +3 -3
- package/dist/cli/src/app/core/findings/{shipped.js → bundle/shipped.js} +30 -18
- package/dist/cli/src/app/core/findings/compose.js +6 -1
- package/dist/cli/src/app/core/findings/effort.js +4 -0
- package/dist/cli/src/app/core/findings/finding.types.js +2 -0
- package/dist/cli/src/app/core/findings/findings.js +68 -55
- package/dist/cli/src/app/core/findings/{assets.js → folder/assets.js} +65 -16
- package/dist/cli/src/app/core/findings/{caching.js → folder/caching.js} +17 -10
- package/dist/cli/src/app/core/findings/{page.js → folder/page.js} +6 -2
- package/dist/cli/src/app/core/findings/{scan.js → folder/scan.js} +37 -13
- package/dist/cli/src/app/core/findings/kind-names.js +163 -0
- package/dist/cli/src/app/core/findings/{deps.js → supplied/deps.js} +13 -8
- package/dist/cli/src/app/core/findings/supplied/forbidden.js +71 -0
- package/dist/cli/src/app/core/findings/{situation.js → supplied/situation.js} +2 -2
- package/dist/cli/src/app/core/findings/text/causes.js +39 -0
- package/dist/cli/src/app/core/findings/text/finding-html.js +33 -0
- package/dist/cli/src/app/core/findings/{finding-text.js → text/finding-text.js} +307 -103
- package/dist/cli/src/app/core/findings/text/tool-advice.js +97 -0
- package/dist/cli/src/app/core/format/format.utils.js +172 -18
- package/dist/cli/src/app/core/format/ownership.js +93 -0
- package/dist/cli/src/app/core/i18n/en.js +180 -61
- package/dist/cli/src/app/core/i18n/es.js +192 -68
- package/dist/cli/src/app/core/intake/bundle-graph.js +285 -46
- package/dist/cli/src/app/core/intake/dist-files.js +87 -16
- package/dist/cli/src/app/core/intake/embedded.js +56 -0
- package/dist/cli/src/app/core/intake/unreached.js +139 -0
- package/dist/cli/src/app/core/intake/webpack-modules.js +185 -0
- package/dist/cli/src/app/core/intake/webpack-stats.js +298 -0
- package/dist/cli/src/app/core/project/budget-advice.js +14 -10
- package/dist/cli/src/app/core/project/project-context.js +89 -19
- package/dist/cli/src/app/core/project/project-name.js +36 -0
- package/dist/cli/src/app/core/scan/scan.js +116 -39
- package/dist/cli/src/app/core/scan/secrets.js +0 -0
- package/dist/cli/src/app/core/situation/situation-text.js +23 -0
- package/dist/cli/src/app/core/situation/situation.js +9 -8
- package/dist/cli/src/app/core/timing/timing.js +5 -2
- package/dist/cli/src/app/core/whatif/defer.js +29 -4
- package/dist/cli/src/app/core/whatif/source-importers.js +58 -0
- package/dist/cli/src/app/core/whatif/why.js +102 -0
- package/dist/cli/src/app/state/report-messages.utils.js +1 -4
- package/docs/CONFIG-REFERENCE.md +230 -0
- package/docs/CONFIG.md +511 -0
- package/docs/HOW-IT-WORKS.md +286 -25
- package/loadline.html +26 -24
- package/loadline.schema.json +1209 -0
- package/package.json +71 -35
- package/dist/cli/cli/text.js +0 -180
- package/dist/cli/src/app/core/findings/finding-html.js +0 -13
- /package/dist/cli/cli/{read-build.types.js → read/read-build.types.js} +0 -0
- /package/dist/cli/cli/{render-json.types.js → render/render-json.types.js} +0 -0
- /package/dist/cli/cli/{text.types.js → text/text.types.js} +0 -0
- /package/dist/cli/src/app/core/analysis/{exclusive.js → graph/exclusive.js} +0 -0
- /package/dist/cli/src/app/core/analysis/{insights.types.js → graph/insights.types.js} +0 -0
- /package/dist/cli/src/app/core/analysis/{module-graph.js → graph/module-graph.js} +0 -0
- /package/dist/cli/src/app/core/analysis/{sourcemap.types.js → sourcemap/sourcemap.types.js} +0 -0
- /package/dist/cli/src/app/core/findings/{finding-plain.js → text/finding-plain.js} +0 -0
|
@@ -9,7 +9,8 @@
|
|
|
9
9
|
* in, and whether a modern version of it is sitting in the same folder.
|
|
10
10
|
*/
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.readAssets = exports.kindOf = exports.extensionOf = void 0;
|
|
12
|
+
exports.routeStylesOf = exports.readAssets = exports.firstTripIn = exports.withLateBoot = exports.SERVER_FILE = exports.kindOf = exports.extensionOf = void 0;
|
|
13
|
+
const index_html_1 = require("../build-text/index-html");
|
|
13
14
|
const json_utils_1 = require("../json/json.utils");
|
|
14
15
|
const EXTENSIONS = {
|
|
15
16
|
js: 'script',
|
|
@@ -48,6 +49,15 @@ const extensionOf = (name) => (/\.([\da-z]+)$/i.exec(name)?.[1] ?? '').toLowerCa
|
|
|
48
49
|
exports.extensionOf = extensionOf;
|
|
49
50
|
const kindOf = (name) => EXTENSIONS[(0, exports.extensionOf)(name)] ?? 'other';
|
|
50
51
|
exports.kindOf = kindOf;
|
|
52
|
+
/**
|
|
53
|
+
* Files whose name is fixed by whoever reads them: the host (`_redirects`, `_headers`,
|
|
54
|
+
* `_routes.json`, `.htaccess`, `CNAME`), a crawler (`robots.txt`, `sitemap.xml`), the browser
|
|
55
|
+
* looking for a service worker or a manifest at a URL that does not move, and the pages a host
|
|
56
|
+
* serves on its own (`404.html`, `200.html`). Nothing in the build names them and none can carry a
|
|
57
|
+
* hash, so "not referenced" and "no hash" are both true of them and say nothing: three real builds
|
|
58
|
+
* listed them under both.
|
|
59
|
+
*/
|
|
60
|
+
exports.SERVER_FILE = /^(?:_redirects|_headers|_routes\.json|\.htaccess|CNAME|robots\.txt|sitemap[\w-]*\.xml|[\w-]*\.webmanifest|sw\.js|service-worker\.js|(?:200|404|50\d)\.html)$/i;
|
|
51
61
|
/** The three of the eight kinds the media table holds. The only place they are listed. */
|
|
52
62
|
const MEDIA_KINDS = new Set(['image', 'video', 'audio']);
|
|
53
63
|
/**
|
|
@@ -118,6 +128,7 @@ const fontsOf = (files, preloaded) => {
|
|
|
118
128
|
.toSorted((a, b) => b.bytes - a.bytes);
|
|
119
129
|
};
|
|
120
130
|
/** Pictures and video, with whether a modern version of the same name is already there. */
|
|
131
|
+
/** @param inPage the paths of the files the page asks for. */
|
|
121
132
|
const mediaOf = (files, inPage) => {
|
|
122
133
|
const media = files.filter(file => mediaKindOf(file.name) !== null);
|
|
123
134
|
// Two files are the same picture when everything but the extension matches. The hash a bundler
|
|
@@ -137,7 +148,7 @@ const mediaOf = (files, inPage) => {
|
|
|
137
148
|
// Never the fallback: `media` is exactly the files whose kind is one of the three.
|
|
138
149
|
kind: mediaKindOf(file.name) ?? 'image',
|
|
139
150
|
format,
|
|
140
|
-
inPage: inPage.has(file.
|
|
151
|
+
inPage: inPage.has(file.path),
|
|
141
152
|
modernNeighbour: modern ?? null,
|
|
142
153
|
};
|
|
143
154
|
})
|
|
@@ -209,6 +220,12 @@ const inlinedOf = (texts) => {
|
|
|
209
220
|
}
|
|
210
221
|
return rows.toSorted((a, b) => b.bytes - a.bytes);
|
|
211
222
|
};
|
|
223
|
+
/**
|
|
224
|
+
* What is downloaded once and kept, and can be kept twice. A stylesheet too: Sapper wrote the same
|
|
225
|
+
* 204 bytes as `main.css` and `chunk.css`, and both were counted. Not a script, whose name carries
|
|
226
|
+
* the hash of its content, so two of them with one content have one name.
|
|
227
|
+
*/
|
|
228
|
+
const DUPLICATE_KINDS = new Set(['font', 'image', 'video', 'audio', 'style']);
|
|
212
229
|
/** Files with the same content under two names, when something actually compared the content. */
|
|
213
230
|
const duplicatesOf = (files, hashes) => {
|
|
214
231
|
if (!hashes || hashes.size === 0) {
|
|
@@ -216,6 +233,14 @@ const duplicatesOf = (files, hashes) => {
|
|
|
216
233
|
}
|
|
217
234
|
const byHash = new Map();
|
|
218
235
|
for (const file of files) {
|
|
236
|
+
// What is downloaded once and kept: a font, a picture, a recording. An HTML document and a
|
|
237
|
+
// data file are one per navigation, and two of them being equal says nothing about waste —
|
|
238
|
+
// Nuxt's 200.html, 404.html and every route's shell are the same page on purpose, and two
|
|
239
|
+
// routes' `_payload.json` came out "identical" because they held the same timestamp.
|
|
240
|
+
// Nor is an empty file: Ember writes two empty stylesheets, and "0 B twice" is no copy.
|
|
241
|
+
if (!DUPLICATE_KINDS.has((0, exports.kindOf)(file.name)) || file.bytes === 0) {
|
|
242
|
+
continue;
|
|
243
|
+
}
|
|
219
244
|
const hash = hashes.get(file.path);
|
|
220
245
|
if (hash) {
|
|
221
246
|
byHash.set(hash, [...(byHash.get(hash) ?? []), file]);
|
|
@@ -230,28 +255,94 @@ const duplicatesOf = (files, hashes) => {
|
|
|
230
255
|
names: group.map(file => file.path),
|
|
231
256
|
bytes: group[0]?.bytes ?? 0,
|
|
232
257
|
wasted: (group.length - 1) * (group[0]?.bytes ?? 0),
|
|
258
|
+
named: null,
|
|
233
259
|
}))
|
|
234
260
|
.toSorted((a, b) => b.wasted - a.wasted);
|
|
235
261
|
};
|
|
262
|
+
/** How many files of the folder carry each name. */
|
|
263
|
+
const namesakesOf = (files) => {
|
|
264
|
+
const counts = new Map();
|
|
265
|
+
for (const file of files) {
|
|
266
|
+
counts.set(file.name, (counts.get(file.name) ?? 0) + 1);
|
|
267
|
+
}
|
|
268
|
+
return counts;
|
|
269
|
+
};
|
|
270
|
+
/** Whether a path the page wrote is this file: the same path, or one under a `base` or a subfolder. */
|
|
271
|
+
const isPathOf = (file, path) => file.path === path || file.path.endsWith(`/${path}`) || path.endsWith(`/${file.path}`);
|
|
272
|
+
/**
|
|
273
|
+
* The files the page asks for. By name, which is how everything else here keys a file — except a
|
|
274
|
+
* name several files share, where only the path the page wrote says which one it is. By name,
|
|
275
|
+
* PocketBase's page asked for all 25 `plugin.min.js` of TinyMCE.
|
|
276
|
+
*/
|
|
277
|
+
const askedFiles = (input) => {
|
|
278
|
+
const namesakes = namesakesOf(input.files);
|
|
279
|
+
const shared = input.files.filter(file => input.inPage.has(file.name) && (namesakes.get(file.name) ?? 0) > 1);
|
|
280
|
+
const paths = shared.length > 0 && input.html ? (0, index_html_1.pagePathsIn)(input.html) : [];
|
|
281
|
+
// One file per path, the exact one first and then the shortest: a folder holding `browser/`
|
|
282
|
+
// and `server/` has two `main.js`, and the page names one of them.
|
|
283
|
+
const picked = new Set(paths.flatMap(path => shared
|
|
284
|
+
.filter(file => isPathOf(file, path))
|
|
285
|
+
.toSorted((a, b) => Number(b.path === path) - Number(a.path === path) || a.path.length - b.path.length)
|
|
286
|
+
.slice(0, 1)));
|
|
287
|
+
return input.files.filter(file => input.inPage.has(file.name) && ((namesakes.get(file.name) ?? 0) < 2 || picked.has(file)));
|
|
288
|
+
};
|
|
289
|
+
/**
|
|
290
|
+
* What a file weighs in the unit given: by path when the folder was read by path, and by name
|
|
291
|
+
* only when the name is its own — 25 `plugin.min.js` keyed by name all weigh the last one read.
|
|
292
|
+
*/
|
|
293
|
+
const weigherOf = (files, weigh, weighPath) => {
|
|
294
|
+
const namesakes = namesakesOf(files);
|
|
295
|
+
return file => weighPath?.get(file.path) ??
|
|
296
|
+
((namesakes.get(file.name) ?? 0) > 1 ? file.bytes : (weigh?.get(file.name) ?? file.bytes));
|
|
297
|
+
};
|
|
236
298
|
/** The whole first trip: everything the page asks for before anything appears. */
|
|
237
|
-
const firstTripOf = (input,
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
299
|
+
const firstTripOf = (input, weighFile, inline = 0) => {
|
|
300
|
+
// A `prefetch` is named by the page and is still not part of this trip: it is for the next
|
|
301
|
+
// navigation. Counted, PocketBase's warm-up of TinyMCE read "731 kB across 12 files" for a first
|
|
302
|
+
// trip of 392 kB in 5, under a signal of the same report saying prefetches are not counted.
|
|
303
|
+
// And only what this folder holds: a stylesheet from a font host or forty posters from an image
|
|
304
|
+
// CDN are files of somebody else's, and counting them as files of the trip made "55 files" out
|
|
305
|
+
// of 14. Taking the files rather than the names the page wrote is what keeps them out.
|
|
306
|
+
// Nor is the page's icon: the browser asks for it beside the page and paints without it. Counted,
|
|
307
|
+
// the favicon of every build was part of "before anything appears".
|
|
308
|
+
const named = askedFiles(input).filter(file => (!input.prefetched?.has(file.name) || input.preloaded.has(file.name)) && !input.icons?.has(file.name));
|
|
309
|
+
const total = (kinds) => named.filter(file => kinds.includes((0, exports.kindOf)(file.name))).reduce((sum, file) => sum + weighFile(file), 0);
|
|
310
|
+
const scripts = total(['script']) + inline;
|
|
241
311
|
const styles = total(['style']);
|
|
242
312
|
const fonts = named
|
|
243
|
-
.filter(
|
|
244
|
-
.reduce((sum,
|
|
313
|
+
.filter(file => (0, exports.kindOf)(file.name) === 'font' && input.preloaded.has(file.name))
|
|
314
|
+
.reduce((sum, file) => sum + weighFile(file), 0);
|
|
245
315
|
const images = total(['image']);
|
|
246
316
|
return {
|
|
247
317
|
scripts,
|
|
248
318
|
styles,
|
|
249
319
|
fonts,
|
|
250
320
|
images,
|
|
321
|
+
inline,
|
|
251
322
|
total: scripts + styles + fonts + images,
|
|
252
|
-
files: named.filter(
|
|
323
|
+
files: named.filter(file => (0, exports.kindOf)(file.name) !== 'font' || input.preloaded.has(file.name)).length,
|
|
253
324
|
};
|
|
254
325
|
};
|
|
326
|
+
/**
|
|
327
|
+
* The first trip with the bootstrap chunks `index.html` does not name, once the analysis has found
|
|
328
|
+
* them. The folder alone only sees what the page names, and a chunk the browser discovers by
|
|
329
|
+
* parsing is still downloaded before anything appears, one round trip later. Left out, a real build
|
|
330
|
+
* read "the whole first trip is 133 kB" under a 464 kB bootstrap: the 346 kB it left out was the
|
|
331
|
+
* chunk that made the bootstrap take two trips.
|
|
332
|
+
*/
|
|
333
|
+
const withLateBoot = (trip, late) => {
|
|
334
|
+
const bytes = late.reduce((sum, size) => sum + size, 0);
|
|
335
|
+
return { ...trip, scripts: trip.scripts + bytes, total: trip.total + bytes, files: trip.files + late.length };
|
|
336
|
+
};
|
|
337
|
+
exports.withLateBoot = withLateBoot;
|
|
338
|
+
/**
|
|
339
|
+
* The first trip weighed with other figures than the report's: raw bytes when `weigh` is absent.
|
|
340
|
+
* The command reads the folder before it knows which unit it was asked for, and a trip weighed in
|
|
341
|
+
* brotli under `--mode raw` read "Bootstrap 110 kB" over "the whole first trip is 48 kB".
|
|
342
|
+
*/
|
|
343
|
+
/** @param inline what the page's inline scripts weigh, in the unit of `weigh` (`inlineScriptsIn`). */
|
|
344
|
+
const firstTripIn = (input, weigh, weighPath = null, inline = 0) => firstTripOf(input, weigherOf(input.files, weigh, weighPath), inline);
|
|
345
|
+
exports.firstTripIn = firstTripIn;
|
|
255
346
|
const readAssets = (input) => {
|
|
256
347
|
const sizes = new Map(input.files.map(file => [file.name, file.bytes]));
|
|
257
348
|
const sizeOf = (name) => input.weigh?.get(name) ?? sizes.get(name) ?? 0;
|
|
@@ -260,14 +351,22 @@ const readAssets = (input) => {
|
|
|
260
351
|
const inlined = inlinedOf(input.texts);
|
|
261
352
|
// Scripts only. A prefetched font or picture is a different conversation and a much smaller
|
|
262
353
|
// one; what makes this worth a line is a framework fetching whole routes nobody asked for.
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
354
|
+
// Matched by the path the page wrote where it can be, and weighed per path: by name alone,
|
|
355
|
+
// files that share one counted once and weighed the same (see `prefetchPathsIn`).
|
|
356
|
+
const weighFile = weigherOf(input.files, input.weigh, input.weighPath);
|
|
357
|
+
const byPath = input.html
|
|
358
|
+
? (0, index_html_1.prefetchPathsIn)(input.html).flatMap(path => input.files.filter(file => isPathOf(file, path)).slice(0, 1))
|
|
359
|
+
: [];
|
|
360
|
+
const asked = askedFiles(input);
|
|
361
|
+
const prefetched = (byPath.length > 0
|
|
362
|
+
? byPath.map(file => ({ name: file.path, bytes: weighFile(file) }))
|
|
363
|
+
: [...(input.prefetched ?? [])].map(name => ({ name, bytes: sizeOf(name) })))
|
|
364
|
+
.filter(file => (0, exports.kindOf)(file.name) === 'script')
|
|
266
365
|
.toSorted((a, b) => b.bytes - a.bytes);
|
|
267
366
|
return {
|
|
268
367
|
files: input.files.map(file => ({ ...file, kind: (0, exports.kindOf)(file.name) })),
|
|
269
368
|
fonts: fontsOf(input.files, input.preloaded),
|
|
270
|
-
media: mediaOf(input.files,
|
|
369
|
+
media: mediaOf(input.files, new Set(asked.map(file => file.path))),
|
|
271
370
|
prefetched,
|
|
272
371
|
prefetchedBytes: prefetched.reduce((sum, file) => sum + file.bytes, 0),
|
|
273
372
|
// A source map is nobody's reference and never will be: it is named by the chunk it belongs
|
|
@@ -281,16 +380,38 @@ const readAssets = (input) => {
|
|
|
281
380
|
(0, exports.kindOf)(file.name) !== 'map' &&
|
|
282
381
|
!PRE_COMPRESSED.test(file.name) &&
|
|
283
382
|
!input.texts.has(file.name) &&
|
|
284
|
-
|
|
383
|
+
!exports.SERVER_FILE.test(file.name) &&
|
|
384
|
+
!/^index(?:\.[\w-]+)?\.html$/i.test(file.name))
|
|
285
385
|
: [],
|
|
286
386
|
referencesRead,
|
|
287
|
-
|
|
387
|
+
// A favicon and a touch icon holding the same picture are alternatives the browser picks one
|
|
388
|
+
// of, not a file downloaded twice under two names.
|
|
389
|
+
duplicates: duplicatesOf(input.files, input.hashes)
|
|
390
|
+
.filter(entry => entry.names.some(path => !input.icons?.has(path.split('/').at(-1) ?? path)))
|
|
391
|
+
.map(entry => ({
|
|
392
|
+
...entry,
|
|
393
|
+
named: referencesRead
|
|
394
|
+
? entry.names.filter(path => referenced.has(path.split('/').at(-1) ?? path)).length
|
|
395
|
+
: null,
|
|
396
|
+
})),
|
|
288
397
|
contentCompared: !!input.hashes && input.hashes.size > 0,
|
|
289
398
|
inlined,
|
|
290
399
|
inlinedBytes: inlined.reduce((sum, row) => sum + row.bytes, 0),
|
|
291
400
|
stylesByChunk: stylesByChunk(input, sizeOf),
|
|
292
|
-
firstTrip: firstTripOf(input,
|
|
401
|
+
firstTrip: firstTripOf(input, weighFile),
|
|
293
402
|
inPage: [...input.inPage],
|
|
403
|
+
inPagePaths: asked.map(file => file.path),
|
|
294
404
|
};
|
|
295
405
|
};
|
|
296
406
|
exports.readAssets = readAssets;
|
|
407
|
+
/**
|
|
408
|
+
* The stylesheets the code loads and the page does not ask for: the CSS of a lazy route, which
|
|
409
|
+
* Vite writes beside its chunk. Not "named by a lazy chunk": Vite lists every route's CSS in the
|
|
410
|
+
* preload table of whichever chunk imports the route, and that is usually the bootstrap. Left out
|
|
411
|
+
* of every total — the figures are JavaScript — and named, so leaving them out is said.
|
|
412
|
+
*/
|
|
413
|
+
const routeStylesOf = (report) => {
|
|
414
|
+
const asked = new Set(report.inPage);
|
|
415
|
+
return [...new Set(report.stylesByChunk.flatMap(row => row.styles))].filter(style => !asked.has(style));
|
|
416
|
+
};
|
|
417
|
+
exports.routeStylesOf = routeStylesOf;
|
|
Binary file
|
|
@@ -11,9 +11,10 @@
|
|
|
11
11
|
* the page does: the terminal has no DOM, and two readers would drift apart.
|
|
12
12
|
*/
|
|
13
13
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
-
exports.originsIn = exports.stylesIn = exports.assetsIn = exports.announcedIn = exports.scriptsIn = exports.indexHtmlOf = void 0;
|
|
14
|
+
exports.titleIn = exports.pagePathsIn = exports.prefetchPathsIn = exports.originsIn = exports.stylesIn = exports.assetsIn = exports.inlineScriptsIn = exports.announcedIn = exports.scriptsIn = exports.indexHtmlOf = void 0;
|
|
15
|
+
const format_utils_1 = require("../format/format.utils");
|
|
15
16
|
/** Angular writes `index.html`; with server-side rendering the browser one is `index.csr.html`. */
|
|
16
|
-
const INDEX_NAME = /^index(
|
|
17
|
+
const INDEX_NAME = /^index(?:\.[\w-]+)?\.html$/i;
|
|
17
18
|
const TAG = /<(script|link)\b([^>]*)>/gi;
|
|
18
19
|
/** Images written into the page itself: they are asked for as soon as the tag is parsed. */
|
|
19
20
|
const IMG = /<img\b([^>]*)>/gi;
|
|
@@ -25,7 +26,7 @@ const BASE = /<(base)\b([^>]*)>/gi;
|
|
|
25
26
|
* page and calls `start()` with the result. Nothing outside that script names those chunks, so
|
|
26
27
|
* without reading it the build has no entry at all and cannot be read — which is what it did.
|
|
27
28
|
*/
|
|
28
|
-
const INLINE_SCRIPT = /<script\b([^>]*)>([\S
|
|
29
|
+
const INLINE_SCRIPT = /<script\b([^>]*)>([\s\S]*?)<\/script>/gi;
|
|
29
30
|
/** `import("./chunk.js")` as it is written in a page: a literal specifier, never a variable. */
|
|
30
31
|
const IMPORT_CALL = /\bimport\s*\(\s*(["'`])([^"'`]+)\1\s*\)/g;
|
|
31
32
|
const ATTRIBUTE = (name) => new RegExp(String.raw `\b${name}\s*=\s*("([^"]*)"|'([^']*)'|([^\s>]+))`, 'i');
|
|
@@ -45,7 +46,13 @@ const fileNameOf = (url) => {
|
|
|
45
46
|
* of the first load with no explanation. When nothing matches the name, a folder holding exactly
|
|
46
47
|
* one HTML file leaves no room for doubt about which page it is.
|
|
47
48
|
*/
|
|
48
|
-
const indexHtmlOf = (files) => {
|
|
49
|
+
const indexHtmlOf = (files, page) => {
|
|
50
|
+
// `build.page` of `loadline.json`, when the page of the application is not the one this would pick.
|
|
51
|
+
const where = (file) => file.path ?? file.webkitRelativePath ?? file.name;
|
|
52
|
+
const chosen = page ? files.find(file => (0, format_utils_1.namedBy)(page, where(file).replaceAll('\\', '/'))) : undefined;
|
|
53
|
+
if (chosen) {
|
|
54
|
+
return chosen;
|
|
55
|
+
}
|
|
49
56
|
const named = files.find(file => INDEX_NAME.test(file.name));
|
|
50
57
|
if (named) {
|
|
51
58
|
return named;
|
|
@@ -55,22 +62,74 @@ const indexHtmlOf = (files) => {
|
|
|
55
62
|
};
|
|
56
63
|
exports.indexHtmlOf = indexHtmlOf;
|
|
57
64
|
/**
|
|
58
|
-
*
|
|
65
|
+
* Any run of text between two quotes inside an inline script. Most are not file names; the ones that
|
|
66
|
+
* name no script of the folder point at nothing and drop out where the graph is read, the same rule
|
|
67
|
+
* that makes the regular expressions over the chunks safe.
|
|
68
|
+
*
|
|
69
|
+
* ⚠️ The quotes are looked at, never consumed. Matched as pairs, one string holding the other kind
|
|
70
|
+
* of quote — Sapper's `"if(0)import('')"` — put every pair after it out of step, and the very
|
|
71
|
+
* string that named the entry was read as the code between two strings.
|
|
72
|
+
*/
|
|
73
|
+
const INLINE_STRING = /(?<=["'`])([^"'`\s<>]{1,300})(?=["'`])/g;
|
|
74
|
+
/** `navigator.serviceWorker.register(` right before a string: that file is no part of the page. */
|
|
75
|
+
const REGISTERS_WORKER = /serviceWorker\s*\.\s*register\s*\(\s*["'`]$/;
|
|
76
|
+
/**
|
|
77
|
+
* The `catch` of an inline script, up to the first closing brace: the way a page says "and if this
|
|
78
|
+
* browser cannot do the above". Sapper starts its client with `type="module"` in a `try` and falls
|
|
79
|
+
* back to the `shimport` loader in the `catch`; what only the fallback names is the copy for old
|
|
80
|
+
* browsers, the same as a `nomodule` script.
|
|
81
|
+
*/
|
|
82
|
+
const CATCH_BLOCK = /\bcatch(?:\s*\([^)]*\))?\s*\{[^}]*\}/g;
|
|
83
|
+
/**
|
|
84
|
+
* `<script nomodule>`: the copy for browsers without ES modules. A browser that runs the modern one
|
|
85
|
+
* skips it, so it is no part of what the page downloads — and counted, a Stencil build had its
|
|
86
|
+
* bootstrap at 48 kB of which 42 kB was `app.js`, the SystemJS fallback nobody on a current browser
|
|
87
|
+
* fetches. Vite's legacy plugin and Angular's differential loading up to 13 write the same tag.
|
|
88
|
+
*/
|
|
89
|
+
const isNoModule = (attributes) => /\bnomodule\b/i.test(attributes);
|
|
90
|
+
/** A script the browser runs. JSON, an import map or a template is data, whatever names it holds. */
|
|
91
|
+
const isExecutable = (attributes) => {
|
|
92
|
+
const type = (attribute(attributes, 'type') ?? '').toLowerCase();
|
|
93
|
+
return type === '' || type === 'module' || /^(?:text|application)\/(?:java|ecma)script$/.test(type);
|
|
94
|
+
};
|
|
95
|
+
/**
|
|
96
|
+
* The chunks an inline script of the page starts by hand. They are entries in the same sense a
|
|
59
97
|
* `<script src>` is: the browser runs that script as soon as it parses the page, so execution
|
|
60
98
|
* starts there.
|
|
99
|
+
*
|
|
100
|
+
* An `import()` is how SvelteKit does it. Pages older than ES modules do it with a string: Sapper
|
|
101
|
+
* writes `s.src="/client/client.11806644.js"` into a script element it creates, Polymer's AMD
|
|
102
|
+
* builds `define(['src/components/my-app.js'])`, a RequireJS page `require(['app.js'])`. Each of
|
|
103
|
+
* them is a page whose application starts from nothing a tag names, and a build whose page names no
|
|
104
|
+
* entry is refused — which is what Sapper's was, with a message saying it had no `index.html` while
|
|
105
|
+
* the folder held one. So any string naming a script counts; whether that script is a file of this
|
|
106
|
+
* build is decided where the files are known, and a name that is none drops out there.
|
|
61
107
|
*/
|
|
62
108
|
const startedInline = (html) => {
|
|
63
109
|
const started = [];
|
|
110
|
+
const legacy = [];
|
|
111
|
+
const workers = [];
|
|
64
112
|
for (const [, attributes = '', body = ''] of html.matchAll(INLINE_SCRIPT)) {
|
|
65
113
|
// A `<script src>` has no body worth reading, and its file is already counted as the entry.
|
|
66
|
-
if (attribute(attributes, 'src') !== null) {
|
|
114
|
+
if (attribute(attributes, 'src') !== null || !isExecutable(attributes)) {
|
|
67
115
|
continue;
|
|
68
116
|
}
|
|
117
|
+
const into = isNoModule(attributes) ? legacy : started;
|
|
69
118
|
for (const call of body.matchAll(IMPORT_CALL)) {
|
|
70
|
-
|
|
119
|
+
into.push(fileNameOf(call[2] ?? ''));
|
|
120
|
+
}
|
|
121
|
+
const fallbacks = [...body.matchAll(CATCH_BLOCK)].map(block => [block.index, block.index + block[0].length]);
|
|
122
|
+
for (const literal of body.matchAll(INLINE_STRING)) {
|
|
123
|
+
const name = fileNameOf(literal[1] ?? '');
|
|
124
|
+
if (!/\.m?js$/.test(name)) {
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
const registered = REGISTERS_WORKER.test(body.slice(Math.max(0, literal.index - 40), literal.index));
|
|
128
|
+
const fallback = fallbacks.some(([from = 0, to = 0]) => literal.index > from && literal.index < to);
|
|
129
|
+
(registered ? workers : fallback ? legacy : into).push(name);
|
|
71
130
|
}
|
|
72
131
|
}
|
|
73
|
-
return started;
|
|
132
|
+
return { started, legacy, workers };
|
|
74
133
|
};
|
|
75
134
|
/**
|
|
76
135
|
* The two ways a page names a script, kept apart because they mean different things. A `<script>`
|
|
@@ -80,6 +139,7 @@ const startedInline = (html) => {
|
|
|
80
139
|
const scriptsIn = (html) => {
|
|
81
140
|
const names = new Set();
|
|
82
141
|
const entries = new Set();
|
|
142
|
+
const legacy = new Set();
|
|
83
143
|
for (const [, tag = '', rest = ''] of html.matchAll(TAG)) {
|
|
84
144
|
const isScript = tag.toLowerCase() === 'script';
|
|
85
145
|
const rel = new Set((attribute(rest, 'rel') ?? '').toLowerCase().split(/\s+/));
|
|
@@ -88,29 +148,60 @@ const scriptsIn = (html) => {
|
|
|
88
148
|
if (!isScript && !preloads) {
|
|
89
149
|
continue;
|
|
90
150
|
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
}
|
|
151
|
+
// RequireJS starts the application from `data-main`, a module id with the extension left off,
|
|
152
|
+
// and the script the tag itself names is only the loader.
|
|
153
|
+
const main = isScript ? attribute(rest, 'data-main') : null;
|
|
154
|
+
if (main) {
|
|
155
|
+
const module = fileNameOf(main);
|
|
156
|
+
entries.add(/\.m?js$/.test(module) ? module : `${module}.js`);
|
|
157
|
+
}
|
|
158
|
+
// Vite's legacy plugin names its entry in `data-src` and starts it with `System.import`.
|
|
159
|
+
const legacySrc = isScript && isNoModule(rest) ? attribute(rest, 'data-src') : null;
|
|
160
|
+
const url = isScript ? (attribute(rest, 'src') ?? legacySrc) : attribute(rest, 'href');
|
|
161
|
+
const name = fileNameOf(url ?? '');
|
|
162
|
+
if (!/\.m?js$/.test(name)) {
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
if (isScript && isNoModule(rest)) {
|
|
166
|
+
legacy.add(name);
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
names.add(name);
|
|
170
|
+
if (isScript) {
|
|
171
|
+
entries.add(name);
|
|
97
172
|
}
|
|
98
173
|
}
|
|
99
174
|
// After the tags, so the order stays the order the page names things in: the links come first
|
|
100
175
|
// in the document and the script that starts the application is at the end of it.
|
|
101
|
-
|
|
102
|
-
|
|
176
|
+
const inline = startedInline(html);
|
|
177
|
+
for (const name of inline.started) {
|
|
178
|
+
if (!/\.m?js$/.test(name) || legacy.has(name)) {
|
|
103
179
|
continue;
|
|
104
180
|
}
|
|
105
181
|
names.add(name);
|
|
106
182
|
entries.add(name);
|
|
107
183
|
}
|
|
108
|
-
|
|
184
|
+
for (const name of inline.legacy) {
|
|
185
|
+
if (/\.m?js$/.test(name) && !entries.has(name)) {
|
|
186
|
+
legacy.add(name);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
return { names: [...names], entries: [...entries], legacy: [...legacy], workers: inline.workers };
|
|
109
190
|
};
|
|
110
191
|
exports.scriptsIn = scriptsIn;
|
|
111
192
|
/** The JavaScript files the page names, as bare file names, in the order the page names them. */
|
|
112
193
|
const announcedIn = (html) => (0, exports.scriptsIn)(html).names;
|
|
113
194
|
exports.announcedIn = announcedIn;
|
|
195
|
+
/**
|
|
196
|
+
* What the page carries as code of its own: the bodies of its inline `<script>`s. They travel with
|
|
197
|
+
* the page, before anything it names, and Polymer's es6 build had 14 kB of them that no figure
|
|
198
|
+
* counted.
|
|
199
|
+
*/
|
|
200
|
+
const inlineScriptsIn = (html) => [...html.matchAll(INLINE_SCRIPT)]
|
|
201
|
+
.filter(([, attributes = '']) => !/\bsrc\s*=/i.test(attributes))
|
|
202
|
+
.map(match => (match[2] ?? '').trim())
|
|
203
|
+
.join('\n');
|
|
204
|
+
exports.inlineScriptsIn = inlineScriptsIn;
|
|
114
205
|
/**
|
|
115
206
|
* Everything else the page names before anything is painted: the images written into the HTML, and
|
|
116
207
|
* whatever it preloads.
|
|
@@ -125,8 +216,35 @@ exports.announcedIn = announcedIn;
|
|
|
125
216
|
* counting them all would overstate the first load, which is the mistake this whole group exists
|
|
126
217
|
* to stop making in the other direction.
|
|
127
218
|
*/
|
|
219
|
+
/**
|
|
220
|
+
* Icons a page names that the browser does not all download.
|
|
221
|
+
*
|
|
222
|
+
* `<link rel="icon">` with several sizes or types are **alternatives**: the browser picks the one
|
|
223
|
+
* that fits and fetches that one, the way it picks one candidate of a `srcset`. `apple-touch-icon`
|
|
224
|
+
* and `mask-icon` are fetched only when somebody saves the page to a home screen or pins a tab. A
|
|
225
|
+
* page with a `.ico`, an SVG, three PNG sizes and a touch icon was counted as downloading all six,
|
|
226
|
+
* which inflated the first trip, the "no hash, asked for by the page" list and the "same file twice"
|
|
227
|
+
* one with files that never travel together.
|
|
228
|
+
*/
|
|
229
|
+
const ON_DEMAND_ICON = new Set(['apple-touch-icon', 'apple-touch-icon-precomposed', 'mask-icon']);
|
|
230
|
+
/**
|
|
231
|
+
* Links that name a file for somebody else — a crawler, a feed reader, a search box — and that the
|
|
232
|
+
* browser does not fetch on its own: `<link rel="sitemap">` made `sitemap.xml` "asked for by the
|
|
233
|
+
* page" and one more file of the first trip.
|
|
234
|
+
*/
|
|
235
|
+
const NOT_FETCHED = new Set(['sitemap', 'canonical', 'alternate', 'author', 'help', 'license', 'search']);
|
|
236
|
+
/** Which of a page's `rel="icon"` alternatives a current browser fetches: the SVG when there is one. */
|
|
237
|
+
const chosenIcon = (icons) => {
|
|
238
|
+
const svg = icons.find(icon => icon.type.includes('svg') || /\.svg$/i.test(icon.source));
|
|
239
|
+
const any = icons.find(icon => icon.sizes === '' || icon.sizes === 'any');
|
|
240
|
+
return (svg ?? any ?? icons[0])?.source ?? null;
|
|
241
|
+
};
|
|
128
242
|
const assetsIn = (html) => {
|
|
129
243
|
const referenced = new Set();
|
|
244
|
+
/** Every icon the page names, and of those, the ones the browser will not fetch on this load. */
|
|
245
|
+
const icons = new Set();
|
|
246
|
+
const alternates = new Set();
|
|
247
|
+
const favicons = [];
|
|
130
248
|
const preloaded = new Set();
|
|
131
249
|
/**
|
|
132
250
|
* Kept apart from both, because it is neither. A `prefetch` is not part of the first load — the
|
|
@@ -149,8 +267,29 @@ const assetsIn = (html) => {
|
|
|
149
267
|
if (!source) {
|
|
150
268
|
continue;
|
|
151
269
|
}
|
|
152
|
-
referenced.add(source);
|
|
153
270
|
hrefs.add(url);
|
|
271
|
+
// The copy for browsers without ES modules is named and never fetched by one that has them:
|
|
272
|
+
// counted, Vite's legacy polyfills made a 1 kB first load read 35 kB.
|
|
273
|
+
const legacy = lower === 'script' && isNoModule(rest);
|
|
274
|
+
if (legacy || (lower === 'link' && [...rel].some(value => NOT_FETCHED.has(value)))) {
|
|
275
|
+
alternates.add(source);
|
|
276
|
+
continue;
|
|
277
|
+
}
|
|
278
|
+
if (lower === 'link' && [...rel].some(value => ON_DEMAND_ICON.has(value))) {
|
|
279
|
+
icons.add(source);
|
|
280
|
+
alternates.add(source);
|
|
281
|
+
continue;
|
|
282
|
+
}
|
|
283
|
+
if (lower === 'link' && rel.has('icon')) {
|
|
284
|
+
icons.add(source);
|
|
285
|
+
favicons.push({
|
|
286
|
+
source,
|
|
287
|
+
type: (attribute(rest, 'type') ?? '').toLowerCase(),
|
|
288
|
+
sizes: (attribute(rest, 'sizes') ?? '').toLowerCase().trim(),
|
|
289
|
+
});
|
|
290
|
+
continue;
|
|
291
|
+
}
|
|
292
|
+
referenced.add(source);
|
|
154
293
|
// `prefetch` is deliberately not a preload: it is what the browser fetches when it is idle,
|
|
155
294
|
// which is the opposite of "before anything appears".
|
|
156
295
|
if (lower === 'link' && preloads && !rel.has('prefetch')) {
|
|
@@ -165,16 +304,33 @@ const assetsIn = (html) => {
|
|
|
165
304
|
for (const [, rest = ''] of html.matchAll(IMG)) {
|
|
166
305
|
const url = attribute(rest, 'src') ?? '';
|
|
167
306
|
const source = fileNameOf(url);
|
|
168
|
-
if (source) {
|
|
169
|
-
|
|
170
|
-
|
|
307
|
+
if (!source) {
|
|
308
|
+
continue;
|
|
309
|
+
}
|
|
310
|
+
referenced.add(source);
|
|
311
|
+
hrefs.add(url);
|
|
312
|
+
}
|
|
313
|
+
// One favicon is fetched; the rest are alternatives. A file that is also named some other way —
|
|
314
|
+
// an `<img>` of the same logo — stays referenced through that other tag.
|
|
315
|
+
const fetched = chosenIcon(favicons);
|
|
316
|
+
for (const icon of favicons) {
|
|
317
|
+
if (icon.source === fetched) {
|
|
318
|
+
referenced.add(icon.source);
|
|
171
319
|
}
|
|
320
|
+
else {
|
|
321
|
+
alternates.add(icon.source);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
for (const name of referenced) {
|
|
325
|
+
alternates.delete(name);
|
|
172
326
|
}
|
|
173
327
|
return {
|
|
174
328
|
referenced: [...referenced],
|
|
175
329
|
preloaded: [...preloaded],
|
|
176
330
|
prefetched: [...prefetched],
|
|
177
331
|
hrefs: [...hrefs],
|
|
332
|
+
icons: [...icons],
|
|
333
|
+
alternates: [...alternates],
|
|
178
334
|
};
|
|
179
335
|
};
|
|
180
336
|
exports.assetsIn = assetsIn;
|
|
@@ -218,13 +374,10 @@ exports.stylesIn = stylesIn;
|
|
|
218
374
|
* A protocol-relative `//cdn.example.com/main.js` counts: it is a different host, which is the
|
|
219
375
|
* whole of what matters here, and the scheme it inherits changes nothing about the handshake.
|
|
220
376
|
*/
|
|
221
|
-
const ABSOLUTE = /^(
|
|
377
|
+
const ABSOLUTE = /^(https?:)?\/\/([^/?#]+)/i;
|
|
222
378
|
const originOf = (url) => {
|
|
223
379
|
const found = ABSOLUTE.exec(url.trim());
|
|
224
|
-
|
|
225
|
-
return null;
|
|
226
|
-
}
|
|
227
|
-
return `${(found[1] ?? '').toLowerCase()}//${(found[2] ?? '').toLowerCase()}`;
|
|
380
|
+
return found ? `${(found[1] ?? '').toLowerCase()}//${(found[2] ?? '').toLowerCase()}` : null;
|
|
228
381
|
};
|
|
229
382
|
/**
|
|
230
383
|
* Where the page fetches from, when that is not itself.
|
|
@@ -246,14 +399,15 @@ const originsIn = (html) => {
|
|
|
246
399
|
const byOrigin = new Map();
|
|
247
400
|
const hinted = new Set();
|
|
248
401
|
let base = null;
|
|
249
|
-
const count = (url, isScript) => {
|
|
402
|
+
const count = (url, isScript, isStyle = false) => {
|
|
250
403
|
const origin = originOf(url);
|
|
251
404
|
if (!origin) {
|
|
252
405
|
return;
|
|
253
406
|
}
|
|
254
|
-
const row = byOrigin.get(origin) ?? { origin, files: 0, scripts: 0 };
|
|
407
|
+
const row = byOrigin.get(origin) ?? { origin, files: 0, scripts: 0, styles: 0 };
|
|
255
408
|
row.files += 1;
|
|
256
409
|
row.scripts += isScript ? 1 : 0;
|
|
410
|
+
row.styles = (row.styles ?? 0) + (isStyle ? 1 : 0);
|
|
257
411
|
byOrigin.set(origin, row);
|
|
258
412
|
};
|
|
259
413
|
for (const [, tag = '', rest = ''] of html.matchAll(TAG)) {
|
|
@@ -271,7 +425,9 @@ const originsIn = (html) => {
|
|
|
271
425
|
if (!url) {
|
|
272
426
|
continue;
|
|
273
427
|
}
|
|
274
|
-
count(url, lower === 'script' || rel.has('modulepreload') || attribute(rest, 'as') === 'script'
|
|
428
|
+
count(url, lower === 'script' || rel.has('modulepreload') || attribute(rest, 'as') === 'script',
|
|
429
|
+
// `media="print"` is the trick for loading one without blocking: see `stylesIn`.
|
|
430
|
+
lower === 'link' && rel.has('stylesheet') && (attribute(rest, 'media') ?? '').toLowerCase() !== 'print');
|
|
275
431
|
}
|
|
276
432
|
for (const [, rest = ''] of html.matchAll(IMG)) {
|
|
277
433
|
count(attribute(rest, 'src') ?? '', false);
|
|
@@ -290,3 +446,52 @@ const originsIn = (html) => {
|
|
|
290
446
|
};
|
|
291
447
|
};
|
|
292
448
|
exports.originsIn = originsIn;
|
|
449
|
+
/**
|
|
450
|
+
* Where the page's `<link rel="prefetch">` point, as paths inside the build: query and hash off,
|
|
451
|
+
* `./` and `/` off, and nothing on another host. The file name alone is not enough to find them —
|
|
452
|
+
* TinyMCE's 25 plugins are all `plugin.min.js`, and read by name sixteen prefetched files came out
|
|
453
|
+
* as five, every one of them weighing the same.
|
|
454
|
+
*/
|
|
455
|
+
const prefetchPathsIn = (html) => pathsIn(html, true);
|
|
456
|
+
exports.prefetchPathsIn = prefetchPathsIn;
|
|
457
|
+
/**
|
|
458
|
+
* Where every script, stylesheet, preload and `<img>` of the page points, as paths inside the
|
|
459
|
+
* build, the same way. What tells the one `plugin.min.js` the page names from the 24 it does not.
|
|
460
|
+
*/
|
|
461
|
+
const pagePathsIn = (html) => pathsIn(html, false);
|
|
462
|
+
exports.pagePathsIn = pagePathsIn;
|
|
463
|
+
const pathsIn = (html, prefetchOnly) => {
|
|
464
|
+
const urls = [];
|
|
465
|
+
for (const [, tag = '', rest = ''] of html.matchAll(TAG)) {
|
|
466
|
+
const link = tag.toLowerCase() === 'link';
|
|
467
|
+
const rel = new Set((attribute(rest, 'rel') ?? '').toLowerCase().split(/\s+/));
|
|
468
|
+
if (prefetchOnly ? link && rel.has('prefetch') : !link || !rel.has('prefetch')) {
|
|
469
|
+
urls.push(attribute(rest, link ? 'href' : 'src') ?? '');
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
if (!prefetchOnly) {
|
|
473
|
+
for (const [, rest = ''] of html.matchAll(IMG)) {
|
|
474
|
+
urls.push(attribute(rest, 'src') ?? '');
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
const paths = new Set();
|
|
478
|
+
for (const url of urls) {
|
|
479
|
+
const path = /^(?:[a-z]+:)?\/\//i.test(url)
|
|
480
|
+
? ''
|
|
481
|
+
: (/^[^?#]*/.exec(url)?.[0] ?? '').replace(/^(?:\.\/|\/)+/, '');
|
|
482
|
+
if (path) {
|
|
483
|
+
paths.add(path);
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
return [...paths];
|
|
487
|
+
};
|
|
488
|
+
/** The `<title>` of the page: what the application calls itself, when nothing else names it. */
|
|
489
|
+
const TITLE = /<title[^>]*>([^<]*)<\/title>/i;
|
|
490
|
+
/** The few entities a title is written with. Anything else stays as it is: it is a label. */
|
|
491
|
+
const ENTITIES = { '&': '&', '<': '<', '>': '>', '"': '"', ''': "'" };
|
|
492
|
+
const titleIn = (html) => {
|
|
493
|
+
const title = (TITLE.exec(html)?.[1] ?? '').replaceAll(/&(?:amp|lt|gt|quot|#39);/g, entity => ENTITIES[entity] ?? entity);
|
|
494
|
+
const trimmed = title.replaceAll(/\s+/g, ' ').trim();
|
|
495
|
+
return trimmed === '' ? null : trimmed;
|
|
496
|
+
};
|
|
497
|
+
exports.titleIn = titleIn;
|