From 4da518cd2f71168574c7b07a168d7eacbe83a085 Mon Sep 17 00:00:00 2001 From: Florian Sylvain Date: Tue, 9 Sep 2025 18:26:11 +0200 Subject: [PATCH] feat: enhance loadAndMinify function to handle binary files and streamline content processing --- server.js | 82 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 50 insertions(+), 32 deletions(-) diff --git a/server.js b/server.js index 0880bac..9a09150 100644 --- a/server.js +++ b/server.js @@ -91,7 +91,7 @@ function minifyCSS(src) { .replace(/\s*([{}:;,>+~\(\)\[\]'"*\/])\s*/g, "$1") .replace(/;}/g, "}") .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( /rgb\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)/g, @@ -426,40 +426,58 @@ function generateSRIHash(content) { async function loadAndMinify(relPath, minify) { const abs = path.join(ROOT, relPath); 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") { - const cssPath = path.join(ROOT, "style.css"); - if (fs.existsSync(cssPath)) { - const cssContent = await fs.promises.readFile(cssPath, "utf8"); - const minifiedCSS = minifyCSS(cssContent); + let body; + let processedContent; - processedContent = processedContent.replace( - /]*href=["']style\.css["'][^>]*>/gi, - `` - ); + if (isBinaryFile) { + body = src; + 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( + /]*href=["']style\.css["'][^>]*>/gi, + `` + ); + } + + 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( + /]*src=["']script\.js["'][^>]*><\/script>/gi, + `` + ); + } + + extractInlineContent(processedContent); } - 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( - /]*src=["']script\.js["'][^>]*><\/script>/gi, - `` - ); - } - - extractInlineContent(processedContent); + body = minify + ? Buffer.from(minify(processedContent), "utf8") + : Buffer.from(processedContent, "utf8"); } - const body = minify - ? Buffer.from(minify(processedContent), "utf8") - : Buffer.from(processedContent, "utf8"); - let sriHash = null; if (relPath === "style.css" || relPath === "script.js") { sriHash = generateSRIHash(body.toString("utf8")); @@ -470,16 +488,16 @@ async function loadAndMinify(relPath, minify) { const stats = await fs.promises.stat(abs); const originalSize = src.length; - const minifiedSize = body.length; + const processedSize = body.length; const brSize = compressed.br?.length || 0; const gzSize = compressed.gz?.length || 0; console.log(`${relPath}:`); console.log(` Original: ${originalSize} bytes`); - if (minify) + if (!isBinaryFile && minify) console.log( - ` Minified: ${minifiedSize} bytes (${( - ((originalSize - minifiedSize) / originalSize) * + ` Minified: ${processedSize} bytes (${( + ((originalSize - processedSize) / originalSize) * 100 ).toFixed(1)}% reduction)` );