anaytics
This commit is contained in:
+32
-2
@@ -1,14 +1,44 @@
|
||||
import { readFileSync } from 'node:fs'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
import path from 'node:path'
|
||||
import react from '@vitejs/plugin-react'
|
||||
import { defineConfig } from 'vite'
|
||||
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: [react()],
|
||||
plugins: [react(), 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).
|
||||
|
||||
Reference in New Issue
Block a user