feat(config): add development environment constant and enhance font loading logic

- Introduced a global constant `__DEV__` to indicate the development environment in the Vite configuration.
- Updated the `OgService` to conditionally include additional font paths when in development mode, improving font loading flexibility.
- Refactored font loading logic to check for file existence before attempting to read, enhancing reliability.

Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
Innei
2025-11-14 20:59:14 +08:00
parent ef19962eb2
commit 4e1b27625b
3 changed files with 30 additions and 6 deletions

View File

@@ -1 +1,5 @@
import 'vite/client'
declare global {
const __DEV__: boolean
}

View File

@@ -1,4 +1,4 @@
import { readFile } from 'node:fs/promises'
import { readFile, stat } from 'node:fs/promises'
import { resolve } from 'node:path'
import type { PhotoManifestItem } from '@afilmory/builder'
@@ -21,6 +21,19 @@ interface ThumbnailCandidateResult {
contentType: string
}
const GeistFontCandidates = [
resolve(process.cwd(), `./${geistFontUrl}`),
resolve(process.cwd(), `./dist/${geistFontUrl}`),
]
if (__DEV__) {
GeistFontCandidates.push(
resolve(process.cwd(), `./be/apps/core/src/modules/content/og/assets/Geist-Medium.ttf`),
resolve(process.cwd(), `./apps/core/src/modules/content/og/assets/Geist-Medium.ttf`),
resolve(process.cwd(), `./core/src/modules/content/og/assets/Geist-Medium.ttf`),
)
}
@injectable()
export class OgService implements OnModuleDestroy {
private fontConfig: SatoriOptions['fonts'] | null = null
@@ -76,19 +89,23 @@ export class OgService implements OnModuleDestroy {
return new Response(body, { status: 200, headers })
}
// private cjkFontPromise: Promise<NonSharedBuffer> | null = null
private geistFontPromise: Promise<NonSharedBuffer> | null = null
loadFonts() {
async loadFonts() {
if (!this.geistFontPromise) {
// this.cjkFontPromise = readFile(resolve(process.cwd(), `./${cjkFontUrl}`))
this.geistFontPromise = readFile(resolve(process.cwd(), `./${geistFontUrl}`))
for (const candidate of GeistFontCandidates) {
const stats = await stat(candidate)
if (stats.isFile()) {
this.geistFontPromise = readFile(candidate)
break
}
}
}
this.resetFontCleanupTimer()
}
private async getFontConfig(): Promise<SatoriOptions['fonts']> {
this.loadFonts()
await this.loadFonts()
return [
{

View File

@@ -135,6 +135,9 @@ export default defineConfig({
'@afilmory/be-utils/': `${resolve(__dirname, '../../packages/utils/src')}/`,
},
},
define: {
__DEV__: JSON.stringify(process.env.NODE_ENV === 'development'),
},
ssr: {
noExternal: true,
external,