This commit is contained in:
张传龙 2023-06-27 17:16:56 +08:00
commit 0f9fb9f1c9
9 changed files with 44 additions and 61 deletions

View File

@ -5,10 +5,7 @@ VITE_PUBLIC_PATH = '/'
VITE_USE_MOCK = true VITE_USE_MOCK = true
# 是否启用MOCK # 是否启用MOCK
VITE_USE_PROXY = false VITE_USE_PROXY = true
# 代理类型(跟启动和构建环境无关) 'dev' | 'test' | 'prod'
VITE_PROXY_TYPE = 'dev'
# base api # base api
VITE_BASE_API = '/api' VITE_BASE_API = '/api'

View File

@ -1,13 +0,0 @@
import dayjs from 'dayjs'
/**
* * 此处定义的是全局常量启动或打包后将添加到window中
* https://vitejs.cn/config/#define
*/
// 项目构建时间
const _BUILD_TIME_ = JSON.stringify(dayjs().format('YYYY-MM-DD HH:mm:ss'))
export const viteDefine = {
_BUILD_TIME_,
}

View File

@ -1,2 +0,0 @@
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
}

View File

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

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: '/api',
target: 'http://localhost:8080',
},
test: {
prefix: '/api',
target: 'http://localhost:8080',
},
prod: {
prefix: '/api',
target: 'http://localhost:8080',
},
}
export function getProxyConfig(envType = 'dev') {
return proxyConfigMappings[envType]
}

View File

@ -1,7 +1,7 @@
<template> <template>
<footer f-c-c flex-col text-14 color="#6a6a6a"> <footer f-c-c flex-col text-14 color="#6a6a6a">
<p> <p>
Copyright©2022 Copyright©2023
<a <a
href="https://github.com/zclzone" href="https://github.com/zclzone"
target="__blank" target="__blank"

View File

@ -1,9 +1,8 @@
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 { createVitePlugins } from './build/plugin' import { createVitePlugins } from './build/plugin'
import { OUTPUT_DIR } from './build/constant' import { OUTPUT_DIR, PROXY_CONFIG } from './build/constant'
export default defineConfig(({ command, mode }) => { export default defineConfig(({ command, mode }) => {
const srcPath = getSrcPath() const srcPath = getSrcPath()
@ -12,7 +11,7 @@ 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 || '/',
@ -22,17 +21,21 @@ export default defineConfig(({ command, mode }) => {
'@': srcPath, '@': srcPath,
}, },
}, },
define: viteDefine,
plugins: createVitePlugins(viteEnv, isBuild), plugins: createVitePlugins(viteEnv, isBuild),
server: { server: {
host: '0.0.0.0', host: '0.0.0.0',
port: VITE_PORT, port: VITE_PORT,
open: false, open: false,
proxy: createViteProxy(VITE_USE_PROXY, VITE_PROXY_TYPE), proxy: VITE_USE_PROXY
? {
[VITE_BASE_API]: PROXY_CONFIG[VITE_BASE_API],
'/api/v2': PROXY_CONFIG['/api/v2'],
}
: undefined,
}, },
build: { build: {
target: 'es2015', target: 'es2015',
outDir: OUTPUT_DIR, outDir: OUTPUT_DIR || 'dist',
reportCompressedSize: false, // 启用/禁用 gzip 压缩大小报告 reportCompressedSize: false, // 启用/禁用 gzip 压缩大小报告
chunkSizeWarningLimit: 1024, // chunk 大小警告的限制单位kb chunkSizeWarningLimit: 1024, // chunk 大小警告的限制单位kb
}, },