refactor: streamline asset references and improve replace-assets script

This commit is contained in:
fynks 2026-03-21 17:58:34 +05:00
parent 084a4fa41f
commit abde978f06
3 changed files with 28 additions and 50 deletions

4
dist/js/app.js vendored
View file

@ -9,8 +9,8 @@
============================================================================ */ ============================================================================ */
const CONFIG = Object.freeze({ const CONFIG = Object.freeze({
API: { API: {
FILE_HOSTS: ['./json/file-hosts-optimized.json', './json/file-hosts.json'], FILE_HOSTS: ['./json/file-hosts-optimized.json'],
ADULT_HOSTS: ['./json/adult-hosts-optimized.json', './json/adult-hosts.json'], ADULT_HOSTS: ['./json/adult-hosts-optimized.json'],
TIMEOUT: 8000, TIMEOUT: 8000,
RETRY_ATTEMPTS: 2 RETRY_ATTEMPTS: 2
}, },

View file

@ -12,7 +12,8 @@
"minify:css": "cleancss -o dist/css/styles-min.css dist/css/styles.css", "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", "minify:js": "terser dist/js/app.js -o dist/js/app-min.js --compress --mangle",
"optimize-json": "chmod +x scripts/json-optimizer.sh && scripts/json-optimizer.sh dist/json/file-hosts.json dist/json/file-hosts-optimized.json && scripts/json-optimizer.sh dist/json/adult-hosts.json dist/json/adult-hosts-optimized.json", "optimize-json": "chmod +x scripts/json-optimizer.sh && scripts/json-optimizer.sh dist/json/file-hosts.json dist/json/file-hosts-optimized.json && scripts/json-optimizer.sh dist/json/adult-hosts.json dist/json/adult-hosts-optimized.json",
"build": "npm run minify:css && npm run minify:js && npm run optimize-json && workbox generateSW workbox-config.js", "prune-json": "rm -f dist/json/file-hosts.json dist/json/adult-hosts.json",
"build": "npm run minify:css && npm run minify:js && npm run optimize-json && npm run prune-json && workbox generateSW workbox-config.js",
"postbuild": "node scripts/replace-assets.js" "postbuild": "node scripts/replace-assets.js"
}, },
"author": "fynks", "author": "fynks",

View file

@ -49,51 +49,30 @@ function replaceAssets() {
const cssTarget = cssHash ? `href="/css/styles-min.css?v=${cssHash}"` : 'href="/css/styles-min.css"'; 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"'; const jsTarget = jsHash ? `src="/js/app-min.js?v=${jsHash}"` : 'src="/js/app-min.js"';
// Replace CSS links in head - more flexible patterns // Normalize all stylesheet/script/preload references to minified assets with one hash.
const cssPatterns = [ const cssHrefPattern = /href=["'](?:\.\/|\/)?css\/(?:styles|styles-min)\.css(?:\?[^"']*)?["']/g;
/href=["']\.\/css\/styles\.css["']/g, const jsSrcPattern = /src=["'](?:\.\/|\/)?js\/(?:app|app-min)\.js(?:\?[^"']*)?["']/g;
/href=["']css\/styles\.css["']/g, const jsHrefPattern = /href=["'](?:\.\/|\/)?js\/(?:app|app-min)\.js(?:\?[^"']*)?["']/g;
/href=["'][^"']*\/styles\.css["']/g
];
cssPatterns.forEach(pattern => { const cssMatches = updatedHeadContent.match(cssHrefPattern);
const matches = updatedHeadContent.match(pattern); if (cssMatches) {
if (matches) { updatedHeadContent = updatedHeadContent.replace(cssHrefPattern, cssTarget);
updatedHeadContent = updatedHeadContent.replace(pattern, cssTarget); cssReplaced += cssMatches.length;
cssReplaced += matches.length;
}
});
// Ensure existing preload to styles-min.css carries the same version hash
if (cssHash) {
const preloadMinCssPattern = /href=["']\/?css\/styles-min\.css["']/g;
if (preloadMinCssPattern.test(updatedHeadContent)) {
updatedHeadContent = updatedHeadContent.replace(preloadMinCssPattern, `href="/css/styles-min.css?v=${cssHash}"`);
}
} }
// Update JS preload tags to include version hash const jsSrcHeadMatches = updatedHeadContent.match(jsSrcPattern);
if (jsHash) { if (jsSrcHeadMatches) {
const preloadMinJsPattern = /href=["']\/?js\/app-min\.js["']/g; updatedHeadContent = updatedHeadContent.replace(jsSrcPattern, jsTarget);
if (preloadMinJsPattern.test(updatedHeadContent)) { jsReplaced += jsSrcHeadMatches.length;
updatedHeadContent = updatedHeadContent.replace(preloadMinJsPattern, `href="/js/app-min.js?v=${jsHash}"`);
}
} }
// Replace JS script src in head - more flexible patterns // Preload script links use href (not src), so normalize them too.
const jsPatterns = [ const jsHrefHeadMatches = updatedHeadContent.match(jsHrefPattern);
/src=["']\.\/js\/app\.js["']/g, if (jsHrefHeadMatches) {
/src=["']js\/app\.js["']/g, const jsHrefTarget = jsHash ? `href="/js/app-min.js?v=${jsHash}"` : 'href="/js/app-min.js"';
/src=["'][^"']*\/app\.js["']/g updatedHeadContent = updatedHeadContent.replace(jsHrefPattern, jsHrefTarget);
]; jsReplaced += jsHrefHeadMatches.length;
}
jsPatterns.forEach(pattern => {
const matches = updatedHeadContent.match(pattern);
if (matches) {
updatedHeadContent = updatedHeadContent.replace(pattern, jsTarget);
jsReplaced += matches.length;
}
});
// Also check for script tags at the end of body (common pattern) // Also check for script tags at the end of body (common pattern)
const bodyMatch = html.match(/(<body[^>]*>)([\s\S]*?)(<\/body>)/i); const bodyMatch = html.match(/(<body[^>]*>)([\s\S]*?)(<\/body>)/i);
@ -101,13 +80,11 @@ function replaceAssets() {
const [fullBodyTag, bodyStart, bodyContent, bodyEnd] = bodyMatch; const [fullBodyTag, bodyStart, bodyContent, bodyEnd] = bodyMatch;
let updatedBodyContent = bodyContent; let updatedBodyContent = bodyContent;
jsPatterns.forEach(pattern => { const jsSrcBodyMatches = updatedBodyContent.match(jsSrcPattern);
const matches = updatedBodyContent.match(pattern); if (jsSrcBodyMatches) {
if (matches) { updatedBodyContent = updatedBodyContent.replace(jsSrcPattern, jsTarget);
updatedBodyContent = updatedBodyContent.replace(pattern, jsTarget); jsReplaced += jsSrcBodyMatches.length;
jsReplaced += matches.length; }
}
});
// Reconstruct the body if changes were made // Reconstruct the body if changes were made
if (updatedBodyContent !== bodyContent) { if (updatedBodyContent !== bodyContent) {