mirror of
https://github.com/Azgaar/Fantasy-Map-Generator.git
synced 2026-03-22 15:17:23 +01:00
Extend bump-version.js to update ?v= cache-busting hashes in public/**/*.js dynamic imports (#1347)
* Initial plan * feat: extend bump-version.js to update ?v= hashes in public/**/*.js dynamic imports Co-authored-by: Azgaar <26469650+Azgaar@users.noreply.github.com> * chore: merge base branch changes (package-lock.json sync, RELEASE_BOT_TOKEN, node 24.x, comment fix) Co-authored-by: Azgaar <26469650+Azgaar@users.noreply.github.com> * Update scripts/bump-version.js Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update scripts/bump-version.js Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Azgaar <26469650+Azgaar@users.noreply.github.com> Co-authored-by: Azgaar <maxganiev@yandex.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
parent
ab445d910e
commit
9a92a481d8
1 changed files with 100 additions and 6 deletions
|
|
@ -9,6 +9,7 @@
|
||||||
* - package.json — "version" field
|
* - package.json — "version" field
|
||||||
* - package-lock.json — top-level "version" and packages[""].version fields
|
* - package-lock.json — top-level "version" and packages[""].version fields
|
||||||
* - src/index.html — ?v= cache-busting hashes for changed public/*.js files
|
* - src/index.html — ?v= cache-busting hashes for changed public/*.js files
|
||||||
|
* - public/**\/*.js — ?v= cache-busting hashes in dynamic import() calls
|
||||||
*
|
*
|
||||||
* Usage:
|
* Usage:
|
||||||
* node scripts/bump-version.js # interactive prompt
|
* node scripts/bump-version.js # interactive prompt
|
||||||
|
|
@ -156,9 +157,7 @@ function updatePackageLockJson(newVersion, dry) {
|
||||||
console.log(` package-lock.json ${oldVersion} → ${newVersion}`);
|
console.log(` package-lock.json ${oldVersion} → ${newVersion}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateIndexHtmlHashes(newVersion, dry) {
|
function updateIndexHtmlHashes(changedFiles, newVersion, dry) {
|
||||||
const changedFiles = getChangedPublicJsFiles();
|
|
||||||
|
|
||||||
if (changedFiles.length === 0) {
|
if (changedFiles.length === 0) {
|
||||||
console.log(" src/index.html (no changed public/*.js files detected)");
|
console.log(" src/index.html (no changed public/*.js files detected)");
|
||||||
return;
|
return;
|
||||||
|
|
@ -181,11 +180,102 @@ function updateIndexHtmlHashes(newVersion, dry) {
|
||||||
console.log(` src/index.html hashes updated for:\n - ${updated.join("\n - ")}`);
|
console.log(` src/index.html hashes updated for:\n - ${updated.join("\n - ")}`);
|
||||||
} else {
|
} else {
|
||||||
console.log(
|
console.log(
|
||||||
` src/index.html (changed files not referenced: ${changedFiles.map(f => f.replace("public/", "")).join(", ")})`
|
const publicRoot = path.join(repoRoot, "public");
|
||||||
|
|
||||||
|
function walk(dir) {
|
||||||
|
for (const entry of fs.readdirSync(dir, {withFileTypes: true})) {
|
||||||
|
const full = path.join(dir, entry.name);
|
||||||
|
if (entry.isDirectory()) {
|
||||||
|
// Skip third-party vendor bundles under public/libs/**
|
||||||
|
const relFromPublic = path
|
||||||
|
.relative(publicRoot, full)
|
||||||
|
.replace(/\\/g, "/");
|
||||||
|
if (relFromPublic === "libs" || relFromPublic.startsWith("libs/")) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
walk(full);
|
||||||
|
} else if (entry.isFile() && entry.name.endsWith(".js")) {
|
||||||
|
results.push(path.relative(repoRoot, full).replace(/\\/g, "/"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
walk(publicRoot);
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Returns all .js file paths (relative to repo root, with forward slashes) under public/. */
|
||||||
|
function getAllPublicJsFiles() {
|
||||||
|
const results = [];
|
||||||
|
function walk(dir) {
|
||||||
|
for (const entry of fs.readdirSync(dir, {withFileTypes: true})) {
|
||||||
|
const full = path.join(dir, entry.name);
|
||||||
|
if (entry.isDirectory()) {
|
||||||
|
walk(full);
|
||||||
|
} else if (entry.isFile() && entry.name.endsWith(".js")) {
|
||||||
|
results.push(path.relative(repoRoot, full).replace(/\\/g, "/"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
walk(path.join(repoRoot, "public"));
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Scans all public/**\/*.js files for relative dynamic-import ?v= references
|
||||||
|
* (e.g. import("../dynamic/supporters.js?v=1.97.14")) and updates any that
|
||||||
|
* point to one of the changed files.
|
||||||
|
*/
|
||||||
|
function updatePublicJsDynamicImportHashes(changedFiles, newVersion, dry) {
|
||||||
|
if (changedFiles.length === 0) {
|
||||||
|
console.log(" public/**/*.js (no changed public/*.js files detected)");
|
||||||
|
return;
|
||||||
|
const replacement = `${quote}${relImportPath}?v=${newVersion}${quote}`;
|
||||||
|
// Only record and apply an update if the version actually changes
|
||||||
|
if (match === replacement) {
|
||||||
|
return match;
|
||||||
|
}
|
||||||
|
if (!updatedMap[relJsFile]) updatedMap[relJsFile] = [];
|
||||||
|
updatedMap[relJsFile].push(relImportPath);
|
||||||
|
return replacement;
|
||||||
|
const publicJsFiles = getAllPublicJsFiles();
|
||||||
|
const updatedMap = {};
|
||||||
|
|
||||||
|
for (const relJsFile of publicJsFiles) {
|
||||||
|
const absJsFile = path.join(repoRoot, relJsFile);
|
||||||
|
const content = readFile(absJsFile);
|
||||||
|
const dir = path.dirname(absJsFile);
|
||||||
|
|
||||||
|
const pattern = /(['"])(\.\.?\/[^'"]*)\?v=[0-9.]+\1/g;
|
||||||
|
const newContent = content.replace(pattern, (match, quote, relImportPath) => {
|
||||||
|
// Strip any query string defensively before resolving the path
|
||||||
|
const cleanImportPath = relImportPath.split("?")[0];
|
||||||
|
const absImport = path.resolve(dir, cleanImportPath);
|
||||||
|
const repoRelImport = path.relative(repoRoot, absImport).replace(/\\/g, "/");
|
||||||
|
if (changedSet.has(repoRelImport)) {
|
||||||
|
if (!updatedMap[relJsFile]) updatedMap[relJsFile] = [];
|
||||||
|
updatedMap[relJsFile].push(relImportPath);
|
||||||
|
return `${quote}${relImportPath}?v=${newVersion}${quote}`;
|
||||||
|
}
|
||||||
|
return match;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (updatedMap[relJsFile] && !dry) {
|
||||||
|
writeFile(absJsFile, newContent);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Object.keys(updatedMap).length > 0) {
|
||||||
|
const lines = Object.entries(updatedMap)
|
||||||
|
.map(([file, refs]) => ` ${file}:\n - ${refs.join("\n - ")}`)
|
||||||
|
.join("\n");
|
||||||
|
console.log(` public/**/*.js hashes updated:\n${lines}`);
|
||||||
|
} else {
|
||||||
|
console.log(" public/**/*.js (no dynamic import ?v= hashes needed updating)");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// Prompt
|
// Prompt
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
@ -230,7 +320,9 @@ async function main() {
|
||||||
`\n[bump-version] Version already updated manually: ${baseVersion} → ${currentVersion} (base was ${baseVersion})\n`
|
`\n[bump-version] Version already updated manually: ${baseVersion} → ${currentVersion} (base was ${baseVersion})\n`
|
||||||
);
|
);
|
||||||
console.log(" Skipping version increment — updating ?v= hashes only.\n");
|
console.log(" Skipping version increment — updating ?v= hashes only.\n");
|
||||||
updateIndexHtmlHashes(currentVersion, dry);
|
const changedFiles = getChangedPublicJsFiles();
|
||||||
|
updateIndexHtmlHashes(changedFiles, currentVersion, dry);
|
||||||
|
updatePublicJsDynamicImportHashes(changedFiles, currentVersion, dry);
|
||||||
console.log(`\n[bump-version] ${dry ? "(dry run) " : ""}done.\n`);
|
console.log(`\n[bump-version] ${dry ? "(dry run) " : ""}done.\n`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -247,10 +339,12 @@ async function main() {
|
||||||
|
|
||||||
console.log(`\n[bump-version] ${bumpType}: ${currentVersion} → ${newVersion}\n`);
|
console.log(`\n[bump-version] ${bumpType}: ${currentVersion} → ${newVersion}\n`);
|
||||||
|
|
||||||
|
const changedFiles = getChangedPublicJsFiles();
|
||||||
updateVersioningJs(newVersion, dry);
|
updateVersioningJs(newVersion, dry);
|
||||||
updatePackageJson(newVersion, dry);
|
updatePackageJson(newVersion, dry);
|
||||||
updatePackageLockJson(newVersion, dry);
|
updatePackageLockJson(newVersion, dry);
|
||||||
updateIndexHtmlHashes(newVersion, dry);
|
updateIndexHtmlHashes(changedFiles, newVersion, dry);
|
||||||
|
updatePublicJsDynamicImportHashes(changedFiles, newVersion, dry);
|
||||||
|
|
||||||
console.log(`\n[bump-version] ${dry ? "(dry run) " : ""}done.\n`);
|
console.log(`\n[bump-version] ${dry ? "(dry run) " : ""}done.\n`);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue