File size: 1,092 Bytes
282f0fd
 
 
 
 
 
 
4d7e26e
282f0fd
 
 
 
 
 
 
 
 
 
4d7e26e
282f0fd
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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);
}