feat: enhance loadAndMinify function to handle binary files and streamline content processing

This commit is contained in:
Florian Sylvain
2025-09-09 18:26:11 +02:00
parent 5a21539931
commit 4da518cd2f
+50 -32
View File
@@ -91,7 +91,7 @@ function minifyCSS(src) {
.replace(/\s*([{}:;,>+~\(\)\[\]'"*\/])\s*/g, "$1") .replace(/\s*([{}:;,>+~\(\)\[\]'"*\/])\s*/g, "$1")
.replace(/;}/g, "}") .replace(/;}/g, "}")
.replace(/\b0+(px|em|rem|vh|vw|%|s|ms)\b/g, "0") .replace(/\b0+(px|em|rem|vh|vw|%|s|ms)\b/g, "0")
.replace(/\b0+\.(\d+)/g, ".$1") // 0.5 -> .5 .replace(/\b0+\.(\d+)/g, ".$1")
.replace(/#([0-9a-fA-F])\1([0-9a-fA-F])\2([0-9a-fA-F])\3\b/g, "#$1$2$3") .replace(/#([0-9a-fA-F])\1([0-9a-fA-F])\2([0-9a-fA-F])\3\b/g, "#$1$2$3")
.replace( .replace(
/rgb\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)/g, /rgb\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)/g,
@@ -426,40 +426,58 @@ function generateSRIHash(content) {
async function loadAndMinify(relPath, minify) { async function loadAndMinify(relPath, minify) {
const abs = path.join(ROOT, relPath); const abs = path.join(ROOT, relPath);
const src = await fs.promises.readFile(abs); const src = await fs.promises.readFile(abs);
const originalContent = src.toString("utf8");
let processedContent = originalContent; const ext = path.extname(relPath).toLowerCase();
const isBinaryFile = [
".webp",
".png",
".jpg",
".jpeg",
".gif",
".ico",
].includes(ext);
if (relPath === "index.html") { let body;
const cssPath = path.join(ROOT, "style.css"); let processedContent;
if (fs.existsSync(cssPath)) {
const cssContent = await fs.promises.readFile(cssPath, "utf8");
const minifiedCSS = minifyCSS(cssContent);
processedContent = processedContent.replace( if (isBinaryFile) {
/<link[^>]*href=["']style\.css["'][^>]*>/gi, body = src;
`<style>${minifiedCSS}</style>` processedContent = null;
); } else {
const originalContent = src.toString("utf8");
processedContent = originalContent;
if (relPath === "index.html") {
const cssPath = path.join(ROOT, "style.css");
if (fs.existsSync(cssPath)) {
const cssContent = await fs.promises.readFile(cssPath, "utf8");
const minifiedCSS = minifyCSS(cssContent);
processedContent = processedContent.replace(
/<link[^>]*href=["']style\.css["'][^>]*>/gi,
`<style>${minifiedCSS}</style>`
);
}
const jsPath = path.join(ROOT, "script.js");
if (fs.existsSync(jsPath)) {
const jsContent = await fs.promises.readFile(jsPath, "utf8");
const minifiedJS = minifyJS(jsContent);
processedContent = processedContent.replace(
/<script[^>]*src=["']script\.js["'][^>]*><\/script>/gi,
`<script>${minifiedJS}</script>`
);
}
extractInlineContent(processedContent);
} }
const jsPath = path.join(ROOT, "script.js"); body = minify
if (fs.existsSync(jsPath)) { ? Buffer.from(minify(processedContent), "utf8")
const jsContent = await fs.promises.readFile(jsPath, "utf8"); : Buffer.from(processedContent, "utf8");
const minifiedJS = minifyJS(jsContent);
processedContent = processedContent.replace(
/<script[^>]*src=["']script\.js["'][^>]*><\/script>/gi,
`<script>${minifiedJS}</script>`
);
}
extractInlineContent(processedContent);
} }
const body = minify
? Buffer.from(minify(processedContent), "utf8")
: Buffer.from(processedContent, "utf8");
let sriHash = null; let sriHash = null;
if (relPath === "style.css" || relPath === "script.js") { if (relPath === "style.css" || relPath === "script.js") {
sriHash = generateSRIHash(body.toString("utf8")); sriHash = generateSRIHash(body.toString("utf8"));
@@ -470,16 +488,16 @@ async function loadAndMinify(relPath, minify) {
const stats = await fs.promises.stat(abs); const stats = await fs.promises.stat(abs);
const originalSize = src.length; const originalSize = src.length;
const minifiedSize = body.length; const processedSize = body.length;
const brSize = compressed.br?.length || 0; const brSize = compressed.br?.length || 0;
const gzSize = compressed.gz?.length || 0; const gzSize = compressed.gz?.length || 0;
console.log(`${relPath}:`); console.log(`${relPath}:`);
console.log(` Original: ${originalSize} bytes`); console.log(` Original: ${originalSize} bytes`);
if (minify) if (!isBinaryFile && minify)
console.log( console.log(
` Minified: ${minifiedSize} bytes (${( ` Minified: ${processedSize} bytes (${(
((originalSize - minifiedSize) / originalSize) * ((originalSize - processedSize) / originalSize) *
100 100
).toFixed(1)}% reduction)` ).toFixed(1)}% reduction)`
); );