Repository navigation
Expand file tree
/
Copy pathmeasure-bundle-size.js
More file actions
74 lines (63 loc) · 2.32 KB
/
Copy pathmeasure-bundle-size.js
File metadata and controls
74 lines (63 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/* eslint-disable @typescript-eslint/no-require-imports */
const fs = require('fs');
const path = require('path');
const zlib = require('zlib');
function normalizePath(filePath) {
// Replace build ID folder (e.g. static/oRr73JL35eL4J_9y-Ef4Q/_buildManifest.js -> static/[build-id]/_buildManifest.js)
let normalized = filePath.replace(
/static\/[^/]+\/(_buildManifest|_ssgManifest)\.js$/,
'static/[build-id]/$1.js'
);
// Replace hex hashes preceded by dash or dot (e.g. -711ef29bc66f648c or .dac65e4204a83905)
normalized = normalized.replace(/[-.][a-f0-9]{8,24}(?=\.js|\.css)/gi, '-[hash]');
// Replace pure hex filenames (e.g. static/css/9186d3cc0904cc35.css -> static/css/[hash].css)
normalized = normalized.replace(/\/([a-f0-9]{8,24})\.(js|css)$/gi, '/[hash].$2');
return normalized;
}
function walkDir(dir, fileList = []) {
if (!fs.existsSync(dir)) return fileList;
const files = fs.readdirSync(dir);
for (const file of files) {
const filePath = path.join(dir, file);
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
walkDir(filePath, fileList);
} else {
if (file.endsWith('.js') || file.endsWith('.css')) {
fileList.push(filePath);
}
}
}
return fileList;
}
function measureBundle() {
const staticDir = path.join(process.cwd(), '.next', 'static');
if (!fs.existsSync(staticDir)) {
console.error('No .next/static directory found. Make sure to run npm run build first.');
process.exit(1);
}
const files = walkDir(staticDir);
const results = {};
for (const file of files) {
const relativePath = path.relative(path.join(process.cwd(), '.next'), file).replace(/\\/g, '/');
const content = fs.readFileSync(file);
const size = content.length;
const gzipped = zlib.gzipSync(content).length;
const norm = normalizePath(relativePath);
if (!results[norm]) {
results[norm] = { size: 0, gzipped: 0, filesCount: 0 };
}
results[norm].size += size;
results[norm].gzipped += gzipped;
results[norm].filesCount += 1;
}
const outputJson = JSON.stringify(results, null, 2);
const outputPath = process.argv[2];
if (outputPath) {
fs.writeFileSync(outputPath, outputJson, 'utf8');
console.log(`Saved bundle size measurements to ${outputPath}`);
} else {
console.log(outputJson);
}
}
measureBundle();