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({
API: {
FILE_HOSTS: ['./json/file-hosts-optimized.json', './json/file-hosts.json'],
ADULT_HOSTS: ['./json/adult-hosts-optimized.json', './json/adult-hosts.json'],
FILE_HOSTS: ['./json/file-hosts-optimized.json'],
ADULT_HOSTS: ['./json/adult-hosts-optimized.json'],
TIMEOUT: 8000,
RETRY_ATTEMPTS: 2
},

View file

@ -12,7 +12,8 @@
"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",
"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"
},
"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 jsTarget = jsHash ? `src="/js/app-min.js?v=${jsHash}"` : 'src="/js/app-min.js"';
// Replace CSS links in head - more flexible patterns
const cssPatterns = [
/href=["']\.\/css\/styles\.css["']/g,
/href=["']css\/styles\.css["']/g,
/href=["'][^"']*\/styles\.css["']/g
];
// 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;
cssPatterns.forEach(pattern => {
const matches = updatedHeadContent.match(pattern);
if (matches) {
updatedHeadContent = updatedHeadContent.replace(pattern, cssTarget);
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}"`);
}
const cssMatches = updatedHeadContent.match(cssHrefPattern);
if (cssMatches) {
updatedHeadContent = updatedHeadContent.replace(cssHrefPattern, cssTarget);
cssReplaced += cssMatches.length;
}
// Update JS preload tags to include version hash
if (jsHash) {
const preloadMinJsPattern = /href=["']\/?js\/app-min\.js["']/g;
if (preloadMinJsPattern.test(updatedHeadContent)) {
updatedHeadContent = updatedHeadContent.replace(preloadMinJsPattern, `href="/js/app-min.js?v=${jsHash}"`);
}
const jsSrcHeadMatches = updatedHeadContent.match(jsSrcPattern);
if (jsSrcHeadMatches) {
updatedHeadContent = updatedHeadContent.replace(jsSrcPattern, jsTarget);
jsReplaced += jsSrcHeadMatches.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, jsTarget);
jsReplaced += matches.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);
@ -101,13 +80,11 @@ function replaceAssets() {
const [fullBodyTag, bodyStart, bodyContent, bodyEnd] = bodyMatch;
let updatedBodyContent = bodyContent;
jsPatterns.forEach(pattern => {
const matches = updatedBodyContent.match(pattern);
if (matches) {
updatedBodyContent = updatedBodyContent.replace(pattern, jsTarget);
jsReplaced += matches.length;
}
});
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) {