@dcaldeira/tooling 0.1.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +34 -34
- package/checks/a11y.mjs +206 -206
- package/checks/architecture.mjs +137 -137
- package/checks/audit.mjs +121 -121
- package/checks/bundle.mjs +128 -126
- package/checks/dependencies.mjs +277 -277
- package/checks/duplication.mjs +135 -0
- package/checks/lint-scss.mjs +363 -349
- package/checks/package.mjs +30 -30
- package/checks/playwright-summary.mjs +53 -53
- package/checks/sass.mjs +67 -67
- package/checks/scan-secrets.mjs +58 -58
- package/checks/shared.mjs +86 -86
- package/checks/translations.mjs +55 -55
- package/checks/visual-compare.mjs +148 -148
- package/checks/visual-report-html.mjs +470 -470
- package/checks/visual-report.mjs +13 -13
- package/config.schema.json +999 -927
- package/configs/eslint.mjs +27 -27
- package/configs/knip.json +11 -11
- package/configs/playwright.mjs +15 -15
- package/configs/vitest.mjs +16 -16
- package/package.json +105 -105
- package/release/finalize.mjs +65 -65
- package/release/github.mjs +64 -64
- package/release/prepare.mjs +30 -30
- package/runner/browsers.mjs +73 -73
- package/runner/cache.mjs +121 -121
- package/runner/ci-plan.mjs +54 -54
- package/runner/cli.mjs +250 -248
- package/runner/config.mjs +148 -148
- package/runner/docs-install.mjs +93 -93
- package/runner/execute.mjs +298 -286
- package/runner/generate.mjs +122 -122
- package/runner/git.mjs +122 -122
- package/runner/graph.mjs +250 -243
- package/runner/insights.mjs +313 -313
- package/runner/provision.mjs +53 -53
- package/runner/repo-standard.mjs +104 -0
- package/runner/report.mjs +35 -35
- package/runner/serve-static.mjs +76 -76
- package/runner/setup.mjs +241 -237
- package/runner/summary.mjs +112 -112
- package/runner/watch.mjs +82 -82
- package/templates/ci.yml +25 -25
- package/templates/dependabot-merge.yml +13 -13
- package/templates/dependabot.yml +26 -26
- package/templates/pre-push +13 -13
- package/templates/release.yml +23 -23
package/checks/bundle.mjs
CHANGED
|
@@ -1,126 +1,128 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
import { readdirSync, readFileSync } from 'node:fs';
|
|
3
|
-
import { join, posix, relative } from 'node:path';
|
|
4
|
-
import { pathToFileURL } from 'node:url';
|
|
5
|
-
import { gzipSync } from 'node:zlib';
|
|
6
|
-
import { loadConfig, inside } from '../runner/config.mjs';
|
|
7
|
-
import { initSync, parse } from 'es-module-lexer';
|
|
8
|
-
import { writeDetails } from './shared.mjs';
|
|
9
|
-
|
|
10
|
-
initSync();
|
|
11
|
-
|
|
12
|
-
const jsFile = (file) => /\.[cm]?js$/.test(file);
|
|
13
|
-
const walk = (directory) =>
|
|
14
|
-
readdirSync(directory, { withFileTypes: true }).flatMap((entry) =>
|
|
15
|
-
entry.isDirectory() ? walk(join(directory, entry.name)) : [join(directory, entry.name)],
|
|
16
|
-
);
|
|
17
|
-
|
|
18
|
-
// The module lexer handles minified ESM, escapes, comments, strings and re-exports.
|
|
19
|
-
export function staticImports(source) {
|
|
20
|
-
const [imports] = parse(source);
|
|
21
|
-
return imports.filter((item) => item.d === -1 && item.n).map((item) => item.n);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export function analyseBundle(root, budget) {
|
|
25
|
-
const dist = inside(root, budget.dist);
|
|
26
|
-
const chunks = walk(dist)
|
|
27
|
-
.map((file) => relative(dist, file).replaceAll('\\', '/'))
|
|
28
|
-
.filter(jsFile);
|
|
29
|
-
const available = new Set(chunks);
|
|
30
|
-
const resolveAsset = (url, importer = 'index.html') => {
|
|
31
|
-
if (/^(?:[a-z][a-z\d+.-]*:|\/\/|#)/i.test(url)) return null;
|
|
32
|
-
let path;
|
|
33
|
-
try {
|
|
34
|
-
path = decodeURIComponent(url.split(/[?#]/)[0]);
|
|
35
|
-
} catch {
|
|
36
|
-
throw new Error(`Invalid asset URL: ${url}`);
|
|
37
|
-
}
|
|
38
|
-
if (!jsFile(path)) return null;
|
|
39
|
-
const local = posix.normalize(
|
|
40
|
-
path.startsWith('/') ? path.slice(1) : posix.join(posix.dirname(importer), path),
|
|
41
|
-
);
|
|
42
|
-
if (available.has(local)) return local;
|
|
43
|
-
// Match the build's real asset path after a deployment prefix such as /my-app/.
|
|
44
|
-
if (path.startsWith('/')) {
|
|
45
|
-
const matches = chunks.filter((file) => path.endsWith('/' + file));
|
|
46
|
-
if (matches.length === 1) return matches[0];
|
|
47
|
-
}
|
|
48
|
-
throw new Error(`Missing local JavaScript asset: ${url} (from ${importer})`);
|
|
49
|
-
};
|
|
50
|
-
const html = readFileSync(join(dist, 'index.html'), 'utf8').replace(/<!--[\s\S]*?-->/g, '');
|
|
51
|
-
const entries = new Set();
|
|
52
|
-
for (const tag of html.matchAll(/<(script|link)\b[^>]*>/gi)) {
|
|
53
|
-
const attrs = Object.fromEntries(
|
|
54
|
-
[...tag[0].matchAll(/([\w-]+)\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+))/g)].map((m) => [
|
|
55
|
-
m[1].toLowerCase(),
|
|
56
|
-
m[2] ?? m[3] ?? m[4],
|
|
57
|
-
]),
|
|
58
|
-
);
|
|
59
|
-
if (tag[1].toLowerCase() === 'link' && attrs.rel?.toLowerCase() !== 'modulepreload') continue;
|
|
60
|
-
const url = attrs.src ?? attrs.href;
|
|
61
|
-
if (!url) continue;
|
|
62
|
-
const asset = resolveAsset(url);
|
|
63
|
-
if (asset) entries.add(asset);
|
|
64
|
-
}
|
|
65
|
-
if (!entries.size) throw new Error('No local initial JavaScript found in dist/index.html');
|
|
66
|
-
const initial = new Set();
|
|
67
|
-
const visit = (file) => {
|
|
68
|
-
if (initial.has(file)) return;
|
|
69
|
-
initial.add(file);
|
|
70
|
-
for (const specifier of staticImports(readFileSync(join(dist, file), 'utf8'))) {
|
|
71
|
-
if (!specifier.startsWith('.') && !specifier.startsWith('/')) continue;
|
|
72
|
-
const dependency = resolveAsset(specifier, file);
|
|
73
|
-
if (dependency) visit(dependency);
|
|
74
|
-
}
|
|
75
|
-
};
|
|
76
|
-
entries.forEach(visit);
|
|
77
|
-
const gzip = (file) => gzipSync(readFileSync(join(dist, file))).length;
|
|
78
|
-
const initialKB = [...initial].reduce((sum, file) => sum + gzip(file), 0) / 1024;
|
|
79
|
-
const errors = [];
|
|
80
|
-
if (initialKB > budget.initialKB)
|
|
81
|
-
errors.push(`initial JS ${initialKB.toFixed(1)} KB > ${budget.initialKB} KB`);
|
|
82
|
-
for (const file of chunks) {
|
|
83
|
-
if (initial.has(file)) continue;
|
|
84
|
-
let vendor;
|
|
85
|
-
try {
|
|
86
|
-
const map = JSON.parse(readFileSync(join(dist, `${file}.map`), 'utf8'));
|
|
87
|
-
const weights = new Map();
|
|
88
|
-
map.sources.forEach((source, i) => {
|
|
89
|
-
const hit = (budget.vendors ?? []).find(([pkg]) =>
|
|
90
|
-
source.replaceAll('\\', '/').includes(`node_modules/${pkg}/`),
|
|
91
|
-
);
|
|
92
|
-
if (hit) weights.set(hit, (weights.get(hit) ?? 0) + (map.sourcesContent?.[i]?.length ?? 0));
|
|
93
|
-
});
|
|
94
|
-
const total = (map.sourcesContent ?? []).reduce(
|
|
95
|
-
(sum, content) => sum + (content?.length ?? 0),
|
|
96
|
-
0,
|
|
97
|
-
);
|
|
98
|
-
const best = [...weights].sort((a, b) => b[1] - a[1])[0];
|
|
99
|
-
if (best && best[1] > total / 2) vendor = best[0];
|
|
100
|
-
} catch (error) {
|
|
101
|
-
if (error.code !== 'ENOENT') throw error;
|
|
102
|
-
}
|
|
103
|
-
const size = gzip(file) / 1024;
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
const
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { readdirSync, readFileSync } from 'node:fs';
|
|
3
|
+
import { join, posix, relative } from 'node:path';
|
|
4
|
+
import { pathToFileURL } from 'node:url';
|
|
5
|
+
import { gzipSync } from 'node:zlib';
|
|
6
|
+
import { loadConfig, inside } from '../runner/config.mjs';
|
|
7
|
+
import { initSync, parse } from 'es-module-lexer';
|
|
8
|
+
import { writeDetails } from './shared.mjs';
|
|
9
|
+
|
|
10
|
+
initSync();
|
|
11
|
+
|
|
12
|
+
const jsFile = (file) => /\.[cm]?js$/.test(file);
|
|
13
|
+
const walk = (directory) =>
|
|
14
|
+
readdirSync(directory, { withFileTypes: true }).flatMap((entry) =>
|
|
15
|
+
entry.isDirectory() ? walk(join(directory, entry.name)) : [join(directory, entry.name)],
|
|
16
|
+
);
|
|
17
|
+
|
|
18
|
+
// The module lexer handles minified ESM, escapes, comments, strings and re-exports.
|
|
19
|
+
export function staticImports(source) {
|
|
20
|
+
const [imports] = parse(source);
|
|
21
|
+
return imports.filter((item) => item.d === -1 && item.n).map((item) => item.n);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function analyseBundle(root, budget) {
|
|
25
|
+
const dist = inside(root, budget.dist);
|
|
26
|
+
const chunks = walk(dist)
|
|
27
|
+
.map((file) => relative(dist, file).replaceAll('\\', '/'))
|
|
28
|
+
.filter(jsFile);
|
|
29
|
+
const available = new Set(chunks);
|
|
30
|
+
const resolveAsset = (url, importer = 'index.html') => {
|
|
31
|
+
if (/^(?:[a-z][a-z\d+.-]*:|\/\/|#)/i.test(url)) return null;
|
|
32
|
+
let path;
|
|
33
|
+
try {
|
|
34
|
+
path = decodeURIComponent(url.split(/[?#]/)[0]);
|
|
35
|
+
} catch {
|
|
36
|
+
throw new Error(`Invalid asset URL: ${url}`);
|
|
37
|
+
}
|
|
38
|
+
if (!jsFile(path)) return null;
|
|
39
|
+
const local = posix.normalize(
|
|
40
|
+
path.startsWith('/') ? path.slice(1) : posix.join(posix.dirname(importer), path),
|
|
41
|
+
);
|
|
42
|
+
if (available.has(local)) return local;
|
|
43
|
+
// Match the build's real asset path after a deployment prefix such as /my-app/.
|
|
44
|
+
if (path.startsWith('/')) {
|
|
45
|
+
const matches = chunks.filter((file) => path.endsWith('/' + file));
|
|
46
|
+
if (matches.length === 1) return matches[0];
|
|
47
|
+
}
|
|
48
|
+
throw new Error(`Missing local JavaScript asset: ${url} (from ${importer})`);
|
|
49
|
+
};
|
|
50
|
+
const html = readFileSync(join(dist, 'index.html'), 'utf8').replace(/<!--[\s\S]*?-->/g, '');
|
|
51
|
+
const entries = new Set();
|
|
52
|
+
for (const tag of html.matchAll(/<(script|link)\b[^>]*>/gi)) {
|
|
53
|
+
const attrs = Object.fromEntries(
|
|
54
|
+
[...tag[0].matchAll(/([\w-]+)\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+))/g)].map((m) => [
|
|
55
|
+
m[1].toLowerCase(),
|
|
56
|
+
m[2] ?? m[3] ?? m[4],
|
|
57
|
+
]),
|
|
58
|
+
);
|
|
59
|
+
if (tag[1].toLowerCase() === 'link' && attrs.rel?.toLowerCase() !== 'modulepreload') continue;
|
|
60
|
+
const url = attrs.src ?? attrs.href;
|
|
61
|
+
if (!url) continue;
|
|
62
|
+
const asset = resolveAsset(url);
|
|
63
|
+
if (asset) entries.add(asset);
|
|
64
|
+
}
|
|
65
|
+
if (!entries.size) throw new Error('No local initial JavaScript found in dist/index.html');
|
|
66
|
+
const initial = new Set();
|
|
67
|
+
const visit = (file) => {
|
|
68
|
+
if (initial.has(file)) return;
|
|
69
|
+
initial.add(file);
|
|
70
|
+
for (const specifier of staticImports(readFileSync(join(dist, file), 'utf8'))) {
|
|
71
|
+
if (!specifier.startsWith('.') && !specifier.startsWith('/')) continue;
|
|
72
|
+
const dependency = resolveAsset(specifier, file);
|
|
73
|
+
if (dependency) visit(dependency);
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
entries.forEach(visit);
|
|
77
|
+
const gzip = (file) => gzipSync(readFileSync(join(dist, file))).length;
|
|
78
|
+
const initialKB = [...initial].reduce((sum, file) => sum + gzip(file), 0) / 1024;
|
|
79
|
+
const errors = [];
|
|
80
|
+
if (initialKB > budget.initialKB)
|
|
81
|
+
errors.push(`initial JS ${initialKB.toFixed(1)} KB > ${budget.initialKB} KB`);
|
|
82
|
+
for (const file of chunks) {
|
|
83
|
+
if (initial.has(file)) continue;
|
|
84
|
+
let vendor;
|
|
85
|
+
try {
|
|
86
|
+
const map = JSON.parse(readFileSync(join(dist, `${file}.map`), 'utf8'));
|
|
87
|
+
const weights = new Map();
|
|
88
|
+
map.sources.forEach((source, i) => {
|
|
89
|
+
const hit = (budget.vendors ?? []).find(([pkg]) =>
|
|
90
|
+
source.replaceAll('\\', '/').includes(`node_modules/${pkg}/`),
|
|
91
|
+
);
|
|
92
|
+
if (hit) weights.set(hit, (weights.get(hit) ?? 0) + (map.sourcesContent?.[i]?.length ?? 0));
|
|
93
|
+
});
|
|
94
|
+
const total = (map.sourcesContent ?? []).reduce(
|
|
95
|
+
(sum, content) => sum + (content?.length ?? 0),
|
|
96
|
+
0,
|
|
97
|
+
);
|
|
98
|
+
const best = [...weights].sort((a, b) => b[1] - a[1])[0];
|
|
99
|
+
if (best && best[1] > total / 2) vendor = best[0];
|
|
100
|
+
} catch (error) {
|
|
101
|
+
if (error.code !== 'ENOENT') throw error;
|
|
102
|
+
}
|
|
103
|
+
const size = gzip(file) / 1024;
|
|
104
|
+
// A file with no source map, such as a pre-built worker, gets its allowance by path.
|
|
105
|
+
const allowed = vendor ?? (budget.files ?? []).find(([part]) => file.includes(part));
|
|
106
|
+
const cap = allowed ? allowed[2] : budget.chunkKB;
|
|
107
|
+
if (size > cap)
|
|
108
|
+
errors.push(
|
|
109
|
+
`${file} (${allowed ? allowed[1] : 'route chunk'}) ${size.toFixed(1)} KB > ${cap} KB`,
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
return { initialKB, initial: [...initial], chunks: chunks.length, errors };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
|
|
116
|
+
const root = process.env.TOOLING_ROOT ?? process.cwd();
|
|
117
|
+
const { config } = loadConfig(root);
|
|
118
|
+
if (!config.budgets?.bundle) throw new Error('Configure budgets.bundle');
|
|
119
|
+
const result = analyseBundle(root, config.budgets.bundle);
|
|
120
|
+
writeDetails({ bundle: result });
|
|
121
|
+
console.log(
|
|
122
|
+
`initial JS: ${result.initialKB.toFixed(1)} KB gzip across ${result.initial.length} files; ${result.chunks} chunks checked`,
|
|
123
|
+
);
|
|
124
|
+
if (result.errors.length) {
|
|
125
|
+
console.error(result.errors.join('\n'));
|
|
126
|
+
process.exitCode = 1;
|
|
127
|
+
} else console.log('Bundle budgets OK.');
|
|
128
|
+
}
|