prelöoad experiment
This commit is contained in:
45
scripts/dev.mjs
Normal file
45
scripts/dev.mjs
Normal file
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
* Dev with full PWA precache: production build --watch + preview.
|
||||
* Vite HMR cannot precache in-memory modules; use npm run dev:hmr 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);
|
||||
Reference in New Issue
Block a user