62 lines
2.4 KiB
TypeScript
62 lines
2.4 KiB
TypeScript
import { readFileSync } from 'node:fs'
|
|
import { fileURLToPath } from 'node:url'
|
|
import path from 'node:path'
|
|
import { defineConfig, type Plugin } from 'vite'
|
|
|
|
const rootDir = path.dirname(fileURLToPath(import.meta.url))
|
|
const engineDir = path.resolve(rootDir, '../engine')
|
|
const versionPath = path.resolve(rootDir, 'version.json')
|
|
|
|
// Read once per config evaluation — a dev-server restart (see restartOnVersionJsonChange below)
|
|
// re-imports this whole file, so a version bump is picked up by simply restarting, without any
|
|
// extra caching/invalidation logic here.
|
|
const version = JSON.parse(readFileSync(versionPath, 'utf-8')) as string;
|
|
|
|
// version.json isn't part of the app's module graph (nothing imports it as JS), so editing it
|
|
// doesn't trigger HMR or Vite's own config-file-triggered restart — only vite.config.ts itself is
|
|
// watched for that. This plugin explicitly watches version.json too and forces a full restart on
|
|
// change, so __APP_VERSION__ (set from `version` below) picks up a version bump without having to
|
|
// stop/start `npm run dev` by hand.
|
|
function restartOnVersionJsonChange(): Plugin {
|
|
return {
|
|
name: 'restart-on-version-json-change',
|
|
configureServer(server) {
|
|
server.watcher.add(versionPath)
|
|
server.watcher.on('change', (file) => {
|
|
if (path.normalize(file) === path.normalize(versionPath)) {
|
|
server.config.logger.info('version.json changed, restarting to pick up __APP_VERSION__...', { clear: false })
|
|
server.restart()
|
|
}
|
|
})
|
|
},
|
|
}
|
|
}
|
|
|
|
// https://vite.dev/config/
|
|
export default defineConfig({
|
|
plugins: [restartOnVersionJsonChange()],
|
|
define: {
|
|
__APP_VERSION__: JSON.stringify(version),
|
|
},
|
|
resolve: {
|
|
// Follow the node_modules symlink to the real sibling directory, so Vite's dev server
|
|
// watches and transforms the actual engine source files (not a frozen pre-bundled copy).
|
|
preserveSymlinks: false,
|
|
},
|
|
optimizeDeps: {
|
|
// Treat @incgame/engine as first-party source, not a vendor dependency to pre-bundle once
|
|
// and cache — otherwise edits to the engine package wouldn't trigger HMR until a full
|
|
// dev-server restart.
|
|
exclude: ['@incgame/engine'],
|
|
},
|
|
server: {
|
|
host: '0.0.0.0',
|
|
port: 8877,
|
|
fs: {
|
|
// The engine package lives outside this project's root (a sibling directory reached
|
|
// through a node_modules symlink); allow Vite to serve/watch files from there too.
|
|
allow: [rootDir, engineDir],
|
|
},
|
|
},
|
|
})
|