UnisKB/ui/vite.config.ts

111 lines
3.1 KiB
TypeScript
Raw Normal View History

2025-04-14 12:11:23 +00:00
import { fileURLToPath, URL } from 'node:url'
2025-04-16 11:10:00 +00:00
import type { ProxyOptions } from 'vite'
import { defineConfig, loadEnv } from 'vite'
2025-04-14 12:11:23 +00:00
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
2025-04-16 11:10:00 +00:00
import DefineOptions from 'unplugin-vue-define-options/vite'
2025-06-13 08:17:02 +00:00
import path from 'path'
import { createHtmlPlugin } from 'vite-plugin-html'
2025-07-01 07:43:00 +00:00
import fs from 'fs'
2025-04-16 11:10:00 +00:00
// import vueDevTools from 'vite-plugin-vue-devtools'
const envDir = './env'
2025-07-01 07:43:00 +00:00
// 自定义插件:重命名入口文件
const renameHtmlPlugin = (outDir: string, entry: string) => {
return {
name: 'rename-html',
closeBundle: () => {
const buildDir = path.resolve(__dirname, outDir)
const oldFile = path.join(buildDir, entry)
const newFile = path.join(buildDir, 'index.html')
2025-04-14 12:11:23 +00:00
2025-07-01 07:43:00 +00:00
// 检查文件是否存在
if (fs.existsSync(oldFile)) {
// 删除已存在的 index.html
if (fs.existsSync(newFile)) {
fs.unlinkSync(newFile)
}
// 重命名文件
fs.renameSync(oldFile, newFile)
}
},
}
}
2025-04-14 12:11:23 +00:00
// https://vite.dev/config/
2025-07-01 07:43:00 +00:00
export default defineConfig((conf: any) => {
const mode = conf.mode
2025-04-16 11:10:00 +00:00
const ENV = loadEnv(mode, envDir)
const proxyConf: Record<string, string | ProxyOptions> = {}
2025-07-01 07:43:00 +00:00
proxyConf['/admin/api'] = {
2025-07-02 10:37:15 +00:00
target: 'http://127.0.0.1:8080',
2025-04-16 11:10:00 +00:00
changeOrigin: true,
2025-06-06 04:31:30 +00:00
}
2025-06-17 03:58:35 +00:00
proxyConf['/chat/api'] = {
2025-06-16 04:08:09 +00:00
target: 'http://127.0.0.1:8080',
changeOrigin: true,
2025-04-16 11:10:00 +00:00
}
proxyConf['/doc'] = {
target: 'http://127.0.0.1:8080',
changeOrigin: true,
2025-06-13 08:17:02 +00:00
rewrite: (path: string) => path.replace(ENV.VITE_BASE_PATH, '/'),
2025-04-16 11:10:00 +00:00
}
proxyConf['/schema'] = {
target: 'http://127.0.0.1:8080',
changeOrigin: true,
rewrite: (path: string) => path.replace(ENV.VITE_BASE_PATH, '/'),
}
2025-04-16 11:10:00 +00:00
proxyConf['/static'] = {
target: 'http://127.0.0.1:8080',
changeOrigin: true,
2025-06-13 08:17:02 +00:00
rewrite: (path: string) => path.replace(ENV.VITE_BASE_PATH, '/'),
2025-04-16 11:10:00 +00:00
}
2025-07-02 13:02:45 +00:00
// 前端静态资源转发到本身
proxyConf[`^${ENV.VITE_BASE_PATH}.+\/oss\/file\/.*$`] = {
target: `http://127.0.0.1:8080`,
changeOrigin: true,
}
// 前端静态资源转发到本身
proxyConf[`^${ENV.VITE_BASE_PATH}oss\/file\/.*$`] = {
target: `http://127.0.0.1:8080`,
changeOrigin: true,
}
2025-07-02 08:58:51 +00:00
// 前端静态资源转发到本身
proxyConf[ENV.VITE_BASE_PATH] = {
target: `http://127.0.0.1:${ENV.VITE_APP_PORT}`,
changeOrigin: true,
rewrite: (path: string) => path.replace(ENV.VITE_BASE_PATH, '/'),
}
2025-07-01 07:43:00 +00:00
2025-04-16 11:10:00 +00:00
return {
preflight: false,
lintOnSave: false,
2025-07-01 07:43:00 +00:00
base: './',
2025-04-16 11:10:00 +00:00
envDir: envDir,
2025-07-01 07:43:00 +00:00
plugins: [
vue(),
vueJsx(),
DefineOptions(),
createHtmlPlugin({ template: ENV.VITE_ENTRY }),
renameHtmlPlugin(`dist${ENV.VITE_BASE_PATH}`, ENV.VITE_ENTRY),
],
2025-04-16 11:10:00 +00:00
server: {
cors: true,
host: '0.0.0.0',
port: Number(ENV.VITE_APP_PORT),
strictPort: true,
proxy: proxyConf,
},
build: {
2025-06-13 08:17:02 +00:00
outDir: `dist${ENV.VITE_BASE_PATH}`,
rollupOptions: {
2025-07-01 07:43:00 +00:00
input: ENV.VITE_ENTRY,
2025-06-13 08:17:02 +00:00
},
2025-04-16 11:10:00 +00:00
},
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url)),
},
2025-04-14 12:11:23 +00:00
},
2025-04-16 11:10:00 +00:00
}
2025-04-14 12:11:23 +00:00
})