Skip to content

Commit

Permalink
fix: 手动打包 Proxy 问题(#91)
Browse files Browse the repository at this point in the history
* perf: 检查代码

* feat: proxy setting

* chore: 调整为测试环境使用 `proxy`
  • Loading branch information
Chanzhaoyu authored Feb 22, 2023
1 parent 66cecb6 commit bf5c0cd
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 16 deletions.
5 changes: 2 additions & 3 deletions .env
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Glob API URL
VITE_GLOB_API_URL=/api

VITE_APP_API_BASE_URL=http://localhost:3002/
VITE_GLOB_API_URL=http://localhost:3002

# Glob API Timeout (ms)
VITE_GLOB_API_TIMEOUT=100000
1 change: 1 addition & 0 deletions .env.development
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
VITE_GLOB_HTTP_PROXY=Y
1 change: 1 addition & 0 deletions config/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './proxy'
16 changes: 16 additions & 0 deletions config/proxy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { ProxyOptions } from 'vite'

export function createViteProxy(isOpenProxy: boolean, viteEnv: ImportMetaEnv) {
if (!isOpenProxy)
return

const proxy: Record<string, string | ProxyOptions> = {
'/api': {
target: viteEnv.VITE_GLOB_API_URL,
changeOrigin: true,
rewrite: path => path.replace(/^\/api/, ''),
},
}

return proxy
}
2 changes: 1 addition & 1 deletion src/typings/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
interface ImportMetaEnv {
readonly VITE_GLOB_API_URL: string;
readonly VITE_GLOB_API_TIMEOUT: string;
readonly VITE_APP_API_BASE_URL: string;
readonly VITE_GLOB_HTTP_PROXY: 'Y' | 'N';
}
4 changes: 1 addition & 3 deletions src/utils/functions/includeCode.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
function includeCode(text: string | null | undefined) {
const regexp = /^(?:\s{4}|\t).+/gm
if (text?.includes(' = ') || text?.match(regexp))
return true
return false
return !!(text?.includes(' = ') || text?.match(regexp))
}

export default includeCode
2 changes: 1 addition & 1 deletion src/utils/request/axios.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios, { type AxiosResponse } from 'axios'

const service = axios.create({
baseURL: import.meta.env.VITE_GLOB_API_URL,
baseURL: import.meta.env.VITE_GLOB_HTTP_PROXY === 'Y' ? '/api' : import.meta.env.VITE_GLOB_API_URL,
timeout: !isNaN(+import.meta.env.VITE_GLOB_API_TIMEOUT) ? Number(import.meta.env.VITE_GLOB_API_TIMEOUT) : 60 * 1000,
})

Expand Down
13 changes: 5 additions & 8 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import path from 'path'
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import { createViteProxy } from './config'

export default defineConfig((env) => {
const viteEnv = loadEnv(env.mode, process.cwd()) as unknown as ImportMetaEnv

const isOpenProxy = viteEnv.VITE_GLOB_HTTP_PROXY === 'Y'

return {
resolve: {
alias: {
Expand All @@ -13,16 +16,10 @@ export default defineConfig((env) => {
},
plugins: [vue()],
server: {
port: 1002,
host: '0.0.0.0',
port: 1002,
open: false,
proxy: {
'/api': {
target: viteEnv.VITE_APP_API_BASE_URL,
changeOrigin: true, // 允许跨域
rewrite: path => path.replace('/api/', '/'),
},
},
proxy: createViteProxy(isOpenProxy, viteEnv),
},
build: {
reportCompressedSize: false,
Expand Down

0 comments on commit bf5c0cd

Please sign in to comment.