@bymaksym/loadline 1.0.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.
Files changed (112) hide show
  1. package/README.md +211 -43
  2. package/dist/cli/cli/args.js +79 -128
  3. package/dist/cli/cli/gates.js +80 -14
  4. package/dist/cli/cli/main.js +164 -33
  5. package/dist/cli/cli/memory.js +40 -4
  6. package/dist/cli/cli/{declared.js → read/declared.js} +1 -1
  7. package/dist/cli/cli/read/locate.js +214 -0
  8. package/dist/cli/cli/{read-build.js → read/read-build.js} +149 -84
  9. package/dist/cli/cli/read/read-config.js +120 -0
  10. package/dist/cli/cli/{html-report.js → render/html-report.js} +14 -10
  11. package/dist/cli/cli/render/render-agent.js +114 -0
  12. package/dist/cli/cli/render/render-asked.js +104 -0
  13. package/dist/cli/cli/render/render-badge.js +140 -0
  14. package/dist/cli/cli/{render-json.js → render/render-json.js} +18 -5
  15. package/dist/cli/cli/{render-markdown.js → render/render-markdown.js} +5 -5
  16. package/dist/cli/cli/{render-pr.js → render/render-pr.js} +33 -13
  17. package/dist/cli/cli/{render-sarif.js → render/render-sarif.js} +8 -3
  18. package/dist/cli/cli/{render-text.js → render/render-text.js} +118 -111
  19. package/dist/cli/cli/render/terminal.js +55 -0
  20. package/dist/cli/cli/report.js +68 -27
  21. package/dist/cli/cli/self-check.js +19 -23
  22. package/dist/cli/cli/since-last.js +2 -2
  23. package/dist/cli/cli/text/text-errors.js +124 -0
  24. package/dist/cli/cli/text/text.js +343 -0
  25. package/dist/cli/cli/text/usage.js +240 -0
  26. package/dist/cli/src/app/core/analysis/analysis.js +63 -23
  27. package/dist/cli/src/app/core/analysis/{barrels.js → graph/barrels.js} +1 -1
  28. package/dist/cli/src/app/core/analysis/{blast.js → graph/blast.js} +56 -17
  29. package/dist/cli/src/app/core/analysis/{cycles.js → graph/cycles.js} +1 -1
  30. package/dist/cli/src/app/core/analysis/{importers.js → graph/importers.js} +1 -1
  31. package/dist/cli/src/app/core/analysis/{insights.js → graph/insights.js} +10 -4
  32. package/dist/cli/src/app/core/analysis/invariant.js +1 -1
  33. package/dist/cli/src/app/core/analysis/metafile.types.js +7 -1
  34. package/dist/cli/src/app/core/analysis/{delivery.js → screens/delivery.js} +1 -1
  35. package/dist/cli/src/app/core/analysis/{entries.js → screens/entries.js} +50 -9
  36. package/dist/cli/src/app/core/analysis/screens/marks.js +32 -0
  37. package/dist/cli/src/app/core/analysis/screens/route-files.js +116 -0
  38. package/dist/cli/src/app/core/analysis/{sourcemap.js → sourcemap/sourcemap.js} +106 -7
  39. package/dist/cli/src/app/core/analysis/{split-drift.js → sourcemap/split-drift.js} +1 -1
  40. package/dist/cli/src/app/core/analysis/tool.js +83 -0
  41. package/dist/cli/src/app/core/assets/assets.js +134 -15
  42. package/dist/cli/src/app/core/baseline/baseline.js +0 -0
  43. package/dist/cli/src/app/core/{intake → build-text}/index-html.js +168 -13
  44. package/dist/cli/src/app/core/build-text/route-table.js +178 -0
  45. package/dist/cli/src/app/core/build-text/tool-marks.js +54 -0
  46. package/dist/cli/src/app/core/caching/caching.js +121 -43
  47. package/dist/cli/src/app/core/caching/from-analysis.js +20 -4
  48. package/dist/cli/src/app/core/config/config-text.js +90 -0
  49. package/dist/cli/src/app/core/config/config-values.js +358 -0
  50. package/dist/cli/src/app/core/config/loadline-config.js +81 -16
  51. package/dist/cli/src/app/core/config/merge-config.js +98 -0
  52. package/dist/cli/src/app/core/deps/deps.js +104 -16
  53. package/dist/cli/src/app/core/export/summary.js +29 -5
  54. package/dist/cli/src/app/core/findings/actions.js +3 -1
  55. package/dist/cli/src/app/core/findings/{boot-single.js → bundle/boot-single.js} +3 -3
  56. package/dist/cli/src/app/core/findings/{dupes.js → bundle/dupes.js} +8 -5
  57. package/dist/cli/src/app/core/findings/{graph.js → bundle/graph.js} +30 -18
  58. package/dist/cli/src/app/core/findings/{own-in-boot.js → bundle/own-in-boot.js} +9 -5
  59. package/dist/cli/src/app/core/findings/{requests.js → bundle/requests.js} +3 -3
  60. package/dist/cli/src/app/core/findings/{shipped.js → bundle/shipped.js} +28 -14
  61. package/dist/cli/src/app/core/findings/compose.js +6 -1
  62. package/dist/cli/src/app/core/findings/effort.js +2 -0
  63. package/dist/cli/src/app/core/findings/finding.types.js +1 -0
  64. package/dist/cli/src/app/core/findings/findings.js +48 -19
  65. package/dist/cli/src/app/core/findings/{assets.js → folder/assets.js} +38 -14
  66. package/dist/cli/src/app/core/findings/{caching.js → folder/caching.js} +16 -9
  67. package/dist/cli/src/app/core/findings/{page.js → folder/page.js} +6 -2
  68. package/dist/cli/src/app/core/findings/{scan.js → folder/scan.js} +25 -11
  69. package/dist/cli/src/app/core/findings/kind-names.js +163 -0
  70. package/dist/cli/src/app/core/findings/{deps.js → supplied/deps.js} +13 -8
  71. package/dist/cli/src/app/core/findings/supplied/forbidden.js +71 -0
  72. package/dist/cli/src/app/core/findings/{situation.js → supplied/situation.js} +2 -2
  73. package/dist/cli/src/app/core/findings/text/causes.js +39 -0
  74. package/dist/cli/src/app/core/findings/{finding-html.js → text/finding-html.js} +8 -1
  75. package/dist/cli/src/app/core/findings/{finding-text.js → text/finding-text.js} +208 -77
  76. package/dist/cli/src/app/core/findings/text/tool-advice.js +97 -0
  77. package/dist/cli/src/app/core/format/format.utils.js +158 -11
  78. package/dist/cli/src/app/core/format/ownership.js +93 -0
  79. package/dist/cli/src/app/core/i18n/en.js +76 -17
  80. package/dist/cli/src/app/core/i18n/es.js +82 -22
  81. package/dist/cli/src/app/core/intake/bundle-graph.js +281 -43
  82. package/dist/cli/src/app/core/intake/dist-files.js +82 -14
  83. package/dist/cli/src/app/core/intake/embedded.js +11 -1
  84. package/dist/cli/src/app/core/intake/unreached.js +139 -0
  85. package/dist/cli/src/app/core/intake/webpack-modules.js +185 -0
  86. package/dist/cli/src/app/core/intake/webpack-stats.js +298 -0
  87. package/dist/cli/src/app/core/project/project-context.js +16 -4
  88. package/dist/cli/src/app/core/project/project-name.js +36 -0
  89. package/dist/cli/src/app/core/scan/scan.js +58 -19
  90. package/dist/cli/src/app/core/scan/secrets.js +23 -3
  91. package/dist/cli/src/app/core/situation/situation-text.js +23 -0
  92. package/dist/cli/src/app/core/situation/situation.js +6 -4
  93. package/dist/cli/src/app/core/timing/timing.js +5 -2
  94. package/dist/cli/src/app/core/whatif/defer.js +29 -4
  95. package/dist/cli/src/app/core/whatif/source-importers.js +58 -0
  96. package/dist/cli/src/app/core/whatif/why.js +102 -0
  97. package/docs/CONFIG-REFERENCE.md +230 -0
  98. package/docs/CONFIG.md +511 -0
  99. package/docs/HOW-IT-WORKS.md +286 -25
  100. package/loadline.html +24 -20
  101. package/loadline.schema.json +1209 -0
  102. package/package.json +35 -29
  103. package/dist/cli/cli/locate.js +0 -60
  104. package/dist/cli/cli/text.js +0 -215
  105. /package/dist/cli/cli/{read-build.types.js → read/read-build.types.js} +0 -0
  106. /package/dist/cli/cli/{render-json.types.js → render/render-json.types.js} +0 -0
  107. /package/dist/cli/cli/{text.types.js → text/text.types.js} +0 -0
  108. /package/dist/cli/src/app/core/analysis/{exclusive.js → graph/exclusive.js} +0 -0
  109. /package/dist/cli/src/app/core/analysis/{insights.types.js → graph/insights.types.js} +0 -0
  110. /package/dist/cli/src/app/core/analysis/{module-graph.js → graph/module-graph.js} +0 -0
  111. /package/dist/cli/src/app/core/analysis/{sourcemap.types.js → sourcemap/sourcemap.types.js} +0 -0
  112. /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.name),
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, sizeOf) => {
238
- const named = [...input.inPage];
239
- const total = (kinds) => named.filter(name => kinds.includes((0, exports.kindOf)(name))).reduce((sum, name) => sum + sizeOf(name), 0);
240
- const scripts = total(['script']);
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(name => (0, exports.kindOf)(name) === 'font' && input.preloaded.has(name))
244
- .reduce((sum, name) => sum + sizeOf(name), 0);
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(name => (0, exports.kindOf)(name) !== 'font' || input.preloaded.has(name)).length,
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
- const prefetched = [...(input.prefetched ?? [])]
264
- .filter(name => (0, exports.kindOf)(name) === 'script')
265
- .map(name => ({ name, bytes: sizeOf(name) }))
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, input.inPage),
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,18 +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) &&
383
+ !exports.SERVER_FILE.test(file.name) &&
284
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
288
388
  // of, not a file downloaded twice under two names.
289
- duplicates: duplicatesOf(input.files, input.hashes).filter(entry => entry.names.some(path => !input.icons?.has(path.split('/').at(-1) ?? path))),
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
+ })),
290
397
  contentCompared: !!input.hashes && input.hashes.size > 0,
291
398
  inlined,
292
399
  inlinedBytes: inlined.reduce((sum, row) => sum + row.bytes, 0),
293
400
  stylesByChunk: stylesByChunk(input, sizeOf),
294
- firstTrip: firstTripOf(input, sizeOf),
401
+ firstTrip: firstTripOf(input, weighFile),
295
402
  inPage: [...input.inPage],
403
+ inPagePaths: asked.map(file => file.path),
296
404
  };
297
405
  };
298
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;
@@ -11,7 +11,8 @@
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
17
  const INDEX_NAME = /^index(?:\.[\w-]+)?\.html$/i;
17
18
  const TAG = /<(script|link)\b([^>]*)>/gi;
@@ -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
- * The chunks an inline script of the page imports by hand. They are entries in the same sense a
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
- started.push(fileNameOf(call[2] ?? ''));
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,10 +148,24 @@ const scriptsIn = (html) => {
88
148
  if (!isScript && !preloads) {
89
149
  continue;
90
150
  }
91
- const name = fileNameOf(attribute(rest, isScript ? 'src' : 'href') ?? '');
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 ?? '');
92
162
  if (!/\.m?js$/.test(name)) {
93
163
  continue;
94
164
  }
165
+ if (isScript && isNoModule(rest)) {
166
+ legacy.add(name);
167
+ continue;
168
+ }
95
169
  names.add(name);
96
170
  if (isScript) {
97
171
  entries.add(name);
@@ -99,19 +173,35 @@ const scriptsIn = (html) => {
99
173
  }
100
174
  // After the tags, so the order stays the order the page names things in: the links come first
101
175
  // in the document and the script that starts the application is at the end of it.
102
- for (const name of startedInline(html)) {
103
- if (!/\.m?js$/.test(name)) {
176
+ const inline = startedInline(html);
177
+ for (const name of inline.started) {
178
+ if (!/\.m?js$/.test(name) || legacy.has(name)) {
104
179
  continue;
105
180
  }
106
181
  names.add(name);
107
182
  entries.add(name);
108
183
  }
109
- return { names: [...names], entries: [...entries] };
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 };
110
190
  };
111
191
  exports.scriptsIn = scriptsIn;
112
192
  /** The JavaScript files the page names, as bare file names, in the order the page names them. */
113
193
  const announcedIn = (html) => (0, exports.scriptsIn)(html).names;
114
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;
115
205
  /**
116
206
  * Everything else the page names before anything is painted: the images written into the HTML, and
117
207
  * whatever it preloads.
@@ -137,6 +227,12 @@ exports.announcedIn = announcedIn;
137
227
  * one with files that never travel together.
138
228
  */
139
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']);
140
236
  /** Which of a page's `rel="icon"` alternatives a current browser fetches: the SVG when there is one. */
141
237
  const chosenIcon = (icons) => {
142
238
  const svg = icons.find(icon => icon.type.includes('svg') || /\.svg$/i.test(icon.source));
@@ -172,6 +268,13 @@ const assetsIn = (html) => {
172
268
  continue;
173
269
  }
174
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
+ }
175
278
  if (lower === 'link' && [...rel].some(value => ON_DEMAND_ICON.has(value))) {
176
279
  icons.add(source);
177
280
  alternates.add(source);
@@ -296,14 +399,15 @@ const originsIn = (html) => {
296
399
  const byOrigin = new Map();
297
400
  const hinted = new Set();
298
401
  let base = null;
299
- const count = (url, isScript) => {
402
+ const count = (url, isScript, isStyle = false) => {
300
403
  const origin = originOf(url);
301
404
  if (!origin) {
302
405
  return;
303
406
  }
304
- const row = byOrigin.get(origin) ?? { origin, files: 0, scripts: 0 };
407
+ const row = byOrigin.get(origin) ?? { origin, files: 0, scripts: 0, styles: 0 };
305
408
  row.files += 1;
306
409
  row.scripts += isScript ? 1 : 0;
410
+ row.styles = (row.styles ?? 0) + (isStyle ? 1 : 0);
307
411
  byOrigin.set(origin, row);
308
412
  };
309
413
  for (const [, tag = '', rest = ''] of html.matchAll(TAG)) {
@@ -321,7 +425,9 @@ const originsIn = (html) => {
321
425
  if (!url) {
322
426
  continue;
323
427
  }
324
- 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');
325
431
  }
326
432
  for (const [, rest = ''] of html.matchAll(IMG)) {
327
433
  count(attribute(rest, 'src') ?? '', false);
@@ -340,3 +446,52 @@ const originsIn = (html) => {
340
446
  };
341
447
  };
342
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 = { '&amp;': '&', '&lt;': '<', '&gt;': '>', '&quot;': '"', '&#39;': "'" };
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;