@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
@@ -5,7 +5,7 @@
5
5
  * the terminal stays in one file.
6
6
  */
7
7
  Object.defineProperty(exports, "__esModule", { value: true });
8
- exports.readCriteria = exports.readProject = exports.readBaseline = exports.readDepsFiles = exports.readDist = exports.isFolder = exports.readStats = exports.readLoadlineConfig = exports.InputError = void 0;
8
+ exports.readCriteria = exports.readProject = exports.readBaseline = exports.nearestPackageName = exports.readDepsFiles = exports.readDist = exports.isFolder = exports.readStats = exports.readJson = exports.exists = exports.InputError = void 0;
9
9
  const node_crypto_1 = require("node:crypto");
10
10
  const node_fs_1 = require("node:fs");
11
11
  const promises_1 = require("node:fs/promises");
@@ -13,41 +13,36 @@ const node_path_1 = require("node:path");
13
13
  const node_stream_1 = require("node:stream");
14
14
  const promises_2 = require("node:stream/promises");
15
15
  const node_zlib_1 = require("node:zlib");
16
- const analysis_1 = require("../src/app/core/analysis/analysis");
17
- const metafile_types_1 = require("../src/app/core/analysis/metafile.types");
18
- const sourcemap_1 = require("../src/app/core/analysis/sourcemap");
19
- const assets_1 = require("../src/app/core/assets/assets");
20
- const baseline_1 = require("../src/app/core/baseline/baseline");
21
- const loadline_config_1 = require("../src/app/core/config/loadline-config");
22
- const criteria_1 = require("../src/app/core/criteria/criteria");
23
- const deps_1 = require("../src/app/core/deps/deps");
24
- const bundle_graph_1 = require("../src/app/core/intake/bundle-graph");
25
- const dist_files_1 = require("../src/app/core/intake/dist-files");
26
- const index_html_1 = require("../src/app/core/intake/index-html");
27
- const project_context_1 = require("../src/app/core/project/project-context");
28
- const report_messages_utils_1 = require("../src/app/state/report-messages.utils");
16
+ const analysis_1 = require("../../src/app/core/analysis/analysis");
17
+ const metafile_types_1 = require("../../src/app/core/analysis/metafile.types");
18
+ const sourcemap_1 = require("../../src/app/core/analysis/sourcemap/sourcemap");
19
+ const assets_1 = require("../../src/app/core/assets/assets");
20
+ const baseline_1 = require("../../src/app/core/baseline/baseline");
21
+ const index_html_1 = require("../../src/app/core/build-text/index-html");
22
+ const config_text_1 = require("../../src/app/core/config/config-text");
23
+ const config_values_1 = require("../../src/app/core/config/config-values");
24
+ const loadline_config_1 = require("../../src/app/core/config/loadline-config");
25
+ const deps_1 = require("../../src/app/core/deps/deps");
26
+ const bundle_graph_1 = require("../../src/app/core/intake/bundle-graph");
27
+ const dist_files_1 = require("../../src/app/core/intake/dist-files");
28
+ const webpack_stats_1 = require("../../src/app/core/intake/webpack-stats");
29
+ const project_context_1 = require("../../src/app/core/project/project-context");
30
+ const report_messages_utils_1 = require("../../src/app/state/report-messages.utils");
31
+ const text_errors_1 = require("../text/text-errors");
29
32
  /** Something the person can fix by editing the command line, as opposed to a bug in the tool. */
30
- class InputError extends Error {
31
- }
32
- exports.InputError = InputError;
33
33
  /**
34
- * `loadline.json`, from the path given or from the working directory.
35
- *
36
- * Found rather than required on purpose: the file is meant to be committed next to the code, and a
37
- * tool that only reads its configuration when told to is a tool whose configuration drifts. An
38
- * explicit `--config` that does not exist **is** an error — somebody asked for a file — while a
39
- * missing one in the working directory is simply the ordinary case.
34
+ * A failure of what was given, not of Loadline: exit 2. `say` words it in the language of the run,
35
+ * and `message` is the English, for anything that prints an error without asking which language.
40
36
  */
