elk/modules/build-info.ts

34 lines
786 B
TypeScript
Raw Normal View History

2022-12-26 19:33:19 +00:00
import { addVitePlugin, defineNuxtModule } from '@nuxt/kit'
2023-01-04 13:26:30 +00:00
import { getEnv, version } from '../config/env'
2022-12-26 19:33:19 +00:00
import type { BuildInfo } from '~/types'
export default defineNuxtModule({
meta: {
name: 'elk:build-info',
},
2023-01-04 13:26:30 +00:00
async setup(_options, nuxt) {
const { env, commit, branch } = await getEnv()
nuxt.options.runtimeConfig.public.env = env
2022-12-26 19:33:19 +00:00
const buildInfo: BuildInfo = {
version,
time: +Date.now(),
2023-01-04 13:26:30 +00:00
commit,
branch,
env,
2022-12-26 19:33:19 +00:00
}
2023-01-04 13:26:30 +00:00
2022-12-26 19:33:19 +00:00
addVitePlugin({
name: 'elk:build-info',
resolveId(id) {
if (id === 'virtual:build-info')
return id
},
load(id) {
if (id === 'virtual:build-info')
2023-01-04 13:26:30 +00:00
return `export const buildInfo = ${JSON.stringify(buildInfo, null, 2)}`
2022-12-26 19:33:19 +00:00
},
})
},
})