From 5dbcd1ab921407e77b4f5b2c879af2088d41be75 Mon Sep 17 00:00:00 2001 From: fynks <75840152+fynks@users.noreply.github.com> Date: Tue, 19 Aug 2025 08:35:34 +0500 Subject: [PATCH] working --- package.json | 2 +- scripts/replace-assets.js | 57 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 scripts/replace-assets.js diff --git a/package.json b/package.json index 455fa7b..0104ab9 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "minify:css": "cleancss -o dist/css/styles-min.css dist/css/styles.css", "minify:js": "terser dist/js/app.js -o dist/js/app-min.js --compress --mangle", "build": "npm run minify:css && npm run minify:js && workbox generateSW workbox-config.js", - "postbuild": "node scripts/replace-assets.js" + "postbuild": "node scripts/replace-assets.js" }, "author": "fynks", "dependencies": { diff --git a/scripts/replace-assets.js b/scripts/replace-assets.js new file mode 100644 index 0000000..8f7c266 --- /dev/null +++ b/scripts/replace-assets.js @@ -0,0 +1,57 @@ +const fs = require('fs'); +const path = require('path'); + +// Improved script with better error handling and logging +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'); + + // Store original for comparison + const originalHtml = html; + + // 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"'); + + // Check if any replacements were made + if (html === originalHtml) { + console.warn('Warning: No asset links were found to replace'); + console.log('This might indicate that the HTML structure has changed'); + } 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'); + } + + if (!fs.existsSync(jsFile)) { + console.warn('Warning: app-min.js not found - JS minification may have failed'); + } + + } catch (error) { + console.error('Error processing HTML file:', error.message); + process.exit(1); + } +} + +replaceAssets(); \ No newline at end of file