Files
Academic-Affairs-System/web/vite.config.ts
T

134 lines
4.4 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', 'push-handler.js'],
manifest: {
name: branding.productName,
short_name: branding.shortName,
description: branding.description,
theme_color: '#152550',
background_color: '#152550',
display: 'standalone',
start_url: '/',
scope: '/',
icons: [
{
src: 'favicon.svg',
sizes: 'any',
type: 'image/svg+xml',
purpose: 'any maskable',
},
],
},
workbox: {
cleanupOutdatedCaches: true,
importScripts: ['/push-handler.js'],
// `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',
},
],
},
}),
{
name: 'runtime-branding-manifest',
enforce: 'post',
transformIndexHtml: {
order: 'post',
handler(html: string) {
if (isCapacitor) return html
const apiBase = (env.VITE_API_BASE_URL || '/api').replace(/\/$/, '')
// A manifest hosted on another origin cannot safely use this site's start URL and scope.
if (!apiBase.startsWith('/') || apiBase.startsWith('//')) return html
return html.replace(/(<link\s+rel="manifest"\s+href=")[^"]+("\s*\/?>)/, `$1${escapeHtml(apiBase)}/business-settings/manifest.webmanifest$2`)
},
},
},
],
base: isCapacitor ? './' : '/',
build: {
outDir: isCapacitor ? 'dist' : '../src/Jiaowu.Api/wwwroot',
emptyOutDir: true,
// CKEditor is already isolated behind the compose-tab async component.
// Keep the warning useful for unexpected chunks without over-splitting
// the editor into many additional requests.
chunkSizeWarningLimit: 1_100,
},
server: {
port: 5173,
proxy: {
'/api': {
target: 'http://localhost:5255',
changeOrigin: true,
},
'/health': {
target: 'http://localhost:5255',
changeOrigin: true,
},
},
},
}
})