Skip to content

Commit 6ea1dc1

Browse files
committed
[wip]
1 parent 2085beb commit 6ea1dc1

File tree

3 files changed

+39
-65
lines changed

3 files changed

+39
-65
lines changed

scripts/compare-builds/web.mjs

Lines changed: 36 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const formatBytes = bytes => {
1515
if (!bytes) {
1616
return '0 B';
1717
}
18+
1819
const i = Math.floor(Math.log(Math.abs(bytes)) / Math.log(1024));
1920
return `${(bytes / Math.pow(1024, i)).toFixed(2)} ${UNITS[i]}`;
2021
};
@@ -45,37 +46,17 @@ const getDirectoryStats = async dir => {
4546
return new Map(entries);
4647
};
4748

48-
/**
49-
* Gets the extension of a file
50-
* @param {string} file - Filename
51-
* @returns {string} File extension (without dot)
52-
*/
53-
const getExtension = file => file.split('.').pop();
54-
55-
/**
56-
* Gets the base name of a file (without extension)
57-
* @param {string} file - Filename
58-
* @returns {string} Base filename
59-
*/
60-
const getBaseName = file => file.slice(0, file.lastIndexOf('.') || file.length);
61-
6249
/**
6350
* Generates a table row for a file
6451
* @param {string} file - Filename
65-
* @param {number} [baseSize] - Base size in bytes
66-
* @param {number} [headSize] - Head size in bytes
52+
* @param {number} baseSize - Base size in bytes
53+
* @param {number} headSize - Head size in bytes
6754
* @returns {string} Markdown table row
6855
*/
6956
const generateRow = (file, baseSize, headSize) => {
70-
const baseCol = baseSize != null ? formatBytes(baseSize) : '-';
71-
const headCol = headSize != null ? formatBytes(headSize) : '-';
72-
73-
let diffCol = 'Added';
74-
if (baseSize != null && headSize != null) {
75-
diffCol = formatDiff(baseSize, headSize);
76-
} else if (baseSize != null) {
77-
diffCol = 'Removed';
78-
}
57+
const baseCol = formatBytes(baseSize);
58+
const headCol = formatBytes(headSize);
59+
const diffCol = formatDiff(baseSize, headSize);
7960

8061
return `| \`${file}\` | ${baseCol} | ${headCol} | ${diffCol} |`;
8162
};
@@ -104,40 +85,48 @@ const generateTable = (files, baseStats, headStats) => {
10485
const details = (summary, content) =>
10586
`<details>\n<summary>${summary}</summary>\n\n${content}\n\n</details>`;
10687

107-
async function main() {
108-
const [baseStats, headStats] = await Promise.all(
109-
[BASE, HEAD].map(getDirectoryStats)
110-
);
88+
const [baseStats, headStats] = await Promise.all(
89+
[BASE, HEAD].map(getDirectoryStats)
90+
);
11191

112-
const allFiles = Array.from(
113-
new Set([...baseStats.keys(), ...headStats.keys()])
114-
);
92+
const allFiles = Array.from(
93+
new Set([...baseStats.keys(), ...headStats.keys()])
94+
);
11595

116-
if (!allFiles.length) {
117-
return;
118-
}
96+
// Filter to only changed files (exist in both and have different sizes)
97+
const changedFiles = allFiles.filter(
98+
f =>
99+
baseStats.has(f) &&
100+
headStats.has(f) &&
101+
baseStats.get(f) !== headStats.get(f)
102+
);
119103

104+
if (changedFiles.length) {
120105
// Separate HTML/JS pairs from other files
121106
const pairs = [];
122107
const other = [];
123108
const processed = new Set();
124109

125-
for (const file of allFiles) {
110+
for (const file of changedFiles) {
126111
if (processed.has(file)) {
127112
continue;
128113
}
129114

130-
const basename = getBaseName(file);
131-
const hasHtml = allFiles.some(
132-
f => getBaseName(f) === basename && getExtension(f) === 'html'
115+
const basename = path.basename(file, path.extname(file));
116+
const hasHtml = changedFiles.some(
117+
f =>
118+
path.basename(f, path.extname(f)) === basename &&
119+
path.extname(f) === '.html'
133120
);
134-
const hasJs = allFiles.some(
135-
f => getBaseName(f) === basename && getExtension(f) === 'js'
121+
const hasJs = changedFiles.some(
122+
f =>
123+
path.basename(f, path.extname(f)) === basename &&
124+
path.extname(f) === '.js'
136125
);
137126

138127
if (hasHtml && hasJs) {
139-
allFiles
140-
.filter(f => getBaseName(f) === basename)
128+
changedFiles
129+
.filter(f => path.basename(f, path.extname(f)) === basename)
141130
.forEach(f => {
142131
pairs.push(f);
143132
processed.add(f);
@@ -151,29 +140,15 @@ async function main() {
151140
pairs.sort();
152141
other.sort();
153142

154-
// Generate report sections
155-
const sections = [];
143+
console.log('## Web Generator\n');
144+
console.log(generateTable(other, baseStats, headStats));
156145

157146
if (pairs.length) {
158-
sections.push(
147+
console.log(
159148
details(
160-
`HTML/JS Pairs (${pairs.length})`,
149+
`Pages (${pairs.length / 2})`,
161150
generateTable(pairs, baseStats, headStats)
162151
)
163152
);
164153
}
165-
166-
if (other.length) {
167-
sections.push(
168-
details(
169-
`Other Files (${other.length})`,
170-
generateTable(other, baseStats, headStats)
171-
)
172-
);
173-
}
174-
175-
console.log('## Web Generator');
176-
console.log(sections.join('\n\n'));
177154
}
178-
179-
main();

src/generators/web/constants.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export const JSX_IMPORTS = {
8585
* Specification rules for resource hints like prerendering and prefetching.
8686
* @see https://developer.mozilla.org/en-US/docs/Web/API/Speculation_Rules_API
8787
*/
88-
export const SPECULATION_RULES = {
88+
export const SPECULATION_RULES = JSON.stringify({
8989
// Eagerly prefetch all links that point to the API docs themselves
9090
// in a moderate eagerness to improve resource loading
9191
prefetch: [{ where: { href_matches: '/*' }, eagerness: 'eager' }],
@@ -94,4 +94,4 @@ export const SPECULATION_RULES = {
9494
// These will be done in a moderate eagerness (hover, likely next navigation)
9595
{ where: { selector_matches: '[rel~=prefetch]' }, eagerness: 'moderate' },
9696
],
97-
};
97+
});

src/generators/web/utils/processing.mjs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ export async function processJSXEntries(
106106
]);
107107

108108
const titleSuffix = `Node.js v${version.version} Documentation`;
109-
const speculationRulesString = JSON.stringify(SPECULATION_RULES, null, 2);
110109

111110
// Step 3: Create final HTML (could be parallelized in workers)
112111
const results = entries.map(({ data: { api, heading } }) => {
@@ -118,7 +117,7 @@ export async function processJSXEntries(
118117
.replace('{{dehydrated}}', serverBundle.get(fileName) ?? '')
119118
.replace('{{importMap}}', clientBundle.importMap ?? '')
120119
.replace('{{entrypoint}}', `./${fileName}?${randomUUID()}`)
121-
.replace('{{speculationRules}}', speculationRulesString);
120+
.replace('{{speculationRules}}', SPECULATION_RULES);
122121

123122
// Minify HTML (input must be a Buffer)
124123
const finalHTMLBuffer = HTMLMinifier.minify(Buffer.from(renderedHtml), {});

0 commit comments

Comments
 (0)