Premier commit
Cette révision appartient à :
révision
0ec3f0e133
109 fichiers modifiés
+17644
Aucun fichier ne correspond à votre recherche
@@ -0,0 +1,92 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* tools/precompress.js
|
||||
*
|
||||
* Générateur statique AOT (Ahead-Of-Time) de fichiers Brotli (.br - niveau 11)
|
||||
* et Gzip (.gz - niveau 9) pour Nginx (brotli_static on; gzip_static on;).
|
||||
*
|
||||
* Zéro dépendance externe (utilise le module natif zlib de Node.js).
|
||||
*
|
||||
* Usage :
|
||||
* node tools/precompress.js
|
||||
*/
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const zlib = require('zlib');
|
||||
|
||||
const ROOT_DIR = path.resolve(__dirname, '..');
|
||||
const EXTENSIONS_TO_COMPRESS = new Set(['.html', '.css', '.js', '.json', '.svg', '.xml', '.txt']);
|
||||
const EXCLUDED_DIRS = new Set(['node_modules', '.git', 'deploy', 'tools']);
|
||||
|
||||
let totalOriginal = 0;
|
||||
let totalGz = 0;
|
||||
let totalBr = 0;
|
||||
let fileCount = 0;
|
||||
|
||||
function walk(dir) {
|
||||
const entries = fs.readdirSync(dir, { withFileTypes: true });
|
||||
for (const entry of entries) {
|
||||
const fullPath = path.join(dir, entry.name);
|
||||
if (entry.isDirectory()) {
|
||||
if (!EXCLUDED_DIRS.has(entry.name)) walk(fullPath);
|
||||
} else if (entry.isFile()) {
|
||||
const ext = path.extname(entry.name).toLowerCase();
|
||||
if (EXTENSIONS_TO_COMPRESS.has(ext)) {
|
||||
compressFile(fullPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function compressFile(filePath) {
|
||||
const relative = path.relative(ROOT_DIR, filePath);
|
||||
const content = fs.readFileSync(filePath);
|
||||
const origSize = content.length;
|
||||
|
||||
if (origSize === 0) return;
|
||||
|
||||
// Compression Gzip - Niveau 9 (maximum)
|
||||
const gzContent = zlib.gzipSync(content, { level: 9 });
|
||||
fs.writeFileSync(filePath + '.gz', gzContent);
|
||||
|
||||
// Compression Brotli - Qualité 11 (maximum)
|
||||
const brContent = zlib.brotliCompressSync(content, {
|
||||
params: {
|
||||
[zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT,
|
||||
[zlib.constants.BROTLI_PARAM_QUALITY]: 11,
|
||||
[zlib.constants.BROTLI_PARAM_LGWIN]: 22
|
||||
}
|
||||
});
|
||||
fs.writeFileSync(filePath + '.br', brContent);
|
||||
|
||||
totalOriginal += origSize;
|
||||
totalGz += gzContent.length;
|
||||
totalBr += brContent.length;
|
||||
fileCount++;
|
||||
|
||||
const gzRatio = ((1 - gzContent.length / origSize) * 100).toFixed(1);
|
||||
const brRatio = ((1 - brContent.length / origSize) * 100).toFixed(1);
|
||||
|
||||
console.log(`✓ ${relative.padEnd(30)} ${formatSize(origSize)} -> GZ: ${formatSize(gzContent.length)} (-${gzRatio}%) | BR: ${formatSize(brContent.length)} (-${brRatio}%)`);
|
||||
}
|
||||
|
||||
function formatSize(bytes) {
|
||||
if (bytes < 1024) return bytes + ' B';
|
||||
return (bytes / 1024).toFixed(1) + ' KB';
|
||||
}
|
||||
|
||||
console.log('================================================================');
|
||||
console.log(' Pré-compression statique Brotli 11 & Gzip 9 (Nginx AOT)');
|
||||
console.log(` Répertoire cible : ${ROOT_DIR}`);
|
||||
console.log('================================================================');
|
||||
|
||||
walk(ROOT_DIR);
|
||||
|
||||
console.log('----------------------------------------------------------------');
|
||||
console.log(`Fichiers traités : ${fileCount}`);
|
||||
console.log(`Taille brute : ${formatSize(totalOriginal)}`);
|
||||
console.log(`Taille Gzip (-9) : ${formatSize(totalGz)} (${((1 - totalGz / totalOriginal) * 100).toFixed(1)}% de gain)`);
|
||||
console.log(`Taille Brotli(11): ${formatSize(totalBr)} (${((1 - totalBr / totalOriginal) * 100).toFixed(1)}% de gain)`);
|
||||
console.log('================================================================');
|
||||
console.log('Prêt pour Nginx brotli_static on; et gzip_static on;');
|
||||
Référencer dans un nouveau ticket
Bloquer un utilisateur