89 lines
2.8 KiB
JavaScript
89 lines
2.8 KiB
JavaScript
/* eslint-env node */
|
|
import path from 'node:path'
|
|
import { defineConfig, loadEnv } from 'vite'
|
|
import { fileURLToPath, URL } from 'node:url'
|
|
import tailwindcss from '@tailwindcss/vite'
|
|
import process from 'node:process'
|
|
import vue from '@vitejs/plugin-vue'
|
|
import vueJsx from '@vitejs/plugin-vue-jsx'
|
|
import vueDevTools from 'vite-plugin-vue-devtools'
|
|
import UnoCSS from 'unocss/vite'
|
|
// import electron from 'vite-plugin-electron/simple'
|
|
|
|
/**
|
|
* Vite 配置文件
|
|
* 支持 TypeScript 和 JavaScript
|
|
* @param {Object} param0 - 配置参数
|
|
* @param {string} param0.mode - 环境模式
|
|
* @returns {import('vite').UserConfig}
|
|
*/
|
|
export default defineConfig(({ mode }) => {
|
|
const env = loadEnv(mode, process.cwd(), '')
|
|
const DEV_TOKEN = env.VITE_DEV_TOKEN || ''
|
|
const TENANT_ID = env.VITE_TENANT_ID || '1'
|
|
const API_TARGET = env.VITE_PROXY_TARGET || 'http://localhost:9900'
|
|
|
|
return {
|
|
plugins: [
|
|
vue(),
|
|
vueJsx(),
|
|
UnoCSS(),
|
|
vueDevTools(),
|
|
tailwindcss()
|
|
// electron({
|
|
// main: {
|
|
// // Shortcut of `build.lib.entry`.
|
|
// entry: 'electron/main.ts',
|
|
// vite: {
|
|
// build: {
|
|
// rollupOptions: {
|
|
// external: ['better-sqlite3'],
|
|
// },
|
|
// },
|
|
// },
|
|
// },
|
|
// preload: {
|
|
// input: fileURLToPath(new URL('./electron/preload.ts', import.meta.url)),
|
|
// },
|
|
// // See 👉 https://github.com/electron-vite/vite-plugin-electron-renderer
|
|
// renderer:
|
|
// process.env.NODE_ENV === 'test'
|
|
// ? // https://github.com/electron-vite/vite-plugin-electron-renderer/issues/78#issuecomment-2053600808
|
|
// undefined
|
|
// : {},
|
|
// }),
|
|
],
|
|
resolve: {
|
|
alias: {
|
|
'@': fileURLToPath(new URL('./src', import.meta.url)),
|
|
'@gold': fileURLToPath(new URL('../../', import.meta.url))
|
|
},
|
|
},
|
|
server: {
|
|
proxy: {
|
|
// 代理 /webApi 开头的请求
|
|
'/webApi': {
|
|
target: API_TARGET,
|
|
changeOrigin: true,
|
|
rewrite: (path) => path.replace(/^\/webApi/, ''),
|
|
configure: (proxy, options) => {
|
|
proxy.on('proxyReq', (proxyReq, req) => {
|
|
// 从客户端请求头中获取 authorization
|
|
const authHeader = req.headers?.authorization
|
|
if (authHeader) {
|
|
proxyReq.setHeader('authorization', authHeader)
|
|
} else if (DEV_TOKEN) {
|
|
// 兜底:使用环境变量中的 token
|
|
proxyReq.setHeader('authorization', `Bearer ${DEV_TOKEN}`)
|
|
}
|
|
|
|
// 添加 RuoYi 租户 ID
|
|
proxyReq.setHeader('tenant-id', TENANT_ID)
|
|
})
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
})
|