Skip to content

Commit c40e08e

Browse files
committed
refactor: use env vars to set API URL and dev port
1 parent f799156 commit c40e08e

File tree

6 files changed

+37
-37
lines changed

6 files changed

+37
-37
lines changed

.env

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1-
VITE_USE_PRODUCTION_API=true
1+
# 后端接口地址
2+
VITE_API=https://prts.maa.plus
3+
4+
# 地图站地址
25
VITE_THERESA_SERVER=https://theresa.wiki

.env.development

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# 本地开发端口
2+
PORT=3000
3+
4+
# 如果需要使用生产环境的API,请把下面一行注释掉,或者在.env.development.local中覆盖
5+
VITE_API=https://maa-docker.kkdy.tech

README.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ MAA 作业站前端!
44

55
## 开发流程
66

7+
后端接口文档:https://maa-docker.kkdy.tech/swagger-ui/index.html
8+
79
该仓库的主分支为 `dev`,线上分支为 `main`,代码合并到 `main` 后将会自动部署到线上
810

911
在自己的 fork 上开发完成后请提交 PR 到 `dev` 分支,由管理员合并
@@ -12,15 +14,9 @@ MAA 作业站前端!
1214

1315
## 环境变量
1416

15-
环境变量在 `.env` 文件内定义,你也可以创建 `.env.local` 来覆盖 `.env` 的配置
16-
17-
```ini
18-
# 使用线上的后端 API,否则使用本地开发服务器
19-
VITE_USE_PRODUCTION_API=true
17+
环境变量定义在 `.env` `.dev.development` 文件内
2018

21-
# 地图服务器,如果正在对地图进行本地开发请改为例如 http://localhost:3001
22-
VITE_THERESA_SERVER=https://theresa.wiki
23-
```
19+
你可以创建 `.env.development.local` 文件来覆盖环境变量,优先级为 `.env.development.local` > `.env.development` > `.env`
2420

2521
## 命令
2622

src/utils/envvar.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/utils/fetcher.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ import unfetch from 'unfetch'
44

55
import { Response } from 'models/network'
66

7-
import { envUseProductionApi } from './envvar'
8-
97
const fetch = window.fetch || unfetch
108

119
export class NetworkError extends Error {}
1210

13-
const baseURL = envUseProductionApi
14-
? 'https://prts.maa.plus'
15-
: 'http://localhost:5259'
11+
if (!import.meta.env.VITE_API) {
12+
throw new Error('env var VITE_API is not set')
13+
}
14+
15+
const baseURL = import.meta.env.VITE_API as string
1616

1717
export const FETCHER_CONFIG: {
1818
apiToken?: () => Promise<string | undefined>

vite.config.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
import react from '@vitejs/plugin-react'
22

3-
import { defineConfig } from 'vite'
3+
import { defineConfig, loadEnv } from 'vite'
44
import viteTsconfigPath from 'vite-tsconfig-paths'
55

66
// https://vitejs.dev/config/
7-
export default defineConfig({
8-
plugins: [react(), viteTsconfigPath()],
9-
server: {
10-
port: 3000,
11-
},
12-
resolve: {
13-
alias: {
14-
src: require('path').resolve(__dirname, 'src'),
7+
export default defineConfig(({ command, mode }) => {
8+
// Load env file based on `mode` in the current working directory.
9+
// Set the third parameter to '' to load all env regardless of the `VITE_` prefix.
10+
const env = loadEnv(mode, process.cwd(), '')
11+
12+
return {
13+
plugins: [react(), viteTsconfigPath()],
14+
server: {
15+
port: +env.PORT || undefined,
16+
},
17+
resolve: {
18+
alias: {
19+
src: require('path').resolve(__dirname, 'src'),
20+
},
21+
},
22+
build: {
23+
sourcemap: true,
1524
},
16-
},
17-
build: {
18-
sourcemap: true,
19-
},
25+
}
2026
})

0 commit comments

Comments
 (0)