shellphone.app/scripts/build-worker.js
m5r 1e9b7a8aa2 * fix "dev:build" watch mode
* remove cross-env
* append build hash to service worker cache names for easy purge
2022-06-11 15:13:28 +02:00

68 lines
1.9 KiB
JavaScript

const fs = require("node:fs");
const path = require("node:path");
const glob = require("glob");
const esbuild = require("esbuild");
const isDev = process.env.NODE_ENV !== "production";
const basePath = process.cwd();
const args = process.argv.slice(2);
const watch = args.includes("--watch");
const cacheVersion = isDev
? "dev"
: (() => {
const manifests = glob.sync(path.join(basePath, "/public/build/manifest-*.js"));
const manifest = manifests.reduce((mostRecent, manifest) =>
fs.statSync(manifest).mtime > fs.statSync(mostRecent).mtime ? manifest : mostRecent,
);
return manifest.match(/manifest-(\w+).js/)[1].toLowerCase();
})();
esbuild
.build({
write: true,
outfile: path.join(basePath, "public", "entry.worker.js"),
entryPoints: [path.join(basePath, "app", "entry.worker.ts")],
format: "esm",
bundle: true,
define: {
ASSET_CACHE: `"asset-cache_${cacheVersion}"`,
DATA_CACHE: `"data-cache_${cacheVersion}"`,
DOCUMENT_CACHE: `"document-cache_${cacheVersion}"`,
},
watch: watch
? {
onRebuild(error, buildResult) {
const warnings = error?.warnings || buildResult?.warnings;
const errors = error?.errors || buildResult?.errors;
if (warnings.length) {
console.log(esbuild.formatMessages(warnings, { kind: "warning" }));
}
if (errors.length) {
console.log(esbuild.formatMessages(errors, { kind: "error" }));
process.exit(1);
}
console.log("Service worker rebuilt successfully");
},
}
: false,
})
.then(({ errors, warnings }) => {
if (warnings.length) {
console.log(esbuild.formatMessages(warnings, { kind: "warning" }));
}
if (errors.length) {
console.log(esbuild.formatMessages(errors, { kind: "error" }));
process.exit(1);
}
console.log("Service worker build succeeded");
})
.catch((err) => {
console.error(err);
process.exit(1);
});