Files
Enterprise-Knowledge-Base/frontend/vite.config.ts
T
wizardchen 589d2a6e52 feat(frontend): inject commit ID into frontend build for version tracking
- Added a new build argument `COMMIT_ID_ARG` to pass the commit ID during the Docker build process.
- Updated the Dockerfile to set an environment variable `VITE_FRONTEND_COMMIT` with the commit ID.
- Enhanced the Vite configuration to resolve the frontend commit ID from the environment or Git.
- Modified the SystemInfo component to display the frontend commit ID in the UI.
- Implemented a version info preparation step in the GitHub Actions workflow to capture the commit ID.
2026-06-09 18:08:57 +08:00

84 lines
2.2 KiB
TypeScript

import { fileURLToPath, URL } from 'node:url'
import { resolve, dirname } from 'node:path'
import { existsSync } from 'node:fs'
import { execSync } from 'node:child_process'
import { createRequire } from 'node:module'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
const __dirname = dirname(fileURLToPath(import.meta.url))
const require = createRequire(import.meta.url)
const pkg = require('./package.json') as { version?: string }
const FRONTEND_VERSION = pkg.version ?? 'unknown'
function resolveFrontendCommit(): string {
const fromEnv = process.env.VITE_FRONTEND_COMMIT || process.env.GITHUB_SHA
if (fromEnv) {
return fromEnv.slice(0, 7)
}
try {
return execSync('git rev-parse --short HEAD', { stdio: ['ignore', 'pipe', 'ignore'] })
.toString()
.trim()
} catch {
return 'unknown'
}
}
const FRONTEND_COMMIT = resolveFrontendCommit()
const DEV_PROXY_TARGET =
process.env.VITE_DEV_PROXY_TARGET ||
process.env.FRONTEND_BACKEND_URL ||
'http://localhost:8080'
function resolveVueOfficePptxEntry(): string {
try {
const pkgDir = dirname(require.resolve('@vue-office/pptx/package.json'))
const candidates = [
resolve(pkgDir, 'lib/v3/index.js'),
resolve(pkgDir, 'lib/index.js'),
resolve(pkgDir, 'lib/v3/vue-office-pptx.mjs'),
]
const matched = candidates.find((candidate) => existsSync(candidate))
return matched ?? '@vue-office/pptx'
} catch {
return '@vue-office/pptx'
}
}
export default defineConfig({
define: {
__FRONTEND_VERSION__: JSON.stringify(FRONTEND_VERSION),
__FRONTEND_COMMIT__: JSON.stringify(FRONTEND_COMMIT),
},
plugins: [
vue(),
vueJsx(),
],
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url)),
'@vue-office/pptx': resolveVueOfficePptxEntry(),
},
},
server: {
port: 5173,
host: true,
// 代理配置,用于开发环境
proxy: {
'/api': {
target: DEV_PROXY_TARGET,
changeOrigin: true,
secure: false,
},
'/files': {
target: DEV_PROXY_TARGET,
changeOrigin: true,
secure: false,
}
}
}
})