Skip to content

Commit

Permalink
Merge pull request #4 from kobe-koto/main
Browse files Browse the repository at this point in the history
Dynamic config.json from environments
  • Loading branch information
wuwuwu223 authored Dec 7, 2024
2 parents dd4cb6d + 4caf503 commit 006b3ab
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
4 changes: 0 additions & 4 deletions public/config.json

This file was deleted.

27 changes: 27 additions & 0 deletions vite.config.js
Original file line number Diff line number Diff line change
@@ -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: {
Expand Down

0 comments on commit 006b3ab

Please sign in to comment.