mirror of
https://github.com/Afilmory/afilmory
synced 2026-04-24 23:05:05 +00:00
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:
4
be/apps/core/src/global.d.ts
vendored
4
be/apps/core/src/global.d.ts
vendored
@@ -1 +1,5 @@
|
||||
import 'vite/client'
|
||||
|
||||
declare global {
|
||||
const __DEV__: boolean
|
||||
}
|
||||
|
||||
@@ -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 [
|
||||
{
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user