mirror of
https://github.com/Floriansylvain/compact-portfolio.git
synced 2026-08-19 11:43:19 +02:00
feat: enhance loadAndMinify function to handle binary files and streamline content processing
This commit is contained in:
@@ -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,9 +426,26 @@ 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);
|
||||||
|
|
||||||
|
let body;
|
||||||
|
let processedContent;
|
||||||
|
|
||||||
|
if (isBinaryFile) {
|
||||||
|
body = src;
|
||||||
|
processedContent = null;
|
||||||
|
} else {
|
||||||
|
const originalContent = src.toString("utf8");
|
||||||
|
processedContent = originalContent;
|
||||||
|
|
||||||
if (relPath === "index.html") {
|
if (relPath === "index.html") {
|
||||||
const cssPath = path.join(ROOT, "style.css");
|
const cssPath = path.join(ROOT, "style.css");
|
||||||
@@ -456,9 +473,10 @@ async function loadAndMinify(relPath, minify) {
|
|||||||
extractInlineContent(processedContent);
|
extractInlineContent(processedContent);
|
||||||
}
|
}
|
||||||
|
|
||||||
const body = minify
|
body = minify
|
||||||
? Buffer.from(minify(processedContent), "utf8")
|
? Buffer.from(minify(processedContent), "utf8")
|
||||||
: Buffer.from(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") {
|
||||||
@@ -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)`
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user