|
|
@@ -27,7 +27,12 @@ const viteConfig = defineConfig((mode: ConfigEnv) => {
|
|
|
const devPort = Number(env.VITE_PORT) || 8888;
|
|
|
/** 浏览器访问用公网 IP/域名;勿写入 server.hmr.host,否则 WS 会 bind 该地址,云主机 EIP 常不在网卡上 → EADDRNOTAVAIL */
|
|
|
const devPublicHost = String(env.VITE_DEV_PUBLIC_HOST || '').trim();
|
|
|
- fs.writeFileSync('./public/config.js', `window.__env__ = ${JSON.stringify(env, null, 2)} `);
|
|
|
+ const runtimeConfigJs = `window.__env__ = ${JSON.stringify(env, null, 2)} `;
|
|
|
+ // dev(serve)才写 public/config.js,避免 build 把生产 env 写回本地开发态文件(如 VITE_API_URL=/prod-api 会让 dev 端口 404)。
|
|
|
+ // build 产物中的 dist/config.js 由下面的 emit-runtime-config-js 插件在打包完成后直接写入 dist/,不经过 public/。
|
|
|
+ if (mode.command === 'serve') {
|
|
|
+ fs.writeFileSync('./public/config.js', runtimeConfigJs);
|
|
|
+ }
|
|
|
return {
|
|
|
plugins: [
|
|
|
visualizer({ open: false }),
|
|
|
@@ -57,6 +62,15 @@ const viteConfig = defineConfig((mode: ConfigEnv) => {
|
|
|
threshold: 5120,
|
|
|
skipIfLargerOrEqual: true,
|
|
|
}),
|
|
|
+ {
|
|
|
+ name: 'emit-runtime-config-js',
|
|
|
+ apply: 'build' as const,
|
|
|
+ writeBundle(options: { dir?: string }) {
|
|
|
+ const outDir = options.dir || resolve(__dirname, 'dist');
|
|
|
+ if (!fs.existsSync(outDir)) fs.mkdirSync(outDir, { recursive: true });
|
|
|
+ fs.writeFileSync(resolve(outDir, 'config.js'), runtimeConfigJs);
|
|
|
+ },
|
|
|
+ },
|
|
|
]
|
|
|
: []),
|
|
|
JSON.parse(env.VITE_OPEN_CDN) ? buildConfig.cdn() : null,
|