143 lines
No EOL
5.3 KiB
JavaScript
143 lines
No EOL
5.3 KiB
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
// Improved script with better error handling and focused head tag processing
|
|
function replaceAssets() {
|
|
const file = path.resolve(__dirname, '..', 'dist', 'index.html');
|
|
|
|
// Check if dist/index.html exists
|
|
if (!fs.existsSync(file)) {
|
|
console.error('Error: dist/index.html not found');
|
|
process.exit(1);
|
|
}
|
|
|
|
try {
|
|
let html = fs.readFileSync(file, 'utf8');
|
|
console.log('Original HTML loaded successfully');
|
|
|
|
// Extract the head section
|
|
const headMatch = html.match(/(<head[^>]*>)([\s\S]*?)(<\/head>)/i);
|
|
if (!headMatch) {
|
|
console.error('Error: Could not find <head> tag in HTML');
|
|
process.exit(1);
|
|
}
|
|
|
|
const [fullHeadTag, headStart, headContent, headEnd] = headMatch;
|
|
console.log('Head section found, processing assets...');
|
|
|
|
let updatedHeadContent = headContent;
|
|
let cssReplaced = 0;
|
|
let jsReplaced = 0;
|
|
|
|
// Compute simple query hash for cache-busting if files exist
|
|
const cssMinPath = path.resolve(__dirname, '..', 'dist', 'css', 'styles-min.css');
|
|
const jsMinPath = path.resolve(__dirname, '..', 'dist', 'js', 'app-min.js');
|
|
const safeHash = (p) => {
|
|
try {
|
|
const buf = fs.readFileSync(p);
|
|
let hash = 0;
|
|
for (let i = 0; i < buf.length; i += Math.ceil(buf.length / 128) || 1) {
|
|
hash = (hash * 31 + buf[i]) >>> 0;
|
|
}
|
|
return hash.toString(36);
|
|
} catch {
|
|
return null;
|
|
}
|
|
};
|
|
const cssHash = safeHash(cssMinPath);
|
|
const jsHash = safeHash(jsMinPath);
|
|
const cssTarget = cssHash ? `href="/css/styles-min.css?v=${cssHash}"` : 'href="/css/styles-min.css"';
|
|
const jsTarget = jsHash ? `src="/js/app-min.js?v=${jsHash}"` : 'src="/js/app-min.js"';
|
|
|
|
// Normalize all stylesheet/script/preload references to minified assets with one hash.
|
|
const cssHrefPattern = /href=["'](?:\.\/|\/)?css\/(?:styles|styles-min)\.css(?:\?[^"']*)?["']/g;
|
|
const jsSrcPattern = /src=["'](?:\.\/|\/)?js\/(?:app|app-min)\.js(?:\?[^"']*)?["']/g;
|
|
const jsHrefPattern = /href=["'](?:\.\/|\/)?js\/(?:app|app-min)\.js(?:\?[^"']*)?["']/g;
|
|
|
|
const cssMatches = updatedHeadContent.match(cssHrefPattern);
|
|
if (cssMatches) {
|
|
updatedHeadContent = updatedHeadContent.replace(cssHrefPattern, cssTarget);
|
|
cssReplaced += cssMatches.length;
|
|
}
|
|
|
|
const jsSrcHeadMatches = updatedHeadContent.match(jsSrcPattern);
|
|
if (jsSrcHeadMatches) {
|
|
updatedHeadContent = updatedHeadContent.replace(jsSrcPattern, jsTarget);
|
|
jsReplaced += jsSrcHeadMatches.length;
|
|
}
|
|
|
|
// Preload script links use href (not src), so normalize them too.
|
|
const jsHrefHeadMatches = updatedHeadContent.match(jsHrefPattern);
|
|
if (jsHrefHeadMatches) {
|
|
const jsHrefTarget = jsHash ? `href="/js/app-min.js?v=${jsHash}"` : 'href="/js/app-min.js"';
|
|
updatedHeadContent = updatedHeadContent.replace(jsHrefPattern, jsHrefTarget);
|
|
jsReplaced += jsHrefHeadMatches.length;
|
|
}
|
|
|
|
// Also check for script tags at the end of body (common pattern)
|
|
const bodyMatch = html.match(/(<body[^>]*>)([\s\S]*?)(<\/body>)/i);
|
|
if (bodyMatch) {
|
|
const [fullBodyTag, bodyStart, bodyContent, bodyEnd] = bodyMatch;
|
|
let updatedBodyContent = bodyContent;
|
|
|
|
const jsSrcBodyMatches = updatedBodyContent.match(jsSrcPattern);
|
|
if (jsSrcBodyMatches) {
|
|
updatedBodyContent = updatedBodyContent.replace(jsSrcPattern, jsTarget);
|
|
jsReplaced += jsSrcBodyMatches.length;
|
|
}
|
|
|
|
// Reconstruct the body if changes were made
|
|
if (updatedBodyContent !== bodyContent) {
|
|
html = html.replace(fullBodyTag, bodyStart + updatedBodyContent + bodyEnd);
|
|
}
|
|
}
|
|
|
|
// Reconstruct the HTML with updated head
|
|
const updatedHeadTag = headStart + updatedHeadContent + headEnd;
|
|
html = html.replace(fullHeadTag, updatedHeadTag);
|
|
|
|
// Log results
|
|
console.log(`CSS replacements made: ${cssReplaced}`);
|
|
console.log(`JS replacements made: ${jsReplaced}`);
|
|
|
|
if (cssReplaced === 0 && jsReplaced === 0) {
|
|
console.warn('Warning: No asset links were found to replace');
|
|
console.log('Checking for existing patterns in head section:');
|
|
|
|
// Debug: show what's actually in the head
|
|
const linkTags = headContent.match(/<link[^>]*>/gi) || [];
|
|
const scriptTags = headContent.match(/<script[^>]*>/gi) || [];
|
|
|
|
console.log('Found link tags:', linkTags);
|
|
console.log('Found script tags:', scriptTags);
|
|
} else {
|
|
console.log('Asset links replaced successfully');
|
|
}
|
|
|
|
// Write the updated HTML back
|
|
fs.writeFileSync(file, html, 'utf8');
|
|
console.log('Updated dist/index.html saved');
|
|
|
|
// Verify the minified files exist
|
|
const cssFile = path.resolve(__dirname, '..', 'dist', 'css', 'styles-min.css');
|
|
const jsFile = path.resolve(__dirname, '..', 'dist', 'js', 'app-min.js');
|
|
|
|
if (!fs.existsSync(cssFile)) {
|
|
console.warn('Warning: styles-min.css not found - CSS minification may have failed');
|
|
} else {
|
|
console.log('✓ styles-min.css exists');
|
|
}
|
|
|
|
if (!fs.existsSync(jsFile)) {
|
|
console.warn('Warning: app-min.js not found - JS minification may have failed');
|
|
} else {
|
|
console.log('✓ app-min.js exists');
|
|
}
|
|
|
|
} catch (error) {
|
|
console.error('Error processing HTML file:', error.message);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
replaceAssets(); |