mirror of
https://github.com/Azgaar/Fantasy-Map-Generator.git
synced 2026-03-23 23:57:23 +01:00
refactor: streamline dynamic import hash updates for public JS files
This commit is contained in:
parent
9a92a481d8
commit
8b9849aeed
1 changed files with 43 additions and 72 deletions
|
|
@ -180,99 +180,70 @@ function updateIndexHtmlHashes(changedFiles, 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(
|
||||||
const publicRoot = path.join(repoRoot, "public");
|
` src/index.html (changed files have no ?v= entry: ${changedFiles.map(f => f.replace("public/", "")).join(", ")}`
|
||||||
|
|
||||||
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
|
* For each changed public JS file, scans ALL other public JS files for
|
||||||
* (e.g. import("../dynamic/supporters.js?v=1.97.14")) and updates any that
|
* dynamic import() calls that reference it via a relative ?v= path, and
|
||||||
* point to one of the changed files.
|
* updates the hash to newVersion.
|
||||||
|
*
|
||||||
|
* Example: public/modules/dynamic/installation.js changed →
|
||||||
|
* main.js: import("./modules/dynamic/installation.js?v=1.89.19")
|
||||||
|
* → import("./modules/dynamic/installation.js?v=1.113.4")
|
||||||
*/
|
*/
|
||||||
function updatePublicJsDynamicImportHashes(changedFiles, newVersion, dry) {
|
function updatePublicJsDynamicImportHashes(changedFiles, newVersion, dry) {
|
||||||
if (changedFiles.length === 0) {
|
if (changedFiles.length === 0) {
|
||||||
console.log(" public/**/*.js (no changed public/*.js files detected)");
|
console.log(" public/**/*.js (no changed files, skipping dynamic import hashes)");
|
||||||
return;
|
return;
|
||||||
const replacement = `${quote}${relImportPath}?v=${newVersion}${quote}`;
|
}
|
||||||
// Only record and apply an update if the version actually changes
|
|
||||||
if (match === replacement) {
|
// Absolute paths of every changed file for O(1) lookup
|
||||||
return match;
|
const changedAbsPaths = new Set(changedFiles.map(f => path.join(repoRoot, f)));
|
||||||
}
|
|
||||||
if (!updatedMap[relJsFile]) updatedMap[relJsFile] = [];
|
// Collect all public JS files, skipping public/libs (third-party)
|
||||||
updatedMap[relJsFile].push(relImportPath);
|
const publicRoot = path.join(repoRoot, "public");
|
||||||
return replacement;
|
const allJsFiles = [];
|
||||||
const publicJsFiles = getAllPublicJsFiles();
|
(function walk(dir) {
|
||||||
|
for (const entry of fs.readdirSync(dir, {withFileTypes: true})) {
|
||||||
|
const full = path.join(dir, entry.name);
|
||||||
|
if (entry.isDirectory()) {
|
||||||
|
if (path.relative(publicRoot, full).replace(/\\/g, "/") === "libs") continue;
|
||||||
|
walk(full);
|
||||||
|
} else if (entry.isFile() && entry.name.endsWith(".js")) {
|
||||||
|
allJsFiles.push(full);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})(publicRoot);
|
||||||
|
|
||||||
const updatedMap = {};
|
const updatedMap = {};
|
||||||
|
|
||||||
for (const relJsFile of publicJsFiles) {
|
for (const absJsFile of allJsFiles) {
|
||||||
const absJsFile = path.join(repoRoot, relJsFile);
|
|
||||||
const content = readFile(absJsFile);
|
const content = readFile(absJsFile);
|
||||||
const dir = path.dirname(absJsFile);
|
// Matches: import("../path/file.js?v=1.2.3") or import('../path/file.js?v=1.2.3')
|
||||||
|
const pattern = /(['"])(\.{1,2}\/[^'"?]+)\?v=[0-9.]+\1/g;
|
||||||
const pattern = /(['"])(\.\.?\/[^'"]*)\?v=[0-9.]+\1/g;
|
let anyChanged = false;
|
||||||
const newContent = content.replace(pattern, (match, quote, relImportPath) => {
|
const newContent = content.replace(pattern, (match, quote, relImportPath) => {
|
||||||
// Strip any query string defensively before resolving the path
|
const absImport = path.resolve(path.dirname(absJsFile), relImportPath);
|
||||||
const cleanImportPath = relImportPath.split("?")[0];
|
if (!changedAbsPaths.has(absImport)) return match;
|
||||||
const absImport = path.resolve(dir, cleanImportPath);
|
const repoRelFile = path.relative(repoRoot, absJsFile).replace(/\\/g, "/");
|
||||||
const repoRelImport = path.relative(repoRoot, absImport).replace(/\\/g, "/");
|
if (!updatedMap[repoRelFile]) updatedMap[repoRelFile] = [];
|
||||||
if (changedSet.has(repoRelImport)) {
|
updatedMap[repoRelFile].push(relImportPath);
|
||||||
if (!updatedMap[relJsFile]) updatedMap[relJsFile] = [];
|
anyChanged = true;
|
||||||
updatedMap[relJsFile].push(relImportPath);
|
return `${quote}${relImportPath}?v=${newVersion}${quote}`;
|
||||||
return `${quote}${relImportPath}?v=${newVersion}${quote}`;
|
|
||||||
}
|
|
||||||
return match;
|
|
||||||
});
|
});
|
||||||
|
if (anyChanged && !dry) writeFile(absJsFile, newContent);
|
||||||
if (updatedMap[relJsFile] && !dry) {
|
|
||||||
writeFile(absJsFile, newContent);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Object.keys(updatedMap).length > 0) {
|
if (Object.keys(updatedMap).length > 0) {
|
||||||
const lines = Object.entries(updatedMap)
|
const lines = Object.entries(updatedMap)
|
||||||
.map(([file, refs]) => ` ${file}:\n - ${refs.join("\n - ")}`)
|
.map(([file, refs]) => ` ${file}:\n - ${refs.join("\n - ")}`)
|
||||||
.join("\n");
|
.join("\n");
|
||||||
console.log(` public/**/*.js hashes updated:\n${lines}`);
|
console.log(` public/**/*.js dynamic import hashes updated:\n${lines}`);
|
||||||
} else {
|
} else {
|
||||||
console.log(" public/**/*.js (no dynamic import ?v= hashes needed updating)");
|
console.log(" public/**/*.js (no dynamic import ?v= hashes to update)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue