- Implemented a service worker (sw.js) that handles installation, activation, and fetch events. - Added caching logic for public assets and a fallback for offline navigation. - Created unit tests (sw.test.ts) to validate service worker behavior, including asset caching, navigation handling, and activation logic. - Ensured that private API routes and third-party requests are excluded from caching.
42 lines
1.4 KiB
TypeScript
42 lines
1.4 KiB
TypeScript
import tailwind from "bun-plugin-tailwind";
|
|
import { rm, mkdir, copyFile } from "node:fs/promises";
|
|
import path from "node:path";
|
|
import { buildPwa } from "./scripts/build-pwa";
|
|
|
|
const outdir = path.join(process.cwd(), "dist");
|
|
await rm(outdir, { recursive: true, force: true });
|
|
|
|
const result = await Bun.build({
|
|
entrypoints: ["src/index.ts"],
|
|
outdir,
|
|
plugins: [tailwind],
|
|
minify: true,
|
|
splitting: true,
|
|
target: "bun",
|
|
external: ["@napi-rs/canvas", "/fonts/browser/*"],
|
|
sourcemap: "linked",
|
|
define: {
|
|
"process.env.NODE_ENV": JSON.stringify("production"),
|
|
},
|
|
});
|
|
|
|
if (!result.success) {
|
|
throw new AggregateError(result.logs, "Build failed");
|
|
}
|
|
|
|
for (const output of result.outputs) {
|
|
console.log(` ${path.relative(process.cwd(), output.path)} ${(output.size / 1024).toFixed(1)} KB`);
|
|
}
|
|
|
|
await mkdir(path.join(outdir, "fonts"), { recursive: true });
|
|
for (const file of ["InstrumentSerif-Regular.ttf", "NotoSans-Regular.ttf", "NotoSans-Medium.ttf", "NotoSans-SemiBold.ttf"]) {
|
|
await copyFile(`src/assets/fonts/${file}`, path.join(outdir, "fonts", file));
|
|
}
|
|
|
|
await mkdir(path.join(outdir, "fonts/browser"), { recursive: true });
|
|
for (const file of ["InstrumentSerif-Regular", "InstrumentSerif-Italic", "NotoSans-Regular", "NotoSans-Medium", "NotoSans-SemiBold"]) {
|
|
await copyFile(`src/assets/fonts/${file}.woff2`, path.join(outdir, `fonts/browser/${file}.woff2`));
|
|
}
|
|
|
|
await buildPwa(outdir, result.outputs);
|