-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.config.ts
46 lines (46 loc) · 1.7 KB
/
vite.config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { defineConfig, loadEnv } from "vite";
import react from "@vitejs/plugin-react";
import { resolve } from "path";
// const ORIGIN_SERVER = import.meta.env.VITE_ORIGIN_SERVER;
// https://vitejs.dev/config/
// Vite 默认是不加载 .env 文件的,因为这些文件需要在执行完 Vite 配置后才能确定加载哪一个
export default defineConfig(({ command, mode }) => {
// 根据当前工作目录中的 `mode` 加载 .env 文件
// 设置第三个参数为 '' 来加载所有环境变量,而不管是否有 `VITE_` 前缀。
const env = loadEnv(mode, process.cwd(), "");
return {
plugins: [react()],
resolve: {
// 配置别名 减少查找模块时间消耗
alias: [
{
find: "@",
replacement: resolve(__dirname, "./src"),
},
],
},
server: {
proxy: {
// string shorthand: http://localhost:5173/foo -> http://localhost:4567/foo
"/foo": "http://localhost:4567",
// with options: http://localhost:5173/api/bar-> http://jsonplaceholder.typicode.com/bar
"/api": {
target: env.VITE_ORIGIN_SERVER,
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, ""),
},
// with RegEx: http://localhost:5173/fallback/ -> http://jsonplaceholder.typicode.com/
"^/fallback/.*": {
target: "http://jsonplaceholder.typicode.com",
changeOrigin: true,
rewrite: (path) => path.replace(/^\/fallback/, ""),
},
// Proxying websockets or socket.io: ws://localhost:5173/socket.io -> ws://localhost:5174/socket.io
"/socket.io": {
target: "ws://localhost:5174",
ws: true,
},
},
},
};
});