diff --git a/scripts/replace-assets.js b/scripts/replace-assets.js index 8f7c266..dec8495 100644 --- a/scripts/replace-assets.js +++ b/scripts/replace-assets.js @@ -1,7 +1,7 @@ const fs = require('fs'); const path = require('path'); -// Improved script with better error handling and logging +// Improved script with better error handling and focused head tag processing function replaceAssets() { const file = path.resolve(__dirname, '..', 'dist', 'index.html'); @@ -15,19 +15,88 @@ function replaceAssets() { let html = fs.readFileSync(file, 'utf8'); console.log('Original HTML loaded successfully'); - // Store original for comparison - const originalHtml = html; + // Extract the head section + const headMatch = html.match(/(
]*>)([\s\S]*?)(<\/head>)/i); + if (!headMatch) { + console.error('Error: Could not find tag in HTML'); + process.exit(1); + } - // Replace CSS links - html = html.replace(/href=["']\.\/css\/styles\.css["']/g, 'href="./css/styles-min.css"'); - - // Replace JS links - fixed regex to match your exact pattern - html = html.replace(/src=["']\.?\/js\/app\.js["']/g, 'src="js/app-min.js"'); + const [fullHeadTag, headStart, headContent, headEnd] = headMatch; + console.log('Head section found, processing assets...'); - // Check if any replacements were made - if (html === originalHtml) { + let updatedHeadContent = headContent; + let cssReplaced = 0; + let jsReplaced = 0; + + // Replace CSS links in head - more flexible patterns + const cssPatterns = [ + /href=["']\.\/css\/styles\.css["']/g, + /href=["']css\/styles\.css["']/g, + /href=["'][^"']*\/styles\.css["']/g + ]; + + cssPatterns.forEach(pattern => { + const matches = updatedHeadContent.match(pattern); + if (matches) { + updatedHeadContent = updatedHeadContent.replace(pattern, 'href="./css/styles-min.css"'); + cssReplaced += matches.length; + } + }); + + // Replace JS script src in head - more flexible patterns + const jsPatterns = [ + /src=["']\.\/js\/app\.js["']/g, + /src=["']js\/app\.js["']/g, + /src=["'][^"']*\/app\.js["']/g + ]; + + jsPatterns.forEach(pattern => { + const matches = updatedHeadContent.match(pattern); + if (matches) { + updatedHeadContent = updatedHeadContent.replace(pattern, 'src="js/app-min.js"'); + jsReplaced += matches.length; + } + }); + + // Also check for script tags at the end of body (common pattern) + const bodyMatch = html.match(/(]*>)([\s\S]*?)(<\/body>)/i); + if (bodyMatch) { + const [fullBodyTag, bodyStart, bodyContent, bodyEnd] = bodyMatch; + let updatedBodyContent = bodyContent; + + jsPatterns.forEach(pattern => { + const matches = updatedBodyContent.match(pattern); + if (matches) { + updatedBodyContent = updatedBodyContent.replace(pattern, 'src="js/app-min.js"'); + jsReplaced += matches.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('This might indicate that the HTML structure has changed'); + console.log('Checking for existing patterns in head section:'); + + // Debug: show what's actually in the head + const linkTags = headContent.match(/]*>/gi) || []; + const scriptTags = headContent.match(/