95 lines
2.7 KiB
TypeScript
95 lines
2.7 KiB
TypeScript
import { execFileSync } from 'node:child_process'
|
|
import { defineConfig, loadEnv } from 'vite'
|
|
import uni from '@dcloudio/vite-plugin-uni'
|
|
|
|
const normalizeEnvUrl = (value: string | undefined, fallback: string) => {
|
|
const normalized = value?.trim()
|
|
return normalized || fallback
|
|
}
|
|
|
|
const resolveGitCommit = () => {
|
|
try {
|
|
return execFileSync('git', ['rev-parse', '--short=12', 'HEAD'], {
|
|
encoding: 'utf8'
|
|
}).trim()
|
|
} catch {
|
|
return 'unknown'
|
|
}
|
|
}
|
|
|
|
const createBuildTimestamp = () => (
|
|
new Date().toISOString().replace(/[-:.TZ]/g, '').slice(0, 14)
|
|
)
|
|
|
|
export default defineConfig(({ mode }) => {
|
|
const env = loadEnv(mode, process.cwd(), '')
|
|
const gitCommit = normalizeEnvUrl(env.VITE_APP_GIT_COMMIT, resolveGitCommit())
|
|
const buildId = normalizeEnvUrl(
|
|
env.VITE_APP_BUILD_ID,
|
|
`${gitCommit}-${createBuildTimestamp()}`
|
|
)
|
|
const buildPlatform = process.env.UNI_PLATFORM === 'mp-weixin' ? 'mp-weixin' : 'h5'
|
|
const appApiProxyTarget = normalizeEnvUrl(env.DEV_PROXY_APP_API_TARGET, 'http://localhost:3001')
|
|
const engineProxyTarget = normalizeEnvUrl(env.DEV_PROXY_ENGINE_TARGET, appApiProxyTarget)
|
|
const sdkProxyTarget = normalizeEnvUrl(env.DEV_PROXY_SDK_TARGET, appApiProxyTarget)
|
|
const museumAssetsProxyTarget = normalizeEnvUrl(env.DEV_PROXY_MUSEUM_ASSETS_TARGET, 'http://localhost:9000')
|
|
const minioProxyTarget = normalizeEnvUrl(env.DEV_PROXY_MINIO_TARGET, appApiProxyTarget)
|
|
const audioProxyTarget = normalizeEnvUrl(env.DEV_PROXY_AUDIO_TARGET, 'http://localhost:19000')
|
|
|
|
return {
|
|
base: normalizeEnvUrl(env.VITE_APP_PUBLIC_BASE, '/'),
|
|
plugins: [uni()],
|
|
define: {
|
|
__APP_BUILD_ID__: JSON.stringify(buildId),
|
|
__APP_GIT_COMMIT__: JSON.stringify(gitCommit),
|
|
__APP_BUILD_PLATFORM__: JSON.stringify(buildPlatform)
|
|
},
|
|
server: {
|
|
host: '0.0.0.0',
|
|
proxy: {
|
|
'/app-api': {
|
|
target: appApiProxyTarget,
|
|
changeOrigin: true
|
|
},
|
|
'/engine': {
|
|
target: engineProxyTarget,
|
|
changeOrigin: true
|
|
},
|
|
'/sdk': {
|
|
target: sdkProxyTarget,
|
|
changeOrigin: true
|
|
},
|
|
'/museum-assets': {
|
|
target: museumAssetsProxyTarget,
|
|
changeOrigin: true
|
|
},
|
|
'/minio': {
|
|
target: minioProxyTarget,
|
|
changeOrigin: true
|
|
},
|
|
'/nhm/audio': {
|
|
target: audioProxyTarget,
|
|
changeOrigin: true
|
|
}
|
|
}
|
|
},
|
|
resolve: {
|
|
alias: {
|
|
'@': '/src'
|
|
}
|
|
},
|
|
optimizeDeps: {
|
|
include: ['three']
|
|
},
|
|
build: {
|
|
rollupOptions: {
|
|
output: {
|
|
manualChunks: {
|
|
three: ['three']
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
})
|