import { defineConfig } from "vite"; import react from "@vitejs/plugin-react"; import { VitePWA } from "vite-plugin-pwa"; // 开发端口固定 5180,接口走代理,避免本地跨域配置漂移 export default defineConfig({ plugins: [ react(), VitePWA({ // 有新版本时先不偷偷换掉:让用户自己点"刷新", // 免得开着看板的时候后台把 chunk 换了,点一下就是 404。 registerType: "prompt", injectRegister: null, // main.tsx 里自己注册,好在拿到 onNeedRefresh includeAssets: ["icons/apple-touch-icon.png"], manifest: { name: "达道Task", short_name: "达道Task", description: "任务、工单、知识库,一条道走到底。", lang: "zh-CN", dir: "ltr", start_url: "/", scope: "/", display: "standalone", orientation: "any", background_color: "#f4f5f9", theme_color: "#4f46e5", categories: ["productivity", "business"], icons: [ { src: "/icons/icon-192.png", sizes: "192x192", type: "image/png", purpose: "any" }, { src: "/icons/icon-512.png", sizes: "512x512", type: "image/png", purpose: "any" }, { src: "/icons/maskable-512.png", sizes: "512x512", type: "image/png", purpose: "maskable", }, ], }, workbox: { // 只预缓存构建产物与图标;Vditor 那 3.7MB 运行时按需缓存,别拖慢首次安装 globPatterns: ["**/*.{js,css,html,svg,png,ico,woff2}"], globIgnores: ["vditor/**", "**/*.map"], navigateFallback: "/index.html", navigateFallbackDenylist: [/^\/api\//], cleanupOutdatedCaches: true, runtimeCaching: [ { // 接口一律走网络:离线时宁可报错,也不要把别人的旧数据端出来 urlPattern: ({ url }) => url.pathname.startsWith("/api/"), handler: "NetworkOnly", }, { // 编辑器的 lute / 语言包 / 图标:用过一次就留着,离线也能写字 urlPattern: ({ url }) => url.pathname.startsWith("/vditor/"), handler: "CacheFirst", options: { cacheName: "vditor", expiration: { maxEntries: 40, maxAgeSeconds: 60 * 60 * 24 * 30 }, }, }, ], }, devOptions: { enabled: false }, }), ], server: { port: 5180, strictPort: true, proxy: { "/api": { target: process.env.VITE_API_TARGET ?? "http://127.0.0.1:8100", changeOrigin: true, }, }, }, build: { outDir: "dist", sourcemap: true, }, });