This commit is contained in:
mizhexiaoxiao 2023-08-17 14:32:31 +08:00
parent 52bd125538
commit fbcdb3ca4d
10 changed files with 38 additions and 47 deletions

View File

@ -1,3 +1,3 @@
VITE_TITLE = 'Vue FastAPI Admin' VITE_TITLE = 'Vue FastAPI Admin'
VITE_PORT = 3100 VITE_PORT = 3100

View File

@ -4,5 +4,5 @@ VITE_PUBLIC_PATH = '/'
# 是否启用代理 # 是否启用代理
VITE_USE_PROXY = true VITE_USE_PROXY = true
# 代理类型 'dev' | 'test' | 'prod' # base api
VITE_PROXY_TYPE = 'dev' VITE_BASE_API = '/api'

1
web/.gitignore vendored
View File

@ -13,6 +13,7 @@ dist
dist-ssr dist-ssr
coverage coverage
*.local *.local
stats.html
# Editor directories and files # Editor directories and files
.vscode/* .vscode/*

View File

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

View File

@ -1,15 +0,0 @@
import { getProxyConfig } from '../../settings'
export function createViteProxy(isUseProxy = true, proxyType) {
if (!isUseProxy) return undefined
const proxyConfig = getProxyConfig(proxyType)
const proxy = {
[proxyConfig.prefix]: {
target: proxyConfig.target,
changeOrigin: true,
rewrite: (path) => path.replace(new RegExp(`^${proxyConfig.prefix}`), ''),
},
}
return proxy
}

23
web/build/constant.js Normal file
View File

@ -0,0 +1,23 @@
export const OUTPUT_DIR = 'dist'
export const PROXY_CONFIG = {
/**
* @desc 替换匹配值
* @请求路径 http://localhost:3100/api/user
* @转发路径 http://localhost:9999/api/v1 +/user
*/
'/api': {
target: 'http://localhost:9999/api/v1',
changeOrigin: true,
rewrite: (path) => path.replace(new RegExp('^/api'), ''),
},
// /**
// * @desc 不替换匹配值
// * @请求路径 http://localhost:3100/api/v1/user
// * @转发路径 http://localhost:9999/api/v1/user
// */
// '/api/v1': {
// target: 'http://localhost:9999',
// changeOrigin: true,
// },
}

View File

@ -1,2 +1 @@
export * from './theme.json' export * from './theme.json'
export * from './proxy-config'

View File

@ -1,18 +0,0 @@
const proxyConfigMappings = {
dev: {
prefix: '/url-patten',
target: 'http://127.0.0.1:9999/api/v1',
},
test: {
prefix: '/url-patten',
target: 'http://127.0.0.1:9999',
},
prod: {
prefix: '/url-patten',
target: 'http://127.0.0.1:9999',
},
}
export function getProxyConfig(envType = 'dev') {
return proxyConfigMappings[envType]
}

View File

@ -1,9 +1,5 @@
import axios from 'axios' import axios from 'axios'
import { resReject, resResolve, reqReject, reqResolve } from './interceptors' import { resReject, resResolve, reqReject, reqResolve } from './interceptors'
import { getProxyConfig } from '../../../settings'
const proxyConfig = getProxyConfig(import.meta.env.VITE_PROXY_TYPE)
const isUseProxy = false
export function createAxios(options = {}) { export function createAxios(options = {}) {
const defaultOptions = { const defaultOptions = {
@ -19,5 +15,5 @@ export function createAxios(options = {}) {
} }
export const request = createAxios({ export const request = createAxios({
baseURL: isUseProxy ? proxyConfig.prefix : proxyConfig.target, baseURL: import.meta.env.VITE_BASE_API,
}) })

View File

@ -1,8 +1,9 @@
import { defineConfig, loadEnv } from 'vite' import { defineConfig, loadEnv } from 'vite'
import { convertEnv, getSrcPath, getRootPath } from './build/utils' import { convertEnv, getSrcPath, getRootPath } from './build/utils'
import { createViteProxy, viteDefine } from './build/config' import { viteDefine } from './build/config'
import { createVitePlugins } from './build/plugin' import { createVitePlugins } from './build/plugin'
import { OUTPUT_DIR, PROXY_CONFIG } from './build/constant'
export default defineConfig(({ command, mode }) => { export default defineConfig(({ command, mode }) => {
const srcPath = getSrcPath() const srcPath = getSrcPath()
@ -11,7 +12,8 @@ export default defineConfig(({ command, mode }) => {
const env = loadEnv(mode, process.cwd()) const env = loadEnv(mode, process.cwd())
const viteEnv = convertEnv(env) const viteEnv = convertEnv(env)
const { VITE_PORT, VITE_PUBLIC_PATH, VITE_USE_PROXY, VITE_PROXY_TYPE } = viteEnv const { VITE_PORT, VITE_PUBLIC_PATH, VITE_USE_PROXY, VITE_BASE_API } = viteEnv
return { return {
base: VITE_PUBLIC_PATH || '/', base: VITE_PUBLIC_PATH || '/',
resolve: { resolve: {
@ -26,11 +28,15 @@ export default defineConfig(({ command, mode }) => {
host: '0.0.0.0', host: '0.0.0.0',
port: VITE_PORT, port: VITE_PORT,
open: true, open: true,
proxy: createViteProxy(VITE_USE_PROXY, VITE_PROXY_TYPE), proxy: VITE_USE_PROXY
? {
[VITE_BASE_API]: PROXY_CONFIG[VITE_BASE_API],
}
: undefined,
}, },
build: { build: {
target: 'es2015', target: 'es2015',
outDir: 'dist', outDir: OUTPUT_DIR || 'dist',
reportCompressedSize: false, // 启用/禁用 gzip 压缩大小报告 reportCompressedSize: false, // 启用/禁用 gzip 压缩大小报告
chunkSizeWarningLimit: 1024, // chunk 大小警告的限制单位kb chunkSizeWarningLimit: 1024, // chunk 大小警告的限制单位kb
}, },