new site
This commit is contained in:
+109
@@ -0,0 +1,109 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
// Find all HTML files that need optimization
|
||||
const findHtmlFiles = (dir) => {
|
||||
const files = [];
|
||||
const items = fs.readdirSync(dir);
|
||||
|
||||
for (const item of items) {
|
||||
const fullPath = path.join(dir, item);
|
||||
const stat = fs.statSync(fullPath);
|
||||
|
||||
if (stat.isDirectory() && !item.startsWith('.') && item !== 'node_modules') {
|
||||
files.push(...findHtmlFiles(fullPath));
|
||||
} else if (item.endsWith('.html') && !item.includes('test') && !item.includes('performance') && !item.includes('optimized')) {
|
||||
files.push(fullPath);
|
||||
}
|
||||
}
|
||||
|
||||
return files;
|
||||
};
|
||||
|
||||
// Apply global optimizations
|
||||
const optimizeFile = (filePath) => {
|
||||
let content = fs.readFileSync(filePath, 'utf8');
|
||||
let modified = false;
|
||||
|
||||
// 1. Replace main.css with main.min.css (if not already minified)
|
||||
if (content.includes('href="/css/main.css"') || content.includes('href="css/main.css"')) {
|
||||
content = content.replace(/href="(\/?)css\/main\.css"/g, 'href="$1css/main.min.css"');
|
||||
modified = true;
|
||||
}
|
||||
|
||||
// 2. Add preconnect hints if missing (only in head section)
|
||||
if (!content.includes('preconnect') && content.includes('<head>')) {
|
||||
const preconnectHints = `
|
||||
<!-- Performance: Preconnect to critical origins -->
|
||||
<link rel="preconnect" href="https://d3js.org" crossorigin>
|
||||
<link rel="preconnect" href="https://images.pexels.com" crossorigin>`;
|
||||
|
||||
content = content.replace(/<head>/, `<head>${preconnectHints}`);
|
||||
modified = true;
|
||||
}
|
||||
|
||||
// 3. Update component scripts to use minified versions
|
||||
if (content.includes('src="/components/') || content.includes('src="components/')) {
|
||||
content = content.replace(/src="(\/?)components\/([^"]+)\.js"/g, (match, slash, name) => {
|
||||
// Only update if .min.js exists
|
||||
const minPath = path.join(__dirname, 'components', `${name}.min.js`);
|
||||
if (fs.existsSync(minPath)) {
|
||||
return `src="${slash}components/${name}.min.js"`;
|
||||
}
|
||||
return match;
|
||||
});
|
||||
|
||||
content = content.replace(/src="(\/?)js\/([^"]+)\.js"/g, (match, slash, name) => {
|
||||
// Only update if .min.js exists
|
||||
const minPath = path.join(__dirname, 'js', `${name}.min.js`);
|
||||
if (fs.existsSync(minPath)) {
|
||||
return `src="${slash}js/${name}.min.js"`;
|
||||
}
|
||||
return match;
|
||||
});
|
||||
modified = true;
|
||||
}
|
||||
|
||||
// 4. Add async loading for D3.js if it's loaded directly
|
||||
if (content.includes('d3.v7.min.js') && !content.includes('async')) {
|
||||
content = content.replace(
|
||||
/<script src="https:\/\/d3js\.org\/d3\.v7\.min\.js"><\/script>/g,
|
||||
'<script src="https://d3js.org/d3.v7.min.js" async defer crossorigin="anonymous"></script>'
|
||||
);
|
||||
modified = true;
|
||||
}
|
||||
|
||||
// Write back if modified
|
||||
if (modified) {
|
||||
fs.writeFileSync(filePath, content);
|
||||
console.log(`✓ Optimized: ${path.relative(__dirname, filePath)}`);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
// Main execution
|
||||
console.log('🚀 Starting global performance optimization...\n');
|
||||
|
||||
const htmlFiles = findHtmlFiles(__dirname);
|
||||
let optimizedCount = 0;
|
||||
|
||||
for (const file of htmlFiles) {
|
||||
try {
|
||||
if (optimizeFile(file)) {
|
||||
optimizedCount++;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`❌ Error optimizing ${file}:`, error.message);
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`\n🎯 Optimization complete!`);
|
||||
console.log(`📊 Optimized ${optimizedCount} out of ${htmlFiles.length} files`);
|
||||
console.log(`\n💡 Optimizations applied:`);
|
||||
console.log(` ✅ Switched to minified CSS (main.min.css)`);
|
||||
console.log(` ✅ Added preconnect hints for faster loading`);
|
||||
console.log(` ✅ Updated to minified component scripts`);
|
||||
console.log(` ✅ Optimized D3.js loading`);
|
||||
console.log(`\n🔒 All D3.js visuals preserved - no content changes!`);
|
||||
Reference in New Issue
Block a user