Files
Academic-Affairs-System/web/vite.config.ts
T
2026-09-29 10:42:28 +08:00

116 lines
3.5 KiB
TypeScript

import { defineConfig, loadEnv } from 'vite'
import { readFileSync } from 'node:fs'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import { VitePWA } from 'vite-plugin-pwa'
import { branding } from './branding.config.ts'
const versions = readFileSync(new URL('../versions.props', import.meta.url), 'utf8')
const frontendVersion = /<JiaowuFrontendVersion>([^<]+)<\/JiaowuFrontendVersion>/.exec(versions)?.[1]
if (!frontendVersion) {
throw new Error('versions.props 中缺少 JiaowuFrontendVersion。')
}
function escapeHtml(value: string) {
return value
.replaceAll('&', '&amp;')
.replaceAll('"', '&quot;')
.replaceAll('<', '&lt;')
.replaceAll('>', '&gt;')
}
// https://vite.dev/config/
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd(), '')
const isCapacitor = env.VITE_ROUTER_MODE === 'hash'
return {
define: {
__APP_VERSION__: JSON.stringify(frontendVersion),
__APP_BRANDING__: JSON.stringify(branding),
},
plugins: [
vue(),
{
name: 'app-branding-html',
transformIndexHtml(html: string) {
return html
.replaceAll('__APP_PRODUCT_NAME__', escapeHtml(branding.productName))
.replaceAll('__APP_DESCRIPTION__', escapeHtml(branding.description))
},
},
AutoImport({
resolvers: [ElementPlusResolver()],
dts: 'src/auto-imports.d.ts',
}),
Components({
resolvers: [ElementPlusResolver({ importStyle: 'css', directives: true })],
dts: 'src/components.d.ts',
}),
VitePWA({
disable: isCapacitor,
registerType: 'prompt',
// Registration is handled in src/services/pwa.ts so updates can be
// confirmed before the active session is reloaded.
injectRegister: false,
includeAssets: ['favicon.svg'],
manifest: {
name: branding.productName,
short_name: branding.shortName,
description: branding.description,
theme_color: '#152550',
background_color: '#152550',
display: 'standalone',
start_url: '/',
scope: '/',
lang: 'zh-CN',
icons: [
{
src: 'favicon.svg',
sizes: 'any',
type: 'image/svg+xml',
purpose: 'any maskable',
},
],
},
workbox: {
cleanupOutdatedCaches: true,
// `skipWaiting()` only activates the new worker. Claiming existing
// clients is what fires `controllerchange` in pwa.ts and reloads the
// open page immediately after the user confirms the update.
clientsClaim: true,
navigateFallback: '/index.html',
navigateFallbackDenylist: [/^\/api\//],
runtimeCaching: [
{
urlPattern: /\/api\/.*/,
handler: 'NetworkOnly',
},
],
},
}),
],
base: isCapacitor ? './' : '/',
build: {
outDir: isCapacitor ? 'dist' : '../src/Jiaowu.Api/wwwroot',
emptyOutDir: true,
},
server: {
port: 5173,
proxy: {
'/api': {
target: 'http://localhost:5255',
changeOrigin: true,
},
'/health': {
target: 'http://localhost:5255',
changeOrigin: true,
},
},
},
}
})