diff --git a/public/config.json b/public/config.json deleted file mode 100644 index 27da73e..0000000 --- a/public/config.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "socket": "ws://192.168.31.64:3000/ws", - "apiURL": "http://192.168.31.64:3000" -} \ No newline at end of file diff --git a/vite.config.js b/vite.config.js index 4217010..b914255 100644 --- a/vite.config.js +++ b/vite.config.js @@ -1,14 +1,41 @@ import { fileURLToPath, URL } from 'node:url' +import { writeFileSync } from 'node:fs' +import path from 'path'; import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' import vueDevTools from 'vite-plugin-vue-devtools' +const generateConfig = () => ({ + socket: process.env.SOCKETURL || "ws://192.168.31.64:3000/ws", + apiURL: process.env.APIURL || "http://192.168.31.64:3000", +}); + // https://vite.dev/config/ export default defineConfig({ plugins: [ vue(), vueDevTools(), + { + name: 'dynamic-config-json', + configureServer (server) { + // dynamic `config.json` for dev + server.middlewares.use((req, res, next) => { + if (req.url === '/config.json') { + res.setHeader('Content-Type', 'application/json'); + res.end(JSON.stringify(generateConfig())); + } else { + next(); + } + }); + }, + closeBundle () { + // static `config.json` for prod + const configPath = path.resolve(__dirname, 'dist/config.json'); + writeFileSync(configPath, JSON.stringify(generateConfig(), null, 2)); + console.log('Generated config.json:', generateConfig()); + }, + }, ], resolve: { alias: {