fix: 手动打包 Proxy 问题(#91)

* perf: 检查代码

* feat: proxy setting

* chore: 调整为测试环境使用 `proxy`
This commit is contained in:
Redon 2023-02-22 14:29:05 +08:00 committed by GitHub
parent 66cecb6049
commit bf5c0cdf04
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 28 additions and 16 deletions

5
.env
View File

@ -1,6 +1,5 @@
# Glob API URL # Glob API URL
VITE_GLOB_API_URL=/api VITE_GLOB_API_URL=http://localhost:3002
VITE_APP_API_BASE_URL=http://localhost:3002/
# Glob API Timeout (ms)
VITE_GLOB_API_TIMEOUT=100000 VITE_GLOB_API_TIMEOUT=100000

1
.env.development Normal file
View File

@ -0,0 +1 @@
VITE_GLOB_HTTP_PROXY=Y

1
config/index.ts Normal file
View File

@ -0,0 +1 @@
export * from './proxy'

16
config/proxy.ts Normal file
View File

@ -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
}

View File

@ -3,5 +3,5 @@
interface ImportMetaEnv { interface ImportMetaEnv {
readonly VITE_GLOB_API_URL: string; readonly VITE_GLOB_API_URL: string;
readonly VITE_GLOB_API_TIMEOUT: string; readonly VITE_GLOB_API_TIMEOUT: string;
readonly VITE_APP_API_BASE_URL: string; readonly VITE_GLOB_HTTP_PROXY: 'Y' | 'N';
} }

View File

@ -1,8 +1,6 @@
function includeCode(text: string | null | undefined) { function includeCode(text: string | null | undefined) {
const regexp = /^(?:\s{4}|\t).+/gm const regexp = /^(?:\s{4}|\t).+/gm
if (text?.includes(' = ') || text?.match(regexp)) return !!(text?.includes(' = ') || text?.match(regexp))
return true
return false
} }
export default includeCode export default includeCode

View File

@ -1,7 +1,7 @@
import axios, { type AxiosResponse } from 'axios' import axios, { type AxiosResponse } from 'axios'
const service = axios.create({ 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, timeout: !isNaN(+import.meta.env.VITE_GLOB_API_TIMEOUT) ? Number(import.meta.env.VITE_GLOB_API_TIMEOUT) : 60 * 1000,
}) })

View File

@ -1,10 +1,13 @@
import path from 'path' import path from 'path'
import { defineConfig, loadEnv } from 'vite' import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue' import vue from '@vitejs/plugin-vue'
import { createViteProxy } from './config'
export default defineConfig((env) => { export default defineConfig((env) => {
const viteEnv = loadEnv(env.mode, process.cwd()) as unknown as ImportMetaEnv const viteEnv = loadEnv(env.mode, process.cwd()) as unknown as ImportMetaEnv
const isOpenProxy = viteEnv.VITE_GLOB_HTTP_PROXY === 'Y'
return { return {
resolve: { resolve: {
alias: { alias: {
@ -13,16 +16,10 @@ export default defineConfig((env) => {
}, },
plugins: [vue()], plugins: [vue()],
server: { server: {
port: 1002,
host: '0.0.0.0', host: '0.0.0.0',
port: 1002,
open: false, open: false,
proxy: { proxy: createViteProxy(isOpenProxy, viteEnv),
'/api': {
target: viteEnv.VITE_APP_API_BASE_URL,
changeOrigin: true, // 允许跨域
rewrite: path => path.replace('/api/', '/'),
},
},
}, },
build: { build: {
reportCompressedSize: false, reportCompressedSize: false,