96 lines
2.9 KiB
JavaScript
96 lines
2.9 KiB
JavaScript
/**
|
|
* Stale browsers often keep an old index.html that still points at previous
|
|
* hashed chunks (e.g. App-CuujNhcz.js). Those old bundles request /assets/*
|
|
* at the domain root → 404 under /citpl_website/.
|
|
*
|
|
* After each build, overwrite known legacy filenames with the current
|
|
* main/App/x/admin/preview bundles so cached HTML still gets prefixed URLs.
|
|
*/
|
|
import { readFileSync, writeFileSync, existsSync, readdirSync } from 'fs';
|
|
import { resolve, dirname, join } from 'path';
|
|
import { fileURLToPath } from 'url';
|
|
|
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
const dist = resolve(__dirname, '../dist');
|
|
const assetsDir = join(dist, 'assets');
|
|
|
|
function entryScript(htmlFile, pattern) {
|
|
const html = readFileSync(join(dist, htmlFile), 'utf8');
|
|
const match = html.match(pattern);
|
|
if (!match) throw new Error(`Could not find entry in ${htmlFile}`);
|
|
return match[1];
|
|
}
|
|
|
|
const current = {
|
|
main: entryScript('index.html', /assets\/(main-[^"]+\.js)/),
|
|
app: entryScript('index.html', /assets\/(App-[^"]+\.js)/),
|
|
x: entryScript('index.html', /assets\/(x-[^"]+\.js)/),
|
|
admin: entryScript('admin.html', /assets\/(admin-[^"]+\.js)/),
|
|
preview: entryScript('preview.html', /assets\/(preview-[^"]+\.js)/),
|
|
};
|
|
|
|
const LEGACY = {
|
|
main: [
|
|
'main-DS8JYUzR.js',
|
|
'main-BxILBfZ3.js',
|
|
'main-oTwpSzcd.js',
|
|
],
|
|
app: [
|
|
'App-EUHghoux.js',
|
|
'App-g0Gkzm_v.js',
|
|
'App-CuujNhcz.js',
|
|
],
|
|
x: [
|
|
'x-DnG5sti3.js',
|
|
],
|
|
admin: [
|
|
'admin-D28kGkNT.js',
|
|
],
|
|
preview: [
|
|
'preview-G0j3SNu_.js',
|
|
'preview-wEp9RUMD.js',
|
|
'preview-C0prGlYe.js',
|
|
],
|
|
};
|
|
|
|
function alias(kind, legacyNames) {
|
|
const srcName = current[kind];
|
|
const srcPath = join(assetsDir, srcName);
|
|
if (!existsSync(srcPath)) {
|
|
console.warn(`⚠ skip ${kind}: missing ${srcName}`);
|
|
return 0;
|
|
}
|
|
const body = readFileSync(srcPath);
|
|
let n = 0;
|
|
for (const name of legacyNames) {
|
|
if (name === srcName) continue;
|
|
writeFileSync(join(assetsDir, name), body);
|
|
n++;
|
|
}
|
|
return n;
|
|
}
|
|
|
|
let total = 0;
|
|
for (const [kind, names] of Object.entries(LEGACY)) {
|
|
total += alias(kind, names);
|
|
}
|
|
|
|
// Also alias any other main-*/App-*/x-* JS still sitting in dist/assets from older builds
|
|
const files = readdirSync(assetsDir);
|
|
for (const file of files) {
|
|
if (!file.endsWith('.js')) continue;
|
|
let kind = null;
|
|
if (/^main-/.test(file) && file !== current.main) kind = 'main';
|
|
else if (/^App-/.test(file) && file !== current.app) kind = 'app';
|
|
else if (/^x-/.test(file) && file !== current.x) kind = 'x';
|
|
else if (/^admin-/.test(file) && file !== current.admin) kind = 'admin';
|
|
else if (/^preview-/.test(file) && file !== current.preview) kind = 'preview';
|
|
if (!kind) continue;
|
|
const already = LEGACY[kind]?.includes(file);
|
|
if (already) continue;
|
|
writeFileSync(join(assetsDir, file), readFileSync(join(assetsDir, current[kind])));
|
|
total++;
|
|
}
|
|
|
|
console.log(`✓ Aliased ${total} legacy JS chunks → current build (${current.main}, ${current.app}, ${current.x})`);
|