Search / scripts /build_static_assets.mjs
vomebook's picture
Add Reader Assets online reading
4d7e26e verified
Raw
History Blame Contribute Delete
1.09 kB
import { createHash } from "node:crypto";
import { copyFileSync, readFileSync, writeFileSync } from "node:fs";
import { extname, join } from "node:path";
const outputDir = process.env.STATIC_OUTPUT_DIR || "/out";
const replacements = new Map();
for (const filename of ["app.js", "style.css", "reader-contract.js", "reader-store.js", "reader.js", "reader.css"]) {
const source = join(outputDir, filename);
const content = readFileSync(source);
const hash = createHash("sha256").update(content).digest("hex").slice(0, 12);
const extension = extname(filename);
const stem = filename.slice(0, -extension.length);
const versioned = `${stem}.${hash}${extension}`;
copyFileSync(source, join(outputDir, versioned));
replacements.set(`/static/${filename}`, `/static/${versioned}`);
}
for (const filename of ["index.html", "reader.html", "sw.js"]) {
const path = join(outputDir, filename);
let content = readFileSync(path, "utf8");
for (const [original, versioned] of replacements) {
content = content.split(original).join(versioned);
}
writeFileSync(path, content);
}