41
- const readLoadlineConfig = async (given, cwd) => {
42
- const path = given ?? (0, node_path_1.join)(cwd, 'loadline.json');
43
- if (!given && !(await exists(path))) {
44
- return { config: null, problems: [], name: null };
37
+ class InputError extends Error {
38
+ say;
39
+ constructor(say) {
40
+ const said = typeof say === 'string' ? () => say : say;
41
+ super(said(text_errors_1.ERROR_TEXT.en));
42
+ this.say = said;
45
43
  }
46
- const parsed = await readJson(path);
47
- const { config, problems } = (0, loadline_config_1.readConfig)(parsed);
48
- return { config, problems, name: config ? path : null };
49
- };
50
- exports.readLoadlineConfig = readLoadlineConfig;
44
+ }
45
+ exports.InputError = InputError;
51
46
  const exists = async (path) => {
52
47
  try {
53
48
  await (0, promises_1.stat)(path);
@@ -57,28 +52,30 @@ const exists = async (path) => {
57
52
  return false;
58
53
  }
59
54
  };
55
+ exports.exists = exists;
60
56
  const readJson = async (path) => {
61
57
  let text;
62
58
  try {
63
59
  text = await (0, promises_1.readFile)(path, 'utf8');
64
60
  }
65
61
  catch {
66
- throw new InputError(`Could not read ${path}.`);
62
+ throw new InputError(t => t.cannotRead(path));
67
63
  }
68
64
  try {
69
65
  return JSON.parse(text);
70
66
  }
71
67
  catch {
72
- throw new InputError(`${path} is not valid JSON.`);
68
+ throw new InputError(t => t.notJson(path));
73
69
  }
74
70
  };
71
+ exports.readJson = readJson;
75
72
  const filesUnder = async (folder) => {
76
73
  let entries;
77
74
  try {
78
75
  entries = await (0, promises_1.readdir)(folder, { withFileTypes: true, recursive: true });
79
76
  }
80
77
  catch {
81
- throw new InputError(`Could not read the folder ${folder}.`);
78
+ throw new InputError(t => t.cannotReadFolder(folder));
82
79
  }
83
80
  return entries.filter(entry => entry.isFile()).map(entry => (0, node_path_1.join)(entry.parentPath, entry.name));
84
81
  };
@@ -89,11 +86,12 @@ const filesUnder = async (folder) => {
89
86
  * command used to answer all three with "not an esbuild metafile", which is true and useless.
90
87
  */
91
88
  const readStats = async (path, t) => {
92
- const parsed = await readJson(path);
93
- if (!(0, metafile_types_1.isMetafile)(parsed)) {
89
+ const parsed = await (0, exports.readJson)(path);
90
+ const meta = (0, webpack_stats_1.metafileOf)(parsed);
91
+ if (!meta) {
94
92
  throw new InputError(`${path}: ${(0, report_messages_utils_1.foreignMessage)((0, metafile_types_1.foreignFormat)(parsed), t)} ${t.errExpected}`);
95
93
  }
96
- return parsed;
94
+ return meta;
97
95
  };
98
96
  exports.readStats = readStats;
99
97
  /** Whether what was named is a folder, which is what decides between the two ways of reading. */
@@ -142,28 +140,37 @@ const bundleFilesOf = (folder, paths) => Promise.all(paths.map(async (path) => {
142
140
  * @param derive also read the import graph out of the chunks, for a build with no stats file. It
143
141
  * reads every source map on the way, so the maps are never parsed twice.
144
142
  */
145
- const readDist = async (folder, derive = false) => {
143
+ const readDist = async (folder, derive = false, hints = {}) => {
146
144
  const paths = await filesUnder(folder);
147
145
  const gzip = new Map();
148
- const brotli = new Map();
146
+ const found = new Map();
147
+ // The same figures by path inside the folder, for the files that share a name with another.
148
+ const gzipByPath = new Map();
149
+ const foundByPath = new Map();
150
+ const inside = (path) => (0, node_path_1.relative)(folder, path).replaceAll('\\', '/');
149
151
  for (const path of paths) {
150
152
  const name = (0, node_path_1.basename)(path);
151
153
  const compressed = /^(.*\.(?:m?js|css))\.(?:br|brotli)$/i.exec(name);
152
154
  if (compressed?.[1]) {
153
155
  const info = await (0, promises_1.stat)(path);
154
- brotli.set(compressed[1], info.size);
156
+ found.set(compressed[1], info.size);
157
+ foundByPath.set(inside(path).replace(/\.(?:br|brotli)$/i, ''), info.size);
155
158
  }
156
159
  else if (/\.(?:m?js|css)$/i.test(name)) {
157
- gzip.set(name, await gzipSizeOf(path));
160
+ const size = await gzipSizeOf(path);
161
+ gzip.set(name, size);
162
+ gzipByPath.set(inside(path), size);
158
163
  }
159
164
  }
160
165
  if (gzip.size === 0) {
161
- throw new InputError(`No .js or .css files under ${folder}. Is that the browser/ folder of the build?`);
166
+ throw new InputError(t => t.noAssets(folder));
162
167
  }
168
+ const brotli = (0, dist_files_1.withGzipFallback)(found, gzip);
163
169
  // The page of the build says which chunks the browser asks for straight away, which is the one
164
170
  // thing the import graph cannot work out on its own.
165
- const page = (0, index_html_1.indexHtmlOf)(paths.map(path => ({ name: (0, node_path_1.basename)(path), path })));
166
- const html = page ? await (0, promises_1.readFile)(page.path, 'utf8') : null;
171
+ // Named by its path inside the folder, which is how `build.page` of `loadline.json` names it.
172
+ const page = (0, index_html_1.indexHtmlOf)(paths.map(path => ({ name: (0, node_path_1.basename)(path), path: (0, node_path_1.relative)(folder, path), file: path })), hints.page);
173
+ const html = page ? await (0, promises_1.readFile)(page.file, 'utf8') : null;
167
174
  // What the page asks for besides the code. A render-blocking stylesheet is the strictest
168
175
  // "downloaded before anything appears" there is, and it was the one thing the headline of this
169
176
  // tool left out.
@@ -197,7 +204,7 @@ const readDist = async (folder, derive = false) => {
197
204
  for (const path of paths) {
198
205
  // Only what a duplicate would be: a font, a picture, a video. Hashing every chunk would
199
206
  // compare files whose names already carry a content hash, which answers nothing.
200
- const uninteresting = new Set(['script', 'style', 'map']);
207
+ const uninteresting = new Set(['script', 'map']);
201
208
  if (uninteresting.has((0, assets_1.kindOf)((0, node_path_1.basename)(path)))) {
202
209
  continue;
203
210
  }
@@ -210,7 +217,7 @@ const readDist = async (folder, derive = false) => {
210
217
  const named = html
211
218
  ? (0, index_html_1.assetsIn)(html)
212
219
  : { referenced: [], preloaded: [], prefetched: [], hrefs: [], icons: [], alternates: [] };
213
- const assets = (0, assets_1.readAssets)({
220
+ const assetInput = {
214
221
  files: assetFiles,
215
222
  html,
216
223
  texts,
@@ -223,11 +230,25 @@ const readDist = async (folder, derive = false) => {
223
230
  // The same figures the rest of the report is in, so the first trip and the headline are
224
231
  // not the same bytes quoted in two units.
225
232
  weigh: brotli.size > 0 ? brotli : gzip,
226
- });
233
+ weighPath: brotli.size > 0 ? (0, dist_files_1.withGzipFallback)(foundByPath, gzipByPath) : gzipByPath,
234
+ };
235
+ const assets = (0, assets_1.readAssets)(assetInput);
236
+ // The first trip in every unit the folder can give, because which one the report is in is only
237
+ // decided later, by `--mode`.
238
+ // The scripts written inside the page, compressed the way the page itself would travel.
239
+ const inline = html ? (0, index_html_1.inlineScriptsIn)(html) : '';
240
+ const inlineBytes = inline
241
+ ? { raw: Buffer.byteLength(inline), gzip: (0, node_zlib_1.gzipSync)(inline).length, brotli: (0, node_zlib_1.brotliCompressSync)(inline).length }
242
+ : { raw: 0, gzip: 0, brotli: 0 };
243
+ const firstTrips = {
244
+ raw: (0, assets_1.firstTripIn)(assetInput, null, null, inlineBytes.raw),
245
+ gzip: (0, assets_1.firstTripIn)(assetInput, gzip, gzipByPath, inlineBytes.gzip),
246
+ ...(brotli.size > 0 && { brotli: (0, assets_1.firstTripIn)(assetInput, brotli, assetInput.weighPath, inlineBytes.brotli) }),
247
+ };
227
248
  const bundle = derive ? await bundleFilesOf(folder, paths) : [];
228
- const graph = derive ? await (0, bundle_graph_1.readBundleGraph)(bundle, new Set(html ? (0, index_html_1.scriptsIn)(html).entries : [])) : null;
249
+ const graph = derive ? await (0, bundle_graph_1.readFolderGraph)(bundle, html, hints) : null;
229
250
  const mapped = paths
230
- .filter(path => /\.m?js(?:\.map)?$/.test(path))
251
+ .filter(path => /\.m?js$/.test(path) || (0, sourcemap_1.isScriptMapName)(path))
231
252
  .map(path => ({ name: (0, node_path_1.basename)(path), text: () => (0, promises_1.readFile)(path, 'utf8') }));
232
253
  const splits = graph?.splits ?? (await (0, sourcemap_1.readSourceMaps)(mapped));
233
254
  return {
@@ -238,13 +259,19 @@ const readDist = async (folder, derive = false) => {
238
259
  graph,
239
260
  pageCss,
240
261
  assets,
262
+ firstTrips,
263
+ pageTitle: html ? (0, index_html_1.titleIn)(html) : null,
241
264
  hrefs: named.hrefs,
242
265
  pageOrigins: html ? (0, index_html_1.originsIn)(html) : null,
243
266
  // The chunk texts are already in memory for the reference search; they are handed on rather
244
267
  // than read a second time. The maps are read here for the same reason.
245
- texts: new Map([...texts].filter(([name]) => /\.(?:m?js|css)$/i.test(name))),
268
+ // The page too, for what its inline scripts carry: they are downloaded like any chunk.
269
+ texts: new Map([
270
+ ...[...texts].filter(([name]) => /\.(?:m?js|css)$/i.test(name)),
271
+ ...(page && html ? [[page.name, html]] : []),
272
+ ]),
246
273
  maps: await Promise.all(paths
247
- .filter(path => /\.m?js\.map$/i.test(path))
274
+ .filter(path => (0, sourcemap_1.isScriptMapName)(path))
248
275
  .map(async (path) => ({ name: (0, node_path_1.basename)(path), text: await (0, promises_1.readFile)(path, 'utf8') }))),
249
276
  };
250
277
  };
@@ -259,47 +286,84 @@ exports.readDist = readDist;
259
286
  */
260
287
  const readDepsFiles = async (lockPath, auditPath) => {
261
288
  let lock = null;
289
+ let lockDirect = null;
262
290
  if (lockPath) {
263
291
  const text = await (0, promises_1.readFile)(lockPath, 'utf8').catch(() => null);
264
292
  if (text === null) {
265
- throw new InputError(`Could not read ${lockPath}.`);
293
+ throw new InputError(t => t.cannotRead(lockPath));
266
294
  }
267
295
  lock = (0, deps_1.readLock)((0, node_path_1.basename)(lockPath), text);
268
296
  if (!lock) {
269
- throw new InputError(`${lockPath} is not a package-lock.json, a pnpm-lock.yaml or a yarn.lock.`);
297
+ throw new InputError(t => t.notLock(lockPath));
270
298
  }
299
+ // What the project asked for by name, from the package.json every lock file sits next to.
300
+ // Without it "shipped but never asked for" needed --project too, the help did not say so,
301
+ // and --lock on its own changed nothing anybody could see.
302
+ const manifest = await (0, promises_1.readFile)((0, node_path_1.join)((0, node_path_1.dirname)(lockPath), 'package.json'), 'utf8').catch(() => null);
303
+ const pkg = manifest === null ? null : (0, project_context_1.readPackageJson)(safeParse(manifest));
304
+ lockDirect = pkg ? pkg.dependencies : null;
271
305
  }
272
306
  let advisories = null;
273
307
  if (auditPath) {
274
308
  const text = await (0, promises_1.readFile)(auditPath, 'utf8').catch(() => null);
275
309
  if (text === null) {
276
- throw new InputError(`Could not read ${auditPath}.`);
310
+ throw new InputError(t => t.cannotRead(auditPath));
277
311
  }
278
312
  advisories = (0, deps_1.readAudit)(text);
279
313
  if (!advisories) {
280
- throw new InputError(`${auditPath} is not the JSON output of npm audit or pnpm audit.`);
314
+ throw new InputError(t => t.notAudit(auditPath));
281
315
  }
282
316
  }
283
- return { lock, advisories };
317
+ return { lock, advisories, lockDirect };
284
318
  };
285
319
  exports.readDepsFiles = readDepsFiles;
320
+ /**
321
+ * The `name` of the `package.json` the build sits under, to call the report by. A few levels up and
322
+ * no further: a build lives inside its project — `dist/app/browser` — and a folder copied somewhere
323
+ * else would otherwise be named after whatever `package.json` the walk happened to reach.
324
+ */
325
+ const nearestPackageName = async (from) => {
326
+ let folder = from;
327
+ for (let level = 0; level < 4; level++) {
328
+ const manifest = await (0, promises_1.readFile)((0, node_path_1.join)(folder, 'package.json'), 'utf8').catch(() => null);
329
+ if (manifest !== null) {
330
+ return (0, project_context_1.readPackageJson)(safeParse(manifest))?.name ?? null;
331
+ }
332
+ const parent = (0, node_path_1.dirname)(folder);
333
+ if (parent === folder) {
334
+ return null;
335
+ }
336
+ folder = parent;
337
+ }
338
+ return null;
339
+ };
340
+ exports.nearestPackageName = nearestPackageName;
341
+ const safeParse = (text) => {
342
+ try {
343
+ return JSON.parse(text);
344
+ }
345
+ catch {
346
+ return null;
347
+ }
348
+ };
286
349
  /**
287
350
  * The baseline: an analysis exported from Loadline, which carries the unit it was measured in, or a
288
351
  * previous `stats.json`, which can only be compared raw because nothing there says what its files
289
352
  * compressed to.
290
353
  */
291
354
  const readBaseline = async (path) => {
292
- const parsed = await readJson(path);
355
+ const parsed = await (0, exports.readJson)(path);
293
356
  if ((0, baseline_1.isSnapshot)(parsed)) {
294
357
  return parsed;
295
358
  }
296
- if (!(0, metafile_types_1.isMetafile)(parsed)) {
297
- throw new InputError(`${path} is neither a stats.json nor an analysis exported from Loadline.`);
359
+ const meta = (0, webpack_stats_1.metafileOf)(parsed);
360
+ if (!meta) {
361
+ throw new InputError(t => t.notStats(path));
298
362
  }
299
363
  const info = await (0, promises_1.stat)(path);
300
364
  // Named by the path it was given rather than by its file name: two builds compared in CI are
301
365
  // both called stats.json, and "compared against stats.json" says nothing.
302
- return (0, baseline_1.snapshotOf)((0, analysis_1.analyze)(parsed, null), 'raw', path, info.mtime);
366
+ return (0, baseline_1.snapshotOf)((0, analysis_1.analyze)(meta, null), 'raw', path, info.mtime);
303
367
  };
304
368
  exports.readBaseline = readBaseline;
305
369
  /**
@@ -325,13 +389,13 @@ const pipelinePaths = async (folder) => {
325
389
  const found = [];
326
390
  for (const name of PIPELINE_NAMES) {
327
391
  const path = (0, node_path_1.join)(folder, name);
328
- if (await exists(path)) {
392
+ if (await (0, exports.exists)(path)) {
329
393
  found.push(path);
330
394
  }
331
395
  }
332
396
  for (const segments of PIPELINE_FOLDERS) {
333
397
  const directory = (0, node_path_1.join)(folder, ...segments);
334
- if (!(await exists(directory))) {
398
+ if (!(await (0, exports.exists)(directory))) {
335
399
  continue;
336
400
  }
337
401
  const paths = await filesUnder(directory);
@@ -347,40 +411,41 @@ const readProject = async (folder) => {
347
411
  const angularPath = (0, node_path_1.join)(folder, 'angular.json');
348
412
  const packagePath = (0, node_path_1.join)(folder, 'package.json');
349
413
  // `package.json` first: the build commands of the pipeline resolve through its scripts.
350
- const pkg = (await exists(packagePath)) ? (0, project_context_1.readPackageJson)(await readJson(packagePath)) : null;
351
- const angular = (await exists(angularPath)) ? (0, project_context_1.readAngularJson)(await readJson(angularPath)) : null;
414
+ const pkg = (await (0, exports.exists)(packagePath)) ? (0, project_context_1.readPackageJson)(await (0, exports.readJson)(packagePath)) : null;
415
+ const angular = (await (0, exports.exists)(angularPath)) ? (0, project_context_1.readAngularJson)(await (0, exports.readJson)(angularPath)) : null;
352
416
  const pipelines = [];
353
417
  const paths = await pipelinePaths(folder);
354
418
  for (const path of paths) {
355
419
  pipelines.push((0, project_context_1.readPipeline)((0, node_path_1.basename)(path), await (0, promises_1.readFile)(path, 'utf8'), pkg?.scripts ?? {}));
356
420
  }
357
421
  if (!pkg && !angular && pipelines.length === 0) {
358
- throw new InputError(`No angular.json, package.json or pipeline file under ${folder}.`);
422
+ throw new InputError(t => t.noProject(folder));
359
423
  }
360
424
  return { ...project_context_1.EMPTY_CONTEXT, angular, pkg, pipelines };
361
425
  };
362
426
  exports.readProject = readProject;
363
427
  /**
364
- * A thresholds file: the same keys the page edits, sizes in bytes and coverages as fractions. Every
365
- * key is optional and replaces one recommended value; an unknown key is rejected rather than
366
- * ignored, because a typo that silently does nothing is the worst way for a gate to be wrong.
428
+ * A thresholds file: the same keys the page edits, either as a bare object or as the `criteria`
429
+ * block of a `loadline.json`. Values are written as in `loadline.json` — `"170kB"` or bytes,
430
+ * `"25%"` or a fraction. Every key is optional and replaces one recommended value; anything that
431
+ * cannot be read is rejected rather than ignored, because a typo that silently does nothing is the
432
+ * worst way for a gate to be wrong.
367
433
  */
368
- const readCriteria = async (path) => {
369
- const parsed = await readJson(path);
370
- if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
371
- throw new InputError(`${path} has to be an object of thresholds.`);
434
+ const readCriteria = async (path, lang = 'en') => {
435
+ const parsed = await (0, exports.readJson)(path);
436
+ const block = (0, loadline_config_1.isLoadlineConfig)(parsed) ? parsed.criteria : parsed;
437
+ if (!block || typeof block !== 'object' || Array.isArray(block)) {
438
+ throw new InputError(t => t.notThresholds(path));
372
439
  }
373
- const known = new Set(Object.keys(criteria_1.RECOMMENDED.raw));
374
- const criteria = {};
375
- for (const [key, value] of Object.entries(parsed)) {
376
- if (!known.has(key)) {
377
- throw new InputError(`${path}: "${key}" is not one of the criteria.`);
378
- }
379
- if (typeof value !== 'number' || !Number.isFinite(value)) {
380
- throw new InputError(`${path}: "${key}" has to be a number.`);
381
- }
382
- criteria[key] = value;
440
+ // Without "the recommended value stays", which is what `loadline.json` does with a value it
441
+ // cannot read: here the run stops, and the sentence promised a report that never came.
442
+ const { criteria, problems } = (0, config_values_1.readCriteriaBlock)(block, {
443
+ ...config_text_1.CONFIG_TEXT[lang],
444
+ recommendedStays: problem => problem,
445
+ });
446
+ if (problems[0]) {
447
+ throw new InputError(`${path}: ${problems[0]}`);
383
448
  }
384
- return criteria;
449
+ return criteria ?? {};
385
450
  };
386
451
  exports.readCriteria = readCriteria;
@@ -0,0 +1,120 @@
1
+ "use strict";
2
+ /**
3
+ * `loadline.json` off the disk, with whatever it `extends` found and joined in.
4
+ *
5
+ * Found rather than required on purpose: the file is meant to be committed next to the code, and a
6
+ * tool that only reads its configuration when told to is a tool whose configuration drifts. An
7
+ * explicit `--config` that does not exist **is** an error — somebody asked for a file — while a
8
+ * missing one in the working directory is simply the ordinary case.
9
+ *
10
+ * How the files join is `mergeConfigs`, in the analysis, because it is a rule about the file and not
11
+ * about the disk. What is here is only where each name of `extends` is:
12
+ *
13
+ * - **A path** (`./base.loadline.json`, `../shared/loadline.json`) is a file, from the folder of the
14
+ * file that writes it — not from where the command runs, which is what makes a base that extends
15
+ * another base work from any repository.
16
+ * - **Anything else is a package**: its `loadline.json`, found the way Node finds a package from
17
+ * that same folder. `@acme/loadline-config` reads `@acme/loadline-config/loadline.json`. No prefix
18
+ * is added and nothing is downloaded: it is whatever `npm install` put there, at the version the
19
+ * lock file says, so a base cannot change under a build without a commit.
20
+ */
21
+ Object.defineProperty(exports, "__esModule", { value: true });
22
+ exports.readLoadlineConfig = void 0;
23
+ const node_module_1 = require("node:module");
24
+ const node_path_1 = require("node:path");
25
+ const config_text_1 = require("../../src/app/core/config/config-text");
26
+ const loadline_config_1 = require("../../src/app/core/config/loadline-config");
27
+ const merge_config_1 = require("../../src/app/core/config/merge-config");
28
+ const text_1 = require("../text/text");
29
+ const read_build_1 = require("./read-build");
30
+ /** Where a name of `extends` is on disk, or `null` when it is nowhere. */
31
+ const locate = async (name, from) => {
32
+ if (name.startsWith('.') || (0, node_path_1.isAbsolute)(name)) {
33
+ const path = (0, node_path_1.resolve)((0, node_path_1.dirname)(from), name);
34
+ return (await (0, read_build_1.exists)(path)) ? path : null;
35
+ }
36
+ try {
37
+ return (0, node_module_1.createRequire)(from).resolve(name.endsWith('.json') ? name : `${name}/loadline.json`);
38
+ }
39
+ catch {
40
+ return null;
41
+ }
42
+ };
43
+ /** `own` on top of `base`, or the reason it cannot be: a mode that would misread every inherited size. */
44
+ const join2 = (base, own, lang, shown) => {
45
+ const names = { base: shown(base.files[0] ?? ''), own: shown(own.files[0] ?? '') };
46
+ const joined = (0, merge_config_1.mergeConfigs)(base.config, own.config, names, config_text_1.CONFIG_TEXT[lang]);
47
+ if (joined.gateProblems.length > 0) {
48
+ throw new read_build_1.InputError(joined.gateProblems.join(' '));
49
+ }
50
+ return {
51
+ config: joined.config,
52
+ problems: [...own.problems, ...base.problems],
53
+ files: [...own.files, ...base.files],
54
+ };
55
+ };
56
+ /**
57
+ * One file and, under it, what it extends — each base joined before the file on top of it, the
58
+ * names of one `extends` from left to right, so the last one named wins over the ones before it.
59
+ */
60
+ const load = async (path, config, problems, lang, seen, shown) => {
61
+ const own = {
62
+ config,
63
+ problems: problems.map(problem => text_1.CLI_TEXT[lang].configProblem(shown(path), problem)),
64
+ files: [path],
65
+ };
66
+ let base = null;
67
+ const names = config.extends ?? [];
68
+ for (const name of names) {
69
+ const found = await locate(name, path);
70
+ if (!found) {
71
+ throw new read_build_1.InputError(t => t.extendsNotFound(name, shown(path)));
72
+ }
73
+ if ([...seen, path].includes(found)) {
74
+ throw new read_build_1.InputError(t => t.extendsCycle([...seen, path, found].map(file => shown(file))));
75
+ }
76
+ const read = await readFile(found, lang, shown);
77
+ const next = await load(found, read.config, read.problems, lang, [...seen, path], shown);
78
+ base = base ? join2(base, next, lang, shown) : next;
79
+ }
80
+ return base ? join2(base, own, lang, shown) : own;
81
+ };
82
+ /** A file read and checked. Anything wrong in one named by `--config` or by `extends` stops the run. */
83
+ const readFile = async (path, lang, shown) => {
84
+ const { config, problems, gateProblems } = (0, loadline_config_1.readConfig)(await (0, read_build_1.readJson)(path), lang);
85
+ // The same mistake typed as a flag stops the run with exit 2; written in the file it used to
86
+ // print a line and switch the gate off, and a pipeline guarded by nothing passes. A base is held
87
+ // to the same rule: its gates are this repository's gates too.
88
+ if (gateProblems.length > 0) {
89
+ throw new read_build_1.InputError(`${shown(path)}: ${gateProblems.join(' ')}`);
90
+ }
91
+ if (!config) {
92
+ throw new read_build_1.InputError(`${shown(path)}: ${problems.join(' ')}`);
93
+ }
94
+ return { config, problems };
95
+ };
96
+ const readLoadlineConfig = async (given, cwd, lang = 'en') => {
97
+ const path = (0, node_path_1.resolve)(cwd, given ?? 'loadline.json');
98
+ if (!given && !(await (0, read_build_1.exists)(path))) {
99
+ return { config: null, problems: [], name: null };
100
+ }
101
+ const shown = (file) => (0, node_path_1.relative)(cwd, file) || file;
102
+ // A file somebody pointed at by name is meant to be one; one that merely sits in the working
103
+ // directory under that name may be somebody else's, and gets the warning.
104
+ const { config, problems, gateProblems } = (0, loadline_config_1.readConfig)(await (0, read_build_1.readJson)(path), lang);
105
+ if (!given && !config && gateProblems.length === 0) {
106
+ return {
107
+ config: null,
108
+ problems: problems.map(problem => text_1.CLI_TEXT[lang].configProblem(shown(path), problem)),
109
+ name: null,
110
+ };
111
+ }
112
+ const top = await readFile(path, lang, shown);
113
+ const loaded = await load(path, top.config, top.problems, lang, [], shown);
114
+ return {
115
+ config: loaded.config,
116
+ problems: loaded.problems,
117
+ name: loaded.files.map(file => shown(file)).join(' ← '),
118
+ };
119
+ };
120
+ exports.readLoadlineConfig = readLoadlineConfig;
@@ -15,18 +15,20 @@
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
16
  exports.openFile = exports.writeHtmlReport = exports.embedText = exports.embedFolder = void 0;
17
17
  const node_child_process_1 = require("node:child_process");
18
+ const node_crypto_1 = require("node:crypto");
18
19
  const promises_1 = require("node:fs/promises");
19
20
  const node_path_1 = require("node:path");
20
- const read_build_1 = require("./read-build");
21
+ const read_build_1 = require("../read/read-build");
21
22
  /** Files read as text inside the page. Everything else is carried as bytes. */
22
23
  const TEXT_FILE = /\.(?:m?js|css|html?|json|webmanifest|map|xml|txt|svg)$/i;
23
24
  /** Carried for their size only: the page never reads what is inside a pre-compressed copy. */
24
25
  const SIZE_ONLY = /\.(?:br|brotli|gz|zst)$/i;
25
- /** Above this a binary travels as a filler of the same size: a report is not a backup of the build. */
26
- const MAX_BINARY = 2 * 1024 * 1024;
27
26
  /** Where `loadline.html` is: next to the package when installed, at the root of a clone otherwise. */
28
27
  const pagePath = async () => {
29
- const candidates = [(0, node_path_1.join)(__dirname, '..', '..', '..', 'loadline.html'), (0, node_path_1.join)(__dirname, '..', 'loadline.html')];
28
+ const candidates = [
29
+ (0, node_path_1.join)(__dirname, '..', '..', '..', '..', 'loadline.html'),
30
+ (0, node_path_1.join)(__dirname, '..', '..', 'loadline.html'),
31
+ ];
30
32
  for (const candidate of candidates) {
31
33
  try {
32
34
  const info = await (0, promises_1.stat)(candidate);
@@ -38,7 +40,7 @@ const pagePath = async () => {
38
40
  // The next one.
39
41
  }
40
42
  }
41
- throw new read_build_1.InputError('loadline.html is missing from this install, so --html has nothing to write. Run: pnpm build');
43
+ throw new read_build_1.InputError(t => t.htmlMissing);
42
44
  };
43
45
  const walk = async (folder) => {
44
46
  const entries = await (0, promises_1.readdir)(folder, { withFileTypes: true, recursive: true });
@@ -57,12 +59,14 @@ const embedFolder = async (folder) => {
57
59
  else if (TEXT_FILE.test(path)) {
58
60
  files.push({ path: inside, text: await (0, promises_1.readFile)(path, 'utf8') });
59
61
  }
60
- else if (size > MAX_BINARY) {
61
- files.push({ path: inside, size });
62
- }
63
62
  else {
64
- const bytes = await (0, promises_1.readFile)(path);
65
- files.push({ path: inside, b64: bytes.toString('base64') });
63
+ // The page reads a binary only to hash it, so its hash travels instead of its bytes:
64
+ // a report is not a backup of the build, and as base64 the fonts and pictures of one
65
+ // build came to 18 MB of a 54 MB file.
66
+ const sha256 = (0, node_crypto_1.createHash)('sha256')
67
+ .update(await (0, promises_1.readFile)(path))
68
+ .digest('hex');
69
+ files.push({ path: inside, size, sha256 });
66
70
  }
67
71
  }
68
72
  return { name: (0, node_path_1.basename)(folder), files };