86 lines
2.4 KiB
TypeScript
86 lines
2.4 KiB
TypeScript
import { defineConfig } from 'vite';
|
|
import react from '@vitejs/plugin-react';
|
|
import path from 'path';
|
|
import pkgs from './package.json';
|
|
import tailwindcss from '@tailwindcss/vite';
|
|
import { tanstackRouter } from '@tanstack/router-plugin/vite'
|
|
import dotenv from 'dotenv';
|
|
import { VitePWA } from 'vite-plugin-pwa';
|
|
|
|
const env = dotenv.config().parsed || {};
|
|
const isDev = env.NODE_ENV === 'development' || process.env.NODE_ENV === 'development';
|
|
const basename = isDev ? '/' : pkgs?.basename || '/';
|
|
|
|
let target = env.VITE_API_URL || process.env.API_URL || 'http://localhost:51515';
|
|
const apiProxy = { target: target, changeOrigin: true, ws: true, rewriteWsOrigin: true, secure: false, cookieDomainRewrite: 'localhost' };
|
|
let proxy = {
|
|
'/root/': apiProxy,
|
|
'/api': apiProxy,
|
|
'/client': apiProxy,
|
|
};
|
|
/**
|
|
* @see https://vitejs.dev/config/
|
|
*/
|
|
export default defineConfig({
|
|
plugins: [
|
|
// Please make sure that '@tanstack/router-plugin' is passed before '@vitejs/plugin-react'
|
|
tanstackRouter({
|
|
target: 'react',
|
|
autoCodeSplitting: true,
|
|
}),
|
|
react(),
|
|
tailwindcss(),
|
|
VitePWA({
|
|
injectRegister: 'auto',
|
|
registerType: 'autoUpdate',
|
|
// Workbox 缓存策略配置
|
|
workbox: {
|
|
// API 请求使用网络优先策略,确保获取最新数据
|
|
runtimeCaching: [
|
|
{
|
|
urlPattern: /^https?.*\/api\/.*/,
|
|
handler: 'NetworkFirst',
|
|
options: {
|
|
cacheName: 'api-cache',
|
|
expiration: {
|
|
maxEntries: 50,
|
|
maxAgeSeconds: 60 * 60 * 24, // 24小时
|
|
},
|
|
cacheableResponse: {
|
|
statuses: [0, 200],
|
|
},
|
|
},
|
|
},
|
|
// 静态资源使用缓存优先,但设置较短过期时间
|
|
{
|
|
urlPattern: /^https?.*\.(js|css|woff2?|png|jpg|jpeg|svg|gif|ico)/,
|
|
handler: 'StaleWhileRevalidate',
|
|
options: {
|
|
cacheName: 'static-resources',
|
|
expiration: {
|
|
maxEntries: 100,
|
|
maxAgeSeconds: 60 * 60 * 24 * 7, // 7天
|
|
},
|
|
},
|
|
},
|
|
],
|
|
},
|
|
}),
|
|
],
|
|
resolve: {
|
|
alias: {
|
|
'@': path.resolve(__dirname, './src'),
|
|
},
|
|
},
|
|
base: basename,
|
|
define: {
|
|
BASE_NAME: JSON.stringify(basename),
|
|
},
|
|
server: {
|
|
port: 7008,
|
|
host: '0.0.0.0',
|
|
allowedHosts: true,
|
|
proxy,
|
|
},
|
|
});
|