|
@@ -7,9 +7,12 @@ import DefineOptions from 'unplugin-vue-define-options/vite'
|
|
|
import path from 'path'
|
|
import path from 'path'
|
|
|
import {createHtmlPlugin} from 'vite-plugin-html'
|
|
import {createHtmlPlugin} from 'vite-plugin-html'
|
|
|
import fs from 'fs'
|
|
import fs from 'fs'
|
|
|
-// import vueDevTools from 'vite-plugin-vue-devtools'
|
|
|
|
|
|
|
+
|
|
|
|
|
+const __filename = fileURLToPath(import.meta.url)
|
|
|
|
|
+const __dirname = path.dirname(__filename)
|
|
|
|
|
+
|
|
|
const envDir = './env'
|
|
const envDir = './env'
|
|
|
-// 自定义插件:重命名入口文件
|
|
|
|
|
|
|
+
|
|
|
const renameHtmlPlugin = (outDir: string, entry: string) => {
|
|
const renameHtmlPlugin = (outDir: string, entry: string) => {
|
|
|
return {
|
|
return {
|
|
|
name: 'rename-html',
|
|
name: 'rename-html',
|
|
@@ -18,22 +21,23 @@ const renameHtmlPlugin = (outDir: string, entry: string) => {
|
|
|
const oldFile = path.join(buildDir, entry)
|
|
const oldFile = path.join(buildDir, entry)
|
|
|
const newFile = path.join(buildDir, 'index.html')
|
|
const newFile = path.join(buildDir, 'index.html')
|
|
|
|
|
|
|
|
- // 检查文件是否存在
|
|
|
|
|
if (fs.existsSync(oldFile)) {
|
|
if (fs.existsSync(oldFile)) {
|
|
|
- // 删除已存在的 index.html
|
|
|
|
|
if (fs.existsSync(newFile)) {
|
|
if (fs.existsSync(newFile)) {
|
|
|
fs.unlinkSync(newFile)
|
|
fs.unlinkSync(newFile)
|
|
|
}
|
|
}
|
|
|
- // 重命名文件
|
|
|
|
|
fs.renameSync(oldFile, newFile)
|
|
fs.renameSync(oldFile, newFile)
|
|
|
}
|
|
}
|
|
|
},
|
|
},
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-// https://vite.dev/config/
|
|
|
|
|
|
|
+
|
|
|
export default defineConfig((conf: any) => {
|
|
export default defineConfig((conf: any) => {
|
|
|
const mode = conf.mode
|
|
const mode = conf.mode
|
|
|
const ENV = loadEnv(mode, envDir)
|
|
const ENV = loadEnv(mode, envDir)
|
|
|
|
|
+
|
|
|
|
|
+ const basePath = ENV.VITE_BASE_PATH || '/'
|
|
|
|
|
+ const entryFile = ENV.VITE_ENTRY || 'index.html'
|
|
|
|
|
+
|
|
|
const proxyConf: Record<string, string | ProxyOptions> = {}
|
|
const proxyConf: Record<string, string | ProxyOptions> = {}
|
|
|
proxyConf['/admin/api'] = {
|
|
proxyConf['/admin/api'] = {
|
|
|
target: 'http://127.0.0.1:8080',
|
|
target: 'http://127.0.0.1:8080',
|
|
@@ -46,46 +50,41 @@ export default defineConfig((conf: any) => {
|
|
|
proxyConf['/doc'] = {
|
|
proxyConf['/doc'] = {
|
|
|
target: 'http://127.0.0.1:8080',
|
|
target: 'http://127.0.0.1:8080',
|
|
|
changeOrigin: true,
|
|
changeOrigin: true,
|
|
|
- rewrite: (path: string) => path.replace(ENV.VITE_BASE_PATH, '/'),
|
|
|
|
|
|
|
+ rewrite: (path: string) => path.replace(basePath, '/'),
|
|
|
}
|
|
}
|
|
|
proxyConf['/schema'] = {
|
|
proxyConf['/schema'] = {
|
|
|
target: 'http://127.0.0.1:8080',
|
|
target: 'http://127.0.0.1:8080',
|
|
|
changeOrigin: true,
|
|
changeOrigin: true,
|
|
|
- rewrite: (path: string) => path.replace(ENV.VITE_BASE_PATH, '/'),
|
|
|
|
|
|
|
+ rewrite: (path: string) => path.replace(basePath, '/'),
|
|
|
}
|
|
}
|
|
|
proxyConf['/static'] = {
|
|
proxyConf['/static'] = {
|
|
|
target: 'http://127.0.0.1:8080',
|
|
target: 'http://127.0.0.1:8080',
|
|
|
changeOrigin: true,
|
|
changeOrigin: true,
|
|
|
- rewrite: (path: string) => path.replace(ENV.VITE_BASE_PATH, '/'),
|
|
|
|
|
|
|
+ rewrite: (path: string) => path.replace(basePath, '/'),
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 前端静态资源转发到本身
|
|
|
|
|
- proxyConf[`^${ENV.VITE_BASE_PATH}.+\/oss\/file\/.*$`] = {
|
|
|
|
|
- target: `http://127.0.0.1:8080`,
|
|
|
|
|
|
|
+ proxyConf[`^${basePath}.+\/oss\/file\/.*$`] = {
|
|
|
|
|
+ target: 'http://127.0.0.1:8080',
|
|
|
changeOrigin: true,
|
|
changeOrigin: true,
|
|
|
}
|
|
}
|
|
|
- // 前端静态资源转发到本身
|
|
|
|
|
- proxyConf[`^${ENV.VITE_BASE_PATH}oss\/file\/.*$`] = {
|
|
|
|
|
- target: `http://127.0.0.1:8080`,
|
|
|
|
|
|
|
+ proxyConf[`^${basePath}oss\/file\/.*$`] = {
|
|
|
|
|
+ target: 'http://127.0.0.1:8080',
|
|
|
changeOrigin: true,
|
|
changeOrigin: true,
|
|
|
}
|
|
}
|
|
|
- proxyConf[`^${ENV.VITE_BASE_PATH}oss\/get_url\/.*$`] = {
|
|
|
|
|
- target: `http://127.0.0.1:8080`,
|
|
|
|
|
|
|
+ proxyConf[`^${basePath}oss\/get_url\/.*$`] = {
|
|
|
|
|
+ target: 'http://127.0.0.1:8080',
|
|
|
changeOrigin: true,
|
|
changeOrigin: true,
|
|
|
}
|
|
}
|
|
|
- // 前端静态资源转发到本身
|
|
|
|
|
- proxyConf[ENV.VITE_BASE_PATH] = {
|
|
|
|
|
- target: `http://127.0.0.1:${ENV.VITE_APP_PORT}`,
|
|
|
|
|
|
|
+ proxyConf[basePath] = {
|
|
|
|
|
+ target: `http://127.0.0.1:${ENV.VITE_APP_PORT || 8080}`,
|
|
|
changeOrigin: true,
|
|
changeOrigin: true,
|
|
|
- rewrite: (path: string) => path.replace(ENV.VITE_BASE_PATH, '/'),
|
|
|
|
|
|
|
+ rewrite: (path: string) => path.replace(basePath, '/'),
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 代理 /chat 到 3001 端口
|
|
|
|
|
if (mode !== 'chat') {
|
|
if (mode !== 'chat') {
|
|
|
proxyConf['/chat'] = {
|
|
proxyConf['/chat'] = {
|
|
|
target: 'http://127.0.0.1:3001',
|
|
target: 'http://127.0.0.1:3001',
|
|
|
changeOrigin: true,
|
|
changeOrigin: true,
|
|
|
- // 避免代理后端的 API
|
|
|
|
|
bypass: (req: any) => {
|
|
bypass: (req: any) => {
|
|
|
if (req.url && req.url.startsWith('/chat/api')) {
|
|
if (req.url && req.url.startsWith('/chat/api')) {
|
|
|
return req.url
|
|
return req.url
|
|
@@ -103,21 +102,21 @@ export default defineConfig((conf: any) => {
|
|
|
vue(),
|
|
vue(),
|
|
|
vueJsx(),
|
|
vueJsx(),
|
|
|
DefineOptions(),
|
|
DefineOptions(),
|
|
|
- createHtmlPlugin({template: ENV.VITE_ENTRY}),
|
|
|
|
|
- renameHtmlPlugin(`dist${ENV.VITE_BASE_PATH}`, ENV.VITE_ENTRY),
|
|
|
|
|
|
|
+ createHtmlPlugin({template: entryFile}),
|
|
|
|
|
+ renameHtmlPlugin(`dist${basePath}`, entryFile),
|
|
|
],
|
|
],
|
|
|
server: {
|
|
server: {
|
|
|
cors: true,
|
|
cors: true,
|
|
|
host: '0.0.0.0',
|
|
host: '0.0.0.0',
|
|
|
- port: Number(ENV.VITE_APP_PORT),
|
|
|
|
|
|
|
+ port: Number(ENV.VITE_APP_PORT) || 8080,
|
|
|
strictPort: true,
|
|
strictPort: true,
|
|
|
proxy: proxyConf,
|
|
proxy: proxyConf,
|
|
|
},
|
|
},
|
|
|
build: {
|
|
build: {
|
|
|
- outDir: `dist${ENV.VITE_BASE_PATH}`,
|
|
|
|
|
|
|
+ outDir: `dist${basePath}`,
|
|
|
target: 'es2022',
|
|
target: 'es2022',
|
|
|
rollupOptions: {
|
|
rollupOptions: {
|
|
|
- input: ENV.VITE_ENTRY,
|
|
|
|
|
|
|
+ input: entryFile,
|
|
|
},
|
|
},
|
|
|
},
|
|
},
|
|
|
resolve: {
|
|
resolve: {
|