blob: 4c4886836e1913e0dc4a49e7968947080c932d1a (
plain)
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
|
import { readdirSync, readFileSync, statSync, writeFileSync } from "node:fs";
import { join } from "node:path";
const distRoot = "docs/.vitepress/dist";
const preloadStylesheetRelPattern = /rel="preload stylesheet"/g;
const stylesheetAsPattern = /(<link[^>]*rel="stylesheet"[^>]*?)\s+as="style"/g;
function walk(dir, files = []) {
for (const entry of readdirSync(dir)) {
const fullPath = join(dir, entry);
const stat = statSync(fullPath);
if (stat.isDirectory()) {
walk(fullPath, files);
} else if (fullPath.endsWith(".html")) {
files.push(fullPath);
}
}
return files;
}
let updated = 0;
for (const filePath of walk(distRoot)) {
const original = readFileSync(filePath, "utf8");
let fixed = original.replace(preloadStylesheetRelPattern, 'rel="stylesheet"');
fixed = fixed.replace(stylesheetAsPattern, "$1");
if (fixed !== original) {
writeFileSync(filePath, fixed, "utf8");
updated += 1;
}
}
console.log(`[fix-css-link-rel] Updated ${updated} HTML files.`);
|