Files

57 lines
2.1 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import react from "@vitejs/plugin-react";
import { VitePWA } from "vite-plugin-pwa";
import tailwindcss from "@tailwindcss/vite";
import { defineConfig } from "vite";
import { visualizer } from "rollup-plugin-visualizer";
export default defineConfig({
resolve: { alias: { "@": "/src" } },
plugins: [
react(),
tailwindcss(),
VitePWA({
registerType: "autoUpdate",
includeAssets: ["icon.svg"],
manifest: {
name: "BookLib 个人书库",
short_name: "BookLib",
description: "个人书库/漫画库在线阅读",
theme_color: "#fafaf9",
background_color: "#fafaf9",
display: "standalone",
icons: [{ src: "icon.svg", sizes: "any", type: "image/svg+xml", purpose: "any" }],
},
workbox: {
globPatterns: ["**/*.{js,css,html,svg,woff2}"],
navigateFallback: "/index.html",
runtimeCaching: [
{
// ?v={hash} 的封面/CBZ 页/原文件 = 不可变 → cache-first(spec §6.1/§6.3)
urlPattern: ({ url }) =>
url.pathname.startsWith("/api/books/") &&
url.searchParams.has("v") &&
(/\/cover$/.test(url.pathname) || /\/pages\/\d+$/.test(url.pathname) || /\/file$/.test(url.pathname)),
handler: "CacheFirst",
options: { cacheName: "booklib-immutable", expiration: { maxEntries: 3000, purgeOnQuotaError: true } },
},
{
// 普通 API 请求 network-first,离线可读最近一次列表/进度
urlPattern: ({ url }) => url.pathname.startsWith("/api/"),
handler: "NetworkFirst",
options: {
cacheName: "booklib-api",
networkTimeoutSeconds: 5,
expiration: { maxAgeSeconds: 3600, maxEntries: 200 },
},
},
],
},
}),
// npm run analyze:体积报告输出到 dist-stats/(git-ignore,不进发布产物)
...(process.env.ANALYZE
? [visualizer({ filename: "dist-stats/stats.html", gzipSize: true, brotliSize: true })]
: []),
],
server: { proxy: { "/api": process.env.VITE_PROXY_TARGET ?? "http://localhost:8080" } },
});