Update
This commit is contained in:
parent
52bd125538
commit
fbcdb3ca4d
@ -4,5 +4,5 @@ VITE_PUBLIC_PATH = '/'
|
||||
# 是否启用代理
|
||||
VITE_USE_PROXY = true
|
||||
|
||||
# 代理类型 'dev' | 'test' | 'prod'
|
||||
VITE_PROXY_TYPE = 'dev'
|
||||
# base api
|
||||
VITE_BASE_API = '/api'
|
||||
|
||||
1
web/.gitignore
vendored
1
web/.gitignore
vendored
@ -13,6 +13,7 @@ dist
|
||||
dist-ssr
|
||||
coverage
|
||||
*.local
|
||||
stats.html
|
||||
|
||||
# Editor directories and files
|
||||
.vscode/*
|
||||
|
||||
@ -1,2 +1 @@
|
||||
export * from './define'
|
||||
export * from './proxy'
|
||||
|
||||
@ -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
23
web/build/constant.js
Normal 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,
|
||||
// },
|
||||
}
|
||||
@ -1,2 +1 @@
|
||||
export * from './theme.json'
|
||||
export * from './proxy-config'
|
||||
|
||||
@ -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]
|
||||
}
|
||||
@ -1,9 +1,5 @@
|
||||
import axios from 'axios'
|
||||
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 = {}) {
|
||||
const defaultOptions = {
|
||||
@ -19,5 +15,5 @@ export function createAxios(options = {}) {
|
||||
}
|
||||
|
||||
export const request = createAxios({
|
||||
baseURL: isUseProxy ? proxyConfig.prefix : proxyConfig.target,
|
||||
baseURL: import.meta.env.VITE_BASE_API,
|
||||
})
|
||||
|
||||
@ -1,8 +1,9 @@
|
||||
import { defineConfig, loadEnv } from 'vite'
|
||||
|
||||
import { convertEnv, getSrcPath, getRootPath } from './build/utils'
|
||||
import { createViteProxy, viteDefine } from './build/config'
|
||||
import { viteDefine } from './build/config'
|
||||
import { createVitePlugins } from './build/plugin'
|
||||
import { OUTPUT_DIR, PROXY_CONFIG } from './build/constant'
|
||||
|
||||
export default defineConfig(({ command, mode }) => {
|
||||
const srcPath = getSrcPath()
|
||||
@ -11,7 +12,8 @@ export default defineConfig(({ command, mode }) => {
|
||||
|
||||
const env = loadEnv(mode, process.cwd())
|
||||
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 {
|
||||
base: VITE_PUBLIC_PATH || '/',
|
||||
resolve: {
|
||||
@ -26,11 +28,15 @@ export default defineConfig(({ command, mode }) => {
|
||||
host: '0.0.0.0',
|
||||
port: VITE_PORT,
|
||||
open: true,
|
||||
proxy: createViteProxy(VITE_USE_PROXY, VITE_PROXY_TYPE),
|
||||
proxy: VITE_USE_PROXY
|
||||
? {
|
||||
[VITE_BASE_API]: PROXY_CONFIG[VITE_BASE_API],
|
||||
}
|
||||
: undefined,
|
||||
},
|
||||
build: {
|
||||
target: 'es2015',
|
||||
outDir: 'dist',
|
||||
outDir: OUTPUT_DIR || 'dist',
|
||||
reportCompressedSize: false, // 启用/禁用 gzip 压缩大小报告
|
||||
chunkSizeWarningLimit: 1024, // chunk 大小警告的限制(单位kb)
|
||||
},
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user