46 lines
1.0 KiB
JavaScript
46 lines
1.0 KiB
JavaScript
/**
|
|
* Dev with full PWA precache: production build --watch + preview.
|
|
* Vite HMR cannot precache in-memory modules; use npm run dev for that.
|
|
*/
|
|
import { build, preview } from 'vite';
|
|
import path from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
const root = path.dirname(fileURLToPath(import.meta.url));
|
|
const configFile = path.resolve(root, '../vite.config.js');
|
|
|
|
const watcher = await build({
|
|
configFile,
|
|
build: { watch: {} },
|
|
});
|
|
|
|
let previewServer;
|
|
|
|
async function startPreview() {
|
|
if (previewServer) return;
|
|
previewServer = await preview({
|
|
configFile,
|
|
preview: { host: true },
|
|
});
|
|
previewServer.printUrls();
|
|
previewServer.bindCLIShortcuts({ print: true });
|
|
}
|
|
|
|
watcher.on('event', (event) => {
|
|
if (event.code === 'BUNDLE_END') {
|
|
void startPreview();
|
|
}
|
|
if (event.code === 'ERROR') {
|
|
console.error(event.error);
|
|
}
|
|
});
|
|
|
|
async function shutdown() {
|
|
await previewServer?.close();
|
|
await watcher.close();
|
|
process.exit(0);
|
|
}
|
|
|
|
process.on('SIGINT', shutdown);
|
|
process.on('SIGTERM', shutdown);
|