@bymaksym/loadline 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +182 -0
- package/bin/loadline.js +50 -0
- package/dist/cli/cli/args.js +324 -0
- package/dist/cli/cli/args.types.js +3 -0
- package/dist/cli/cli/gates.js +173 -0
- package/dist/cli/cli/gates.types.js +3 -0
- package/dist/cli/cli/main.js +156 -0
- package/dist/cli/cli/read-build.js +382 -0
- package/dist/cli/cli/read-build.types.js +3 -0
- package/dist/cli/cli/render-json.js +116 -0
- package/dist/cli/cli/render-json.types.js +10 -0
- package/dist/cli/cli/render-markdown.js +30 -0
- package/dist/cli/cli/render-pr.js +76 -0
- package/dist/cli/cli/render-sarif.js +83 -0
- package/dist/cli/cli/render-text.js +388 -0
- package/dist/cli/cli/report.js +196 -0
- package/dist/cli/cli/report.types.js +3 -0
- package/dist/cli/cli/self-check.js +85 -0
- package/dist/cli/cli/text.js +180 -0
- package/dist/cli/cli/text.types.js +10 -0
- package/dist/cli/src/app/core/analysis/analysis.js +661 -0
- package/dist/cli/src/app/core/analysis/analysis.types.js +6 -0
- package/dist/cli/src/app/core/analysis/barrels.js +128 -0
- package/dist/cli/src/app/core/analysis/blast.js +172 -0
- package/dist/cli/src/app/core/analysis/cycles.js +110 -0
- package/dist/cli/src/app/core/analysis/delivery.js +143 -0
- package/dist/cli/src/app/core/analysis/entries.js +217 -0
- package/dist/cli/src/app/core/analysis/exclusive.js +91 -0
- package/dist/cli/src/app/core/analysis/importers.js +38 -0
- package/dist/cli/src/app/core/analysis/insights.js +202 -0
- package/dist/cli/src/app/core/analysis/insights.types.js +10 -0
- package/dist/cli/src/app/core/analysis/invariant.js +69 -0
- package/dist/cli/src/app/core/analysis/metafile.types.js +44 -0
- package/dist/cli/src/app/core/analysis/module-graph.js +192 -0
- package/dist/cli/src/app/core/analysis/sourcemap.js +231 -0
- package/dist/cli/src/app/core/analysis/sourcemap.types.js +3 -0
- package/dist/cli/src/app/core/analysis/split-drift.js +47 -0
- package/dist/cli/src/app/core/assets/assets.js +296 -0
- package/dist/cli/src/app/core/assets/assets.types.js +11 -0
- package/dist/cli/src/app/core/baseline/baseline.js +0 -0
- package/dist/cli/src/app/core/baseline/baseline.types.js +3 -0
- package/dist/cli/src/app/core/caching/caching.js +241 -0
- package/dist/cli/src/app/core/caching/caching.types.js +10 -0
- package/dist/cli/src/app/core/caching/from-analysis.js +73 -0
- package/dist/cli/src/app/core/config/loadline-config.js +110 -0
- package/dist/cli/src/app/core/config/loadline-config.types.js +12 -0
- package/dist/cli/src/app/core/criteria/criteria.js +228 -0
- package/dist/cli/src/app/core/criteria/criteria.types.js +3 -0
- package/dist/cli/src/app/core/deps/deps.js +225 -0
- package/dist/cli/src/app/core/deps/deps.types.js +15 -0
- package/dist/cli/src/app/core/export/markdown-table.utils.js +40 -0
- package/dist/cli/src/app/core/export/summary.js +72 -0
- package/dist/cli/src/app/core/findings/actions.js +59 -0
- package/dist/cli/src/app/core/findings/assets.js +130 -0
- package/dist/cli/src/app/core/findings/caching.js +165 -0
- package/dist/cli/src/app/core/findings/compose.js +41 -0
- package/dist/cli/src/app/core/findings/deps.js +66 -0
- package/dist/cli/src/app/core/findings/effort.js +117 -0
- package/dist/cli/src/app/core/findings/finding-html.js +13 -0
- package/dist/cli/src/app/core/findings/finding-plain.js +26 -0
- package/dist/cli/src/app/core/findings/finding-text.js +1231 -0
- package/dist/cli/src/app/core/findings/finding.types.js +80 -0
- package/dist/cli/src/app/core/findings/findings.js +514 -0
- package/dist/cli/src/app/core/findings/graph.js +167 -0
- package/dist/cli/src/app/core/findings/own-in-boot.js +88 -0
- package/dist/cli/src/app/core/findings/page.js +49 -0
- package/dist/cli/src/app/core/findings/requests.js +185 -0
- package/dist/cli/src/app/core/findings/scan.js +126 -0
- package/dist/cli/src/app/core/findings/shipped.js +244 -0
- package/dist/cli/src/app/core/findings/situation.js +116 -0
- package/dist/cli/src/app/core/format/format.utils.js +204 -0
- package/dist/cli/src/app/core/i18n/en.js +819 -0
- package/dist/cli/src/app/core/i18n/es.js +822 -0
- package/dist/cli/src/app/core/i18n/ui-strings.js +11 -0
- package/dist/cli/src/app/core/i18n/ui.js +7 -0
- package/dist/cli/src/app/core/intake/bundle-graph.js +346 -0
- package/dist/cli/src/app/core/intake/bundle-graph.types.js +3 -0
- package/dist/cli/src/app/core/intake/dist-files.js +160 -0
- package/dist/cli/src/app/core/intake/index-html.js +292 -0
- package/dist/cli/src/app/core/json/json.utils.js +55 -0
- package/dist/cli/src/app/core/measurement/measurement.types.js +14 -0
- package/dist/cli/src/app/core/project/budget-advice.js +76 -0
- package/dist/cli/src/app/core/project/project-context.js +394 -0
- package/dist/cli/src/app/core/project/project-context.types.js +3 -0
- package/dist/cli/src/app/core/scan/catalog.js +67 -0
- package/dist/cli/src/app/core/scan/scan.js +280 -0
- package/dist/cli/src/app/core/scan/scan.types.js +10 -0
- package/dist/cli/src/app/core/scan/secrets.js +0 -0
- package/dist/cli/src/app/core/situation/situation.js +261 -0
- package/dist/cli/src/app/core/situation/situation.types.js +19 -0
- package/dist/cli/src/app/core/timing/timing.js +87 -0
- package/dist/cli/src/app/core/timing/timing.types.js +3 -0
- package/dist/cli/src/app/core/whatif/defer.js +68 -0
- package/dist/cli/src/app/state/report-messages.utils.js +73 -0
- package/docs/HOW-IT-WORKS.md +1087 -0
- package/loadline.html +78 -0
- package/package.json +154 -0
|
@@ -0,0 +1,382 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* The disk side of the command: the same inputs the page takes by drag and drop, read from paths.
|
|
4
|
+
* Nothing here computes anything — that is `report.ts` — so what differs between the browser and
|
|
5
|
+
* the terminal stays in one file.
|
|
6
|
+
*/
|
|
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;
|
|
9
|
+
const node_crypto_1 = require("node:crypto");
|
|
10
|
+
const node_fs_1 = require("node:fs");
|
|
11
|
+
const promises_1 = require("node:fs/promises");
|
|
12
|
+
const node_path_1 = require("node:path");
|
|
13
|
+
const node_stream_1 = require("node:stream");
|
|
14
|
+
const promises_2 = require("node:stream/promises");
|
|
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");
|
|
29
|
+
/** 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
|
+
/**
|
|
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.
|
|
40
|
+
*/
|
|
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 };
|
|
45
|
+
}
|
|
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;
|
|
51
|
+
const exists = async (path) => {
|
|
52
|
+
try {
|
|
53
|
+
await (0, promises_1.stat)(path);
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
const readJson = async (path) => {
|
|
61
|
+
let text;
|
|
62
|
+
try {
|
|
63
|
+
text = await (0, promises_1.readFile)(path, 'utf8');
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
throw new InputError(`Could not read ${path}.`);
|
|
67
|
+
}
|
|
68
|
+
try {
|
|
69
|
+
return JSON.parse(text);
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
throw new InputError(`${path} is not valid JSON.`);
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
const filesUnder = async (folder) => {
|
|
76
|
+
let entries;
|
|
77
|
+
try {
|
|
78
|
+
entries = await (0, promises_1.readdir)(folder, { withFileTypes: true, recursive: true });
|
|
79
|
+
}
|
|
80
|
+
catch {
|
|
81
|
+
throw new InputError(`Could not read the folder ${folder}.`);
|
|
82
|
+
}
|
|
83
|
+
return entries.filter(entry => entry.isFile()).map(entry => (0, node_path_1.join)(entry.parentPath, entry.name));
|
|
84
|
+
};
|
|
85
|
+
/**
|
|
86
|
+
* The stats file. When it is not one, the format it *is* gets named: a webpack stats.json, a Vite
|
|
87
|
+
* manifest and a visualizer dump are all files somebody can reasonably believe are "the stats of my
|
|
88
|
+
* build", and each has a different one-sentence answer. The page has said this for a while; the
|
|
89
|
+
* command used to answer all three with "not an esbuild metafile", which is true and useless.
|
|
90
|
+
*/
|
|
91
|
+
const readStats = async (path, t) => {
|
|
92
|
+
const parsed = await readJson(path);
|
|
93
|
+
if (!(0, metafile_types_1.isMetafile)(parsed)) {
|
|
94
|
+
throw new InputError(`${path}: ${(0, report_messages_utils_1.foreignMessage)((0, metafile_types_1.foreignFormat)(parsed), t)} ${t.errExpected}`);
|
|
95
|
+
}
|
|
96
|
+
return parsed;
|
|
97
|
+
};
|
|
98
|
+
exports.readStats = readStats;
|
|
99
|
+
/** Whether what was named is a folder, which is what decides between the two ways of reading. */
|
|
100
|
+
const isFolder = async (path) => {
|
|
101
|
+
try {
|
|
102
|
+
const info = await (0, promises_1.stat)(path);
|
|
103
|
+
return info.isDirectory();
|
|
104
|
+
}
|
|
105
|
+
catch {
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
exports.isFolder = isFolder;
|
|
110
|
+
/**
|
|
111
|
+
* What a file compresses to. `createGzip()` and the browser's `CompressionStream('gzip')` are both
|
|
112
|
+
* zlib at its default level, so this is the figure the page shows for the same file. Streamed
|
|
113
|
+
* rather than read whole: a build folder runs to tens of megabytes and only the count is wanted.
|
|
114
|
+
*/
|
|
115
|
+
const gzipSizeOf = async (path) => {
|
|
116
|
+
let size = 0;
|
|
117
|
+
const count = new node_stream_1.Writable({
|
|
118
|
+
write(chunk, _encoding, done) {
|
|
119
|
+
size += chunk.length;
|
|
120
|
+
done();
|
|
121
|
+
},
|
|
122
|
+
});
|
|
123
|
+
await (0, promises_2.pipeline)((0, node_fs_1.createReadStream)(path), (0, node_zlib_1.createGzip)(), count);
|
|
124
|
+
return size;
|
|
125
|
+
};
|
|
126
|
+
/**
|
|
127
|
+
* The folder as the graph reader takes it: paths relative to the folder, with forward slashes on
|
|
128
|
+
* every platform, because that is how a chunk writes `./sibling.js` inside itself.
|
|
129
|
+
*/
|
|
130
|
+
const bundleFilesOf = (folder, paths) => Promise.all(paths.map(async (path) => {
|
|
131
|
+
const info = await (0, promises_1.stat)(path);
|
|
132
|
+
return {
|
|
133
|
+
path: (0, node_path_1.relative)(folder, path).replaceAll('\\', '/'),
|
|
134
|
+
bytes: info.size,
|
|
135
|
+
text: () => (0, promises_1.readFile)(path, 'utf8'),
|
|
136
|
+
};
|
|
137
|
+
}));
|
|
138
|
+
/**
|
|
139
|
+
* The build folder. Names are cut down to their last segment, which is how the metafile names its
|
|
140
|
+
* outputs and how the page keys the same figures.
|
|
141
|
+
*
|
|
142
|
+
* @param derive also read the import graph out of the chunks, for a build with no stats file. It
|
|
143
|
+
* reads every source map on the way, so the maps are never parsed twice.
|
|
144
|
+
*/
|
|
145
|
+
const readDist = async (folder, derive = false) => {
|
|
146
|
+
const paths = await filesUnder(folder);
|
|
147
|
+
const gzip = new Map();
|
|
148
|
+
const brotli = new Map();
|
|
149
|
+
for (const path of paths) {
|
|
150
|
+
const name = (0, node_path_1.basename)(path);
|
|
151
|
+
const compressed = /^(.*\.(?:m?js|css))\.(?:br|brotli)$/i.exec(name);
|
|
152
|
+
if (compressed?.[1]) {
|
|
153
|
+
const info = await (0, promises_1.stat)(path);
|
|
154
|
+
brotli.set(compressed[1], info.size);
|
|
155
|
+
}
|
|
156
|
+
else if (/\.(?:m?js|css)$/i.test(name)) {
|
|
157
|
+
gzip.set(name, await gzipSizeOf(path));
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
if (gzip.size === 0) {
|
|
161
|
+
throw new InputError(`No .js or .css files under ${folder}. Is that the browser/ folder of the build?`);
|
|
162
|
+
}
|
|
163
|
+
// The page of the build says which chunks the browser asks for straight away, which is the one
|
|
164
|
+
// 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;
|
|
167
|
+
// What the page asks for besides the code. A render-blocking stylesheet is the strictest
|
|
168
|
+
// "downloaded before anything appears" there is, and it was the one thing the headline of this
|
|
169
|
+
// tool left out.
|
|
170
|
+
const cssRaw = new Map();
|
|
171
|
+
for (const path of paths) {
|
|
172
|
+
if (!/\.css$/i.test(path)) {
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
const info = await (0, promises_1.stat)(path);
|
|
176
|
+
cssRaw.set((0, node_path_1.basename)(path), info.size);
|
|
177
|
+
}
|
|
178
|
+
const pageCss = html ? (0, dist_files_1.pageCssOf)((0, index_html_1.stylesIn)(html), { raw: cssRaw, gzip, brotli }) : null;
|
|
179
|
+
// --- everything the folder holds besides the code -------------------------------------------
|
|
180
|
+
// The texts are read once and used twice: to find which file names anything mentions, and to
|
|
181
|
+
// count the bytes that are really an image the compiler put inline. The hashes are what turns
|
|
182
|
+
// "two files of the same size" into "the same file twice", which is a claim rather than a hunch.
|
|
183
|
+
const assetFiles = await Promise.all(paths.map(async (path) => {
|
|
184
|
+
const info = await (0, promises_1.stat)(path);
|
|
185
|
+
return { path: (0, node_path_1.relative)(folder, path).replaceAll('\\', '/'), name: (0, node_path_1.basename)(path), bytes: info.size };
|
|
186
|
+
}));
|
|
187
|
+
// Every text file of the folder, not only the code. A web app manifest names the icons and a
|
|
188
|
+
// `robots.txt` names a sitemap: reading only the chunks reported both as files nothing reaches,
|
|
189
|
+
// which is a wrong answer given with the same confidence as a right one.
|
|
190
|
+
const texts = new Map();
|
|
191
|
+
for (const path of paths) {
|
|
192
|
+
if (/\.(?:m?js|css|json|webmanifest|xml|txt|svg)$/i.test(path)) {
|
|
193
|
+
texts.set((0, node_path_1.basename)(path), await (0, promises_1.readFile)(path, 'utf8'));
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
const hashes = new Map();
|
|
197
|
+
for (const path of paths) {
|
|
198
|
+
// Only what a duplicate would be: a font, a picture, a video. Hashing every chunk would
|
|
199
|
+
// compare files whose names already carry a content hash, which answers nothing.
|
|
200
|
+
const uninteresting = new Set(['script', 'style', 'map']);
|
|
201
|
+
if (uninteresting.has((0, assets_1.kindOf)((0, node_path_1.basename)(path)))) {
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
// Keyed by path: a folder per route writes several `index.html`, and keyed by name they
|
|
205
|
+
// would collapse onto one hash and come out as copies of each other.
|
|
206
|
+
hashes.set((0, node_path_1.relative)(folder, path).replaceAll('\\', '/'), (0, node_crypto_1.createHash)('sha256')
|
|
207
|
+
.update(await (0, promises_1.readFile)(path))
|
|
208
|
+
.digest('hex'));
|
|
209
|
+
}
|
|
210
|
+
const named = html ? (0, index_html_1.assetsIn)(html) : { referenced: [], preloaded: [], prefetched: [], hrefs: [] };
|
|
211
|
+
const assets = (0, assets_1.readAssets)({
|
|
212
|
+
files: assetFiles,
|
|
213
|
+
html,
|
|
214
|
+
texts,
|
|
215
|
+
hashes,
|
|
216
|
+
inPage: new Set([...named.referenced, ...(html ? (0, index_html_1.announcedIn)(html) : []), ...(html ? (0, index_html_1.stylesIn)(html) : [])]),
|
|
217
|
+
preloaded: new Set(named.preloaded),
|
|
218
|
+
prefetched: new Set(named.prefetched),
|
|
219
|
+
bootChunks: new Set(),
|
|
220
|
+
// The same figures the rest of the report is in, so the first trip and the headline are
|
|
221
|
+
// not the same bytes quoted in two units.
|
|
222
|
+
weigh: brotli.size > 0 ? brotli : gzip,
|
|
223
|
+
});
|
|
224
|
+
const bundle = derive ? await bundleFilesOf(folder, paths) : [];
|
|
225
|
+
const graph = derive ? await (0, bundle_graph_1.readBundleGraph)(bundle, new Set(html ? (0, index_html_1.scriptsIn)(html).entries : [])) : null;
|
|
226
|
+
const mapped = paths
|
|
227
|
+
.filter(path => /\.m?js(?:\.map)?$/.test(path))
|
|
228
|
+
.map(path => ({ name: (0, node_path_1.basename)(path), text: () => (0, promises_1.readFile)(path, 'utf8') }));
|
|
229
|
+
const splits = graph?.splits ?? (await (0, sourcemap_1.readSourceMaps)(mapped));
|
|
230
|
+
return {
|
|
231
|
+
gzip,
|
|
232
|
+
brotli: brotli.size > 0 ? brotli : null,
|
|
233
|
+
splits: splits.size > 0 ? splits : null,
|
|
234
|
+
announced: html ? new Set((0, index_html_1.announcedIn)(html)) : null,
|
|
235
|
+
graph,
|
|
236
|
+
pageCss,
|
|
237
|
+
assets,
|
|
238
|
+
hrefs: named.hrefs,
|
|
239
|
+
pageOrigins: html ? (0, index_html_1.originsIn)(html) : null,
|
|
240
|
+
// The chunk texts are already in memory for the reference search; they are handed on rather
|
|
241
|
+
// than read a second time. The maps are read here for the same reason.
|
|
242
|
+
texts: new Map([...texts].filter(([name]) => /\.(?:m?js|css)$/i.test(name))),
|
|
243
|
+
maps: await Promise.all(paths
|
|
244
|
+
.filter(path => /\.m?js\.map$/i.test(path))
|
|
245
|
+
.map(async (path) => ({ name: (0, node_path_1.basename)(path), text: await (0, promises_1.readFile)(path, 'utf8') }))),
|
|
246
|
+
};
|
|
247
|
+
};
|
|
248
|
+
exports.readDist = readDist;
|
|
249
|
+
/**
|
|
250
|
+
* The lock file and the audit report: two files the person already has, read through the same door
|
|
251
|
+
* `angular.json` goes through.
|
|
252
|
+
*
|
|
253
|
+
* Both are optional and both are `null` when absent rather than empty, because "nobody ran an
|
|
254
|
+
* audit" and "the audit found nothing" are different statements and a report that conflates them
|
|
255
|
+
* is telling somebody they are safe when nothing checked.
|
|
256
|
+
*/
|
|
257
|
+
const readDepsFiles = async (lockPath, auditPath) => {
|
|
258
|
+
let lock = null;
|
|
259
|
+
if (lockPath) {
|
|
260
|
+
const text = await (0, promises_1.readFile)(lockPath, 'utf8').catch(() => null);
|
|
261
|
+
if (text === null) {
|
|
262
|
+
throw new InputError(`Could not read ${lockPath}.`);
|
|
263
|
+
}
|
|
264
|
+
lock = (0, deps_1.readLock)((0, node_path_1.basename)(lockPath), text);
|
|
265
|
+
if (!lock) {
|
|
266
|
+
throw new InputError(`${lockPath} is not a package-lock.json, a pnpm-lock.yaml or a yarn.lock.`);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
let advisories = null;
|
|
270
|
+
if (auditPath) {
|
|
271
|
+
const text = await (0, promises_1.readFile)(auditPath, 'utf8').catch(() => null);
|
|
272
|
+
if (text === null) {
|
|
273
|
+
throw new InputError(`Could not read ${auditPath}.`);
|
|
274
|
+
}
|
|
275
|
+
advisories = (0, deps_1.readAudit)(text);
|
|
276
|
+
if (!advisories) {
|
|
277
|
+
throw new InputError(`${auditPath} is not the JSON output of npm audit or pnpm audit.`);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
return { lock, advisories };
|
|
281
|
+
};
|
|
282
|
+
exports.readDepsFiles = readDepsFiles;
|
|
283
|
+
/**
|
|
284
|
+
* The baseline: an analysis exported from Loadline, which carries the unit it was measured in, or a
|
|
285
|
+
* previous `stats.json`, which can only be compared raw because nothing there says what its files
|
|
286
|
+
* compressed to.
|
|
287
|
+
*/
|
|
288
|
+
const readBaseline = async (path) => {
|
|
289
|
+
const parsed = await readJson(path);
|
|
290
|
+
if ((0, baseline_1.isSnapshot)(parsed)) {
|
|
291
|
+
return parsed;
|
|
292
|
+
}
|
|
293
|
+
if (!(0, metafile_types_1.isMetafile)(parsed)) {
|
|
294
|
+
throw new InputError(`${path} is neither a stats.json nor an analysis exported from Loadline.`);
|
|
295
|
+
}
|
|
296
|
+
const info = await (0, promises_1.stat)(path);
|
|
297
|
+
// Named by the path it was given rather than by its file name: two builds compared in CI are
|
|
298
|
+
// both called stats.json, and "compared against stats.json" says nothing.
|
|
299
|
+
return (0, baseline_1.snapshotOf)((0, analysis_1.analyze)(parsed, null), 'raw', path, info.mtime);
|
|
300
|
+
};
|
|
301
|
+
exports.readBaseline = readBaseline;
|
|
302
|
+
/**
|
|
303
|
+
* Pipeline files that sit at the root of a project, one name per CI.
|
|
304
|
+
*
|
|
305
|
+
* It was GitLab's two names and GitHub's folder. Everything else — Azure, Bitbucket, CircleCI,
|
|
306
|
+
* Jenkins — was simply not looked for, so the budget check quietly did half its job on those
|
|
307
|
+
* projects and said nothing about it.
|
|
308
|
+
*/
|
|
309
|
+
const PIPELINE_NAMES = [
|
|
310
|
+
'.gitlab-ci.yml',
|
|
311
|
+
'.gitlab-ci.yaml',
|
|
312
|
+
'azure-pipelines.yml',
|
|
313
|
+
'azure-pipelines.yaml',
|
|
314
|
+
'bitbucket-pipelines.yml',
|
|
315
|
+
'bitbucket-pipelines.yaml',
|
|
316
|
+
'Jenkinsfile',
|
|
317
|
+
];
|
|
318
|
+
/** Folders where a CI keeps one file per pipeline. */
|
|
319
|
+
const PIPELINE_FOLDERS = [['.github', 'workflows'], ['.circleci']];
|
|
320
|
+
/** The pipeline files worth looking for, in the places projects keep them. */
|
|
321
|
+
const pipelinePaths = async (folder) => {
|
|
322
|
+
const found = [];
|
|
323
|
+
for (const name of PIPELINE_NAMES) {
|
|
324
|
+
const path = (0, node_path_1.join)(folder, name);
|
|
325
|
+
if (await exists(path)) {
|
|
326
|
+
found.push(path);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
for (const segments of PIPELINE_FOLDERS) {
|
|
330
|
+
const directory = (0, node_path_1.join)(folder, ...segments);
|
|
331
|
+
if (await exists(directory)) {
|
|
332
|
+
const paths = await filesUnder(directory);
|
|
333
|
+
found.push(...paths.filter(path => /\.ya?ml$/i.test(path)));
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
return found;
|
|
337
|
+
};
|
|
338
|
+
/**
|
|
339
|
+
* The project's own configuration, for the budget checks. A missing file is not an error: each one
|
|
340
|
+
* adds a check, and a project with no pipeline file still gets everything `angular.json` allows.
|
|
341
|
+
*/
|
|
342
|
+
const readProject = async (folder) => {
|
|
343
|
+
const angularPath = (0, node_path_1.join)(folder, 'angular.json');
|
|
344
|
+
const packagePath = (0, node_path_1.join)(folder, 'package.json');
|
|
345
|
+
// `package.json` first: the build commands of the pipeline resolve through its scripts.
|
|
346
|
+
const pkg = (await exists(packagePath)) ? (0, project_context_1.readPackageJson)(await readJson(packagePath)) : null;
|
|
347
|
+
const angular = (await exists(angularPath)) ? (0, project_context_1.readAngularJson)(await readJson(angularPath)) : null;
|
|
348
|
+
const pipelines = [];
|
|
349
|
+
const paths = await pipelinePaths(folder);
|
|
350
|
+
for (const path of paths) {
|
|
351
|
+
pipelines.push((0, project_context_1.readPipeline)((0, node_path_1.basename)(path), await (0, promises_1.readFile)(path, 'utf8'), pkg?.scripts ?? {}));
|
|
352
|
+
}
|
|
353
|
+
if (!pkg && !angular && pipelines.length === 0) {
|
|
354
|
+
throw new InputError(`No angular.json, package.json or pipeline file under ${folder}.`);
|
|
355
|
+
}
|
|
356
|
+
return { ...project_context_1.EMPTY_CONTEXT, angular, pkg, pipelines };
|
|
357
|
+
};
|
|
358
|
+
exports.readProject = readProject;
|
|
359
|
+
/**
|
|
360
|
+
* A thresholds file: the same keys the page edits, sizes in bytes and coverages as fractions. Every
|
|
361
|
+
* key is optional and replaces one recommended value; an unknown key is rejected rather than
|
|
362
|
+
* ignored, because a typo that silently does nothing is the worst way for a gate to be wrong.
|
|
363
|
+
*/
|
|
364
|
+
const readCriteria = async (path) => {
|
|
365
|
+
const parsed = await readJson(path);
|
|
366
|
+
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
|
|
367
|
+
throw new InputError(`${path} has to be an object of thresholds.`);
|
|
368
|
+
}
|
|
369
|
+
const known = new Set(Object.keys(criteria_1.RECOMMENDED.raw));
|
|
370
|
+
const criteria = {};
|
|
371
|
+
for (const [key, value] of Object.entries(parsed)) {
|
|
372
|
+
if (!known.has(key)) {
|
|
373
|
+
throw new InputError(`${path}: "${key}" is not one of the criteria.`);
|
|
374
|
+
}
|
|
375
|
+
if (typeof value !== 'number' || !Number.isFinite(value)) {
|
|
376
|
+
throw new InputError(`${path}: "${key}" has to be a number.`);
|
|
377
|
+
}
|
|
378
|
+
criteria[key] = value;
|
|
379
|
+
}
|
|
380
|
+
return criteria;
|
|
381
|
+
};
|
|
382
|
+
exports.readCriteria = readCriteria;
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/** The report as data. Shape and its rules: `render-json.types.ts`. */
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.renderJson = void 0;
|
|
5
|
+
const criteria_1 = require("../src/app/core/criteria/criteria");
|
|
6
|
+
const actions_1 = require("../src/app/core/findings/actions");
|
|
7
|
+
const finding_plain_1 = require("../src/app/core/findings/finding-plain");
|
|
8
|
+
const budget_advice_1 = require("../src/app/core/project/budget-advice");
|
|
9
|
+
const timing_1 = require("../src/app/core/timing/timing");
|
|
10
|
+
const comparisonOf = (report) => {
|
|
11
|
+
const comparison = report.comparison;
|
|
12
|
+
if (!comparison) {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
return {
|
|
16
|
+
baseline: comparison.baselineName,
|
|
17
|
+
date: comparison.baselineDate,
|
|
18
|
+
mode: comparison.mode,
|
|
19
|
+
boot: comparison.boot,
|
|
20
|
+
newScreens: comparison.newScreens.map(screen => screen.label),
|
|
21
|
+
goneScreens: comparison.goneScreens.map(screen => screen.label),
|
|
22
|
+
newBootPackages: comparison.newBootPackages,
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
const screensOf = (report) => report.analysis.screens.map(screen => {
|
|
26
|
+
const delta = report.comparison?.screens.get(screen.source);
|
|
27
|
+
return {
|
|
28
|
+
label: screen.label,
|
|
29
|
+
source: screen.source,
|
|
30
|
+
files: screen.files,
|
|
31
|
+
boot: screen.boot,
|
|
32
|
+
shared: screen.shared,
|
|
33
|
+
own: screen.own,
|
|
34
|
+
total: screen.total,
|
|
35
|
+
verdict: (0, criteria_1.rate)(screen.total, report.criteria.screenOk, report.criteria.screenBad),
|
|
36
|
+
delta: delta ? delta.total : null,
|
|
37
|
+
};
|
|
38
|
+
});
|
|
39
|
+
/** The two lists of lazy entries that are not screens, in the order the report shows them. */
|
|
40
|
+
const entriesOf = (entries) => entries.map(entry => ({ label: entry.label, source: entry.source, bytes: entry.bytes }));
|
|
41
|
+
const renderJson = (report, violations, anyAsked) => {
|
|
42
|
+
const { analysis, criteria } = report;
|
|
43
|
+
const asking = new Set(['budgetNone', 'budgetTooHigh', 'budgetWarnOnly']);
|
|
44
|
+
const advice = report.findings.some(finding => asking.has(finding.kind))
|
|
45
|
+
? (0, budget_advice_1.budgetAdvice)(analysis.bootRawBytes + (report.pageCssRawBytes ?? 0))
|
|
46
|
+
: null;
|
|
47
|
+
const payload = {
|
|
48
|
+
tool: 'loadline',
|
|
49
|
+
version: 1,
|
|
50
|
+
stats: report.statsName,
|
|
51
|
+
project: report.projectName,
|
|
52
|
+
mode: report.mode,
|
|
53
|
+
unit: report.unit,
|
|
54
|
+
splitSource: analysis.splitSource,
|
|
55
|
+
serverOutputs: analysis.serverOutputs,
|
|
56
|
+
pageCss: report.pageCssBytes === null ? null : { bytes: report.pageCssBytes, files: report.pageCssFiles },
|
|
57
|
+
firstTrip: report.assets?.firstTrip ?? null,
|
|
58
|
+
assets: report.assets
|
|
59
|
+
? {
|
|
60
|
+
fonts: report.assets.fonts,
|
|
61
|
+
media: report.assets.media,
|
|
62
|
+
unreferenced: report.assets.unreferenced,
|
|
63
|
+
referencesRead: report.assets.referencesRead,
|
|
64
|
+
duplicates: report.assets.duplicates,
|
|
65
|
+
contentCompared: report.assets.contentCompared,
|
|
66
|
+
inlinedBytes: report.assets.inlinedBytes,
|
|
67
|
+
inlined: report.assets.inlined,
|
|
68
|
+
}
|
|
69
|
+
: null,
|
|
70
|
+
boot: {
|
|
71
|
+
bytes: analysis.bootBytes,
|
|
72
|
+
rawBytes: analysis.bootRawBytes,
|
|
73
|
+
files: analysis.bootFiles,
|
|
74
|
+
verdict: (0, criteria_1.rate)(analysis.bootBytes, criteria.bootOk, criteria.bootBad),
|
|
75
|
+
},
|
|
76
|
+
screens: screensOf(report),
|
|
77
|
+
deferred: entriesOf(analysis.deferredBlocks),
|
|
78
|
+
groupers: entriesOf(analysis.routeGroupers),
|
|
79
|
+
data: entriesOf(analysis.lazyData),
|
|
80
|
+
shared: analysis.sharedChunks.map(chunk => ({
|
|
81
|
+
file: chunk.file,
|
|
82
|
+
name: chunk.name,
|
|
83
|
+
bytes: chunk.bytes,
|
|
84
|
+
screens: chunk.screens,
|
|
85
|
+
mainContent: chunk.mainContent,
|
|
86
|
+
})),
|
|
87
|
+
findings: report.findings.map(finding => ({
|
|
88
|
+
kind: finding.kind,
|
|
89
|
+
severity: finding.severity,
|
|
90
|
+
chip: finding.chip,
|
|
91
|
+
title: finding.title,
|
|
92
|
+
body: (0, finding_plain_1.plainText)(finding.body),
|
|
93
|
+
fix: (0, finding_plain_1.plainText)(finding.fix),
|
|
94
|
+
...(finding.saving !== undefined && { saving: finding.saving }),
|
|
95
|
+
...(finding.sources !== undefined && { sources: finding.sources }),
|
|
96
|
+
})),
|
|
97
|
+
actions: (0, actions_1.rankActions)(report.findings).map(action => ({
|
|
98
|
+
kind: action.finding.kind,
|
|
99
|
+
title: action.finding.title,
|
|
100
|
+
saving: action.saving,
|
|
101
|
+
effort: action.effort,
|
|
102
|
+
})),
|
|
103
|
+
saving: (0, actions_1.totalSaving)(analysis, report.findings),
|
|
104
|
+
timing: (0, timing_1.timingsOf)(analysis.bootBytes + (report.pageCssBytes ?? 0), analysis.bootRawBytes, analysis.startup?.waves ?? 1, criteria.latencyMs),
|
|
105
|
+
budget: advice,
|
|
106
|
+
caching: report.caching,
|
|
107
|
+
scan: report.scan,
|
|
108
|
+
deps: report.deps,
|
|
109
|
+
comparison: comparisonOf(report),
|
|
110
|
+
comparable: !report.comparisonBlocked,
|
|
111
|
+
gates: { asked: anyAsked, violations },
|
|
112
|
+
ok: violations.length === 0,
|
|
113
|
+
};
|
|
114
|
+
return JSON.stringify(payload, null, 2);
|
|
115
|
+
};
|
|
116
|
+
exports.renderJson = renderJson;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* The machine-readable contract. It is written out as an interface rather than assembled ad hoc
|
|
4
|
+
* because something else parses it: a dashboard, a bot posting on a merge request, the next job of
|
|
5
|
+
* the pipeline. Adding a field is safe; renaming or removing one is not, and `version` is what says
|
|
6
|
+
* so out loud.
|
|
7
|
+
*
|
|
8
|
+
* Every size is a number of bytes in the unit `mode` names. Ratios are fractions, not percentages.
|
|
9
|
+
*/
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* The report as Markdown, to paste into a merge request. The screens table is the very same
|
|
4
|
+
* `markdownTable` the page's "Export analysis" button writes, so a table posted by the pipeline and
|
|
5
|
+
* one copied from the page are the same table.
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.renderMarkdown = void 0;
|
|
9
|
+
const markdown_table_utils_1 = require("../src/app/core/export/markdown-table.utils");
|
|
10
|
+
const finding_plain_1 = require("../src/app/core/findings/finding-plain");
|
|
11
|
+
const ui_1 = require("../src/app/core/i18n/ui");
|
|
12
|
+
const gates_1 = require("./gates");
|
|
13
|
+
const text_1 = require("./text");
|
|
14
|
+
const renderMarkdown = (report, violations, anyAsked) => {
|
|
15
|
+
const text = text_1.CLI_TEXT[report.lang];
|
|
16
|
+
const strings = ui_1.UI[report.lang];
|
|
17
|
+
const blocks = [(0, markdown_table_utils_1.markdownTable)(report.analysis, report.comparison, strings, report.unit)];
|
|
18
|
+
if (report.findings.length > 0) {
|
|
19
|
+
blocks.push(`### ${text.headSignals}`, report.findings
|
|
20
|
+
.map(finding => `- **${finding.title}** — ${(0, finding_plain_1.plainText)(finding.body)}\n ${text.fix}: ${(0, finding_plain_1.plainText)(finding.fix)}`)
|
|
21
|
+
.join('\n'));
|
|
22
|
+
}
|
|
23
|
+
if (anyAsked) {
|
|
24
|
+
blocks.push(`### ${text.headGates}`, violations.length === 0
|
|
25
|
+
? text.passed
|
|
26
|
+
: [...(0, gates_1.violationLines)(violations).map(line => `- ${line}`), '', text.failed(violations.length)].join('\n'));
|
|
27
|
+
}
|
|
28
|
+
return `${blocks.join('\n\n')}\n`;
|
|
29
|
+
};
|
|
30
|
+
exports.renderMarkdown = renderMarkdown;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* The comment a bot leaves on a merge request, and edits next time instead of writing a new one.
|
|
4
|
+
*
|
|
5
|
+
* The marker at the top is the whole point and it is two lines of code. Without it, a branch with
|
|
6
|
+
* fifteen commits ends up with fifteen comments saying much the same thing, and by the third one
|
|
7
|
+
* nobody reads any of them. With it, `gh pr comment --edit-last` or any bot that greps for a
|
|
8
|
+
* marker updates the one that is already there.
|
|
9
|
+
*
|
|
10
|
+
* What leads is what changed — the deltas, the signals that came in, the ones that went away —
|
|
11
|
+
* because a merge request is about a change and not about a build. Everything else goes inside a
|
|
12
|
+
* `<details>`: still there, still complete, and folded up so the comment is three lines tall until
|
|
13
|
+
* somebody wants the rest.
|
|
14
|
+
*/
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.renderPrComment = exports.PR_MARKER = void 0;
|
|
17
|
+
const markdown_table_utils_1 = require("../src/app/core/export/markdown-table.utils");
|
|
18
|
+
const actions_1 = require("../src/app/core/findings/actions");
|
|
19
|
+
const finding_plain_1 = require("../src/app/core/findings/finding-plain");
|
|
20
|
+
const format_utils_1 = require("../src/app/core/format/format.utils");
|
|
21
|
+
const ui_1 = require("../src/app/core/i18n/ui");
|
|
22
|
+
const gates_1 = require("./gates");
|
|
23
|
+
const text_1 = require("./text");
|
|
24
|
+
/**
|
|
25
|
+
* What a bot looks for to find its own previous comment. It is an HTML comment, so it is invisible
|
|
26
|
+
* in the rendered page and survives being edited by hand.
|
|
27
|
+
*/
|
|
28
|
+
exports.PR_MARKER = '<!-- loadline-report -->';
|
|
29
|
+
const details = (summary, body) => `<details>\n<summary>${summary}</summary>\n\n${body}\n\n</details>`;
|
|
30
|
+
const renderPrComment = (report, violations, anyAsked) => {
|
|
31
|
+
const text = text_1.CLI_TEXT[report.lang];
|
|
32
|
+
const strings = ui_1.UI[report.lang];
|
|
33
|
+
const { analysis, comparison } = report;
|
|
34
|
+
const headline = comparison
|
|
35
|
+
? `**${(0, format_utils_1.formatBytes)(analysis.bootBytes)}** ${text.headBoot.toLowerCase()} · ${(0, format_utils_1.formatDelta)(comparison.boot.diff)} vs \`${comparison.baselineName}\``
|
|
36
|
+
: `**${(0, format_utils_1.formatBytes)(analysis.bootBytes)}** ${text.headBoot.toLowerCase()} · ${report.unit}`;
|
|
37
|
+
const lines = [exports.PR_MARKER, `### Loadline${report.projectName ? ` · ${report.projectName}` : ''}`, '', headline];
|
|
38
|
+
// Which signals came and went. It is the line the person opening the merge request reads, and
|
|
39
|
+
// the only one that makes fixing something visible.
|
|
40
|
+
if (comparison?.findingsComparable) {
|
|
41
|
+
const fixed = comparison.goneFindings.map(finding => `\`${finding.kind}\``);
|
|
42
|
+
const added = comparison.newFindings.map(finding => `\`${finding.kind}\``);
|
|
43
|
+
if (fixed.length > 0 || added.length > 0) {
|
|
44
|
+
lines.push('', [
|
|
45
|
+
fixed.length > 0 ? `✅ ${text.prFixed(fixed.length)}: ${fixed.join(', ')}` : '',
|
|
46
|
+
added.length > 0 ? `⚠️ ${text.prNew(added.length)}: ${added.join(', ')}` : '',
|
|
47
|
+
]
|
|
48
|
+
.filter(Boolean)
|
|
49
|
+
.join(' \n'));
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
const actions = (0, actions_1.rankActions)(report.findings);
|
|
53
|
+
if (actions.length > 0) {
|
|
54
|
+
const total = (0, actions_1.totalSaving)(analysis, report.findings);
|
|
55
|
+
const rows = actions.map((action, index) => `| ${index + 1} | ${action.finding.title} | ${action.saving > 0 ? (0, format_utils_1.formatBytes)(action.saving) : '—'} | ${text.effortLabel[action.effort]} |`);
|
|
56
|
+
lines.push('', `#### ${text.headActions}`, `| # | ${text.colAction} | ${text.colSaving} | ${text.colEffort} |`, '| --: | --- | --: | --- |', ...rows);
|
|
57
|
+
if (total.counted > 0) {
|
|
58
|
+
lines.push('', `_${text.totalSaving((0, format_utils_1.formatBytes)(total.bytes), (0, format_utils_1.formatBytes)(total.after), total.counted)}_`);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
if (anyAsked) {
|
|
62
|
+
lines.push('', violations.length === 0
|
|
63
|
+
? `✅ ${text.passed}`
|
|
64
|
+
: [...(0, gates_1.violationLines)(violations).map(line => `- ❌ ${line}`), '', text.failed(violations.length)].join('\n'));
|
|
65
|
+
}
|
|
66
|
+
// Everything the report says, folded. Nothing is dropped: the rule is to group, never to trim.
|
|
67
|
+
const rest = [
|
|
68
|
+
(0, markdown_table_utils_1.markdownTable)(analysis, comparison, strings, report.unit),
|
|
69
|
+
'',
|
|
70
|
+
`#### ${text.headSignals}`,
|
|
71
|
+
...report.findings.map(finding => `- **${finding.title}** — ${(0, finding_plain_1.plainText)(finding.body)}\n ${text.fix}: ${(0, finding_plain_1.plainText)(finding.fix)}`),
|
|
72
|
+
].join('\n');
|
|
73
|
+
lines.push('', details(text.prDetails, rest));
|
|
74
|
+
return `${lines.join('\n')}\n`;
|
|
75
|
+
};
|
|
76
|
+
exports.renderPrComment = renderPrComment;
|