This repository has been archived on 2026-09-13. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
BPOS-TMP-REPO/apps/web/vite.config.ts
T
ChaiHaoandClaude Opus 5 2b1d983678 Web 开发服务器显式绑 IPv4 回环 —— 127.0.0.1 从前直接连不上
不给 `server.host` 时 vite 走 Node 默认解析,在本机上**只绑到 `[::1]`**:
`http://127.0.0.1:5273` 连不上,而 `localhost` 通不通取决于浏览器/系统把它解析成哪个栈。
同一台机器上表现为「有时打得开有时打不开」,查起来极费劲。

绑 `127.0.0.1` 之后两种写法都通,且不暴露到局域网(要给别的设备访问再显式 `--host`,
那是另一个决定)。代理目标也一并从 `localhost:8787` 换成 `127.0.0.1:8787`,
免得同一处歧义在代理侧再来一遍。

实测:改前 `127.0.0.1:5273` → 连不上、`[::1]:5273` → 200;
改后 `127.0.0.1` 与 `localhost` 都 200,且只靠配置生效、不必带命令行参数。

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KeDBx2PHLavbURXB8LJJ7m
2026-07-28 14:07:00 +08:00

38 lines
1.3 KiB
TypeScript

import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import { fileURLToPath } from 'node:url';
const root = fileURLToPath(new URL('../..', import.meta.url));
const p = (rel: string) => fileURLToPath(new URL(rel, import.meta.url));
export default defineConfig({
plugins: [react()],
resolve: {
alias: {
'@contracts': `${root}packages/contracts/src/index.ts`,
'@sfeel': `${root}packages/sfeel/src/index.ts`,
'@ui': `${root}packages/ui-kit/src/index.ts`,
'@utils': `${root}packages/utils/src/index.ts`,
'@features': p('./src/features'),
'@web-shared': p('./src/shared'),
'@contracts/': `${root}packages/contracts/src/`,
},
},
server: {
port: 5173,
/*
* 显式绑 IPv4 回环。
*
* 不给 host 时 vite 走 Node 的默认解析,在本机上只绑到 `[::1]` —— 于是
* `http://127.0.0.1:5273` 直接连不上,而 `localhost` 通不通取决于浏览器/系统
* 把它解析成哪个栈:同一台机器上时好时坏,查起来极费劲(实测栽过一次)。
* 绑 `127.0.0.1` 之后 `localhost` 与 `127.0.0.1` 都通,且不暴露到局域网
* (要给别的设备访问再显式 `--host`,那是另一个决定)。
*/
host: '127.0.0.1',
proxy: {
'/api': { target: 'http://127.0.0.1:8787', changeOrigin: true },
},
},
});