Skip to content

Commit

Permalink
fix: replace #nuxt-og-image with #og-image
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Oct 30, 2024
1 parent 80af9db commit e368342
Show file tree
Hide file tree
Showing 20 changed files with 61 additions and 61 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
"h3",
"nitropack",
"@vue/runtime-core",
"#nuxt-og-image/components",
"#og-image/components",
"sharp",
"unstorage",
"unstorage/drivers/fs",
Expand Down
8 changes: 4 additions & 4 deletions src/compatibility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ export async function applyNitroPresetCompatibility(nitroConfig: NitroConfig, op
const satoriEnabled = typeof options.compatibility?.satori !== 'undefined' ? !!options.compatibility.satori : !!compatibility.satori
const chromiumEnabled = typeof options.compatibility?.chromium !== 'undefined' ? !!options.compatibility.chromium : !!compatibility.chromium
// renderers
nitroConfig.alias!['#nuxt-og-image/renderers/satori'] = satoriEnabled ? resolve('./runtime/nitro/og-image/satori/renderer') : 'unenv/runtime/mock/empty'
nitroConfig.alias!['#nuxt-og-image/renderers/chromium'] = chromiumEnabled ? resolve('./runtime/nitro/og-image/chromium/renderer') : 'unenv/runtime/mock/empty'
nitroConfig.alias!['#og-image/renderers/satori'] = satoriEnabled ? resolve('./runtime/nitro/og-image/satori/renderer') : 'unenv/runtime/mock/empty'
nitroConfig.alias!['#og-image/renderers/chromium'] = chromiumEnabled ? resolve('./runtime/nitro/og-image/chromium/renderer') : 'unenv/runtime/mock/empty'

const resolvedCompatibility: Partial<Omit<RuntimeCompatibilitySchema, 'wasm'>> = {}
function applyBinding(key: keyof Omit<RuntimeCompatibilitySchema, 'wasm'>) {
Expand All @@ -159,7 +159,7 @@ export async function applyNitroPresetCompatibility(nitroConfig: NitroConfig, op
// @ts-expect-error untyped
resolvedCompatibility[key] = binding
return {
[`#nuxt-og-image/bindings/${key}`]: binding === false ? 'unenv/runtime/mock/empty' : resolve(`./runtime/nitro/og-image/bindings/${key}/${binding}`),
[`#og-image/bindings/${key}`]: binding === false ? 'unenv/runtime/mock/empty' : resolve(`./runtime/nitro/og-image/bindings/${key}/${binding}`),
}
}
nitroConfig.alias = defu(
Expand All @@ -178,7 +178,7 @@ export async function applyNitroPresetCompatibility(nitroConfig: NitroConfig, op
nitroConfig.rollupConfig = nitroConfig.rollupConfig || {}
nitroConfig.wasm = defu(compatibility.wasm, nitroConfig.wasm)

nitroConfig.virtual!['#nuxt-og-image/compatibility'] = () => `export default ${JSON.stringify(resolvedCompatibility)}`
nitroConfig.virtual!['#og-image/compatibility'] = () => `export default ${JSON.stringify(resolvedCompatibility)}`
addTemplate({
filename: 'nuxt-og-image/compatibility.mjs',
getContents() {
Expand Down
14 changes: 7 additions & 7 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@ export default defineNuxtModule<ModuleOptions>({
logger.warn('Nuxt OG Image is enabled but SSR is disabled.\n\nYou should enable SSR (`ssr: true`) or disable the module (`ogImage: { enabled: false }`).')
return
}
nuxt.options.alias['#nuxt-og-image'] = resolve('./runtime')
nuxt.options.alias['#nuxt-og-image-cache'] = resolve('./runtime/nitro/og-image/cache/lru')
nuxt.options.alias['#og-image'] = resolve('./runtime')
nuxt.options.alias['#og-image-cache'] = resolve('./runtime/nitro/og-image/cache/lru')

const preset = resolveNitroPreset(nuxt.options.nitro)
const targetCompatibility = getPresetNitroPresetCompatibility(preset)
Expand All @@ -215,7 +215,7 @@ export default defineNuxtModule<ModuleOptions>({

if (!nuxt.options.dev) {
addBuildPlugin(TreeShakeComposablesPlugin, { server: true, client: true, build: true })
nuxt.options.alias['#nuxt-og-image-cache'] = resolve('./runtime/nitro/og-image/cache/mock')
nuxt.options.alias['#og-image-cache'] = resolve('./runtime/nitro/og-image/cache/mock')
}
}

Expand Down Expand Up @@ -491,7 +491,7 @@ export default defineNuxtModule<ModuleOptions>({
options: { mode: 'server' },
})
nuxt.options.nitro.virtual = nuxt.options.nitro.virtual || {}
nuxt.options.nitro.virtual['#nuxt-og-image-virtual/component-names.mjs'] = () => {
nuxt.options.nitro.virtual['#og-image-virtual/component-names.mjs'] = () => {
return `export const componentNames = ${JSON.stringify(ogImageComponentCtx.components)}`
}

Expand All @@ -505,7 +505,7 @@ export default defineNuxtModule<ModuleOptions>({
nuxt.hook('unocss:config', (_unoCssConfig) => {
unoCssConfig = { ..._unoCssConfig.theme }
})
nuxt.options.nitro.virtual['#nuxt-og-image-virtual/unocss-config.mjs'] = () => {
nuxt.options.nitro.virtual['#og-image-virtual/unocss-config.mjs'] = () => {
return `export const theme = ${JSON.stringify(unoCssConfig)}`
}

Expand Down Expand Up @@ -537,12 +537,12 @@ declare module 'nitropack' {
}
}
declare module '#nuxt-og-image/components' {
declare module '#og-image/components' {
export interface OgImageComponents {
${componentImports}
}
}
declare module '#nuxt-og-image/unocss-config' {
declare module '#og-image/unocss-config' {
export type theme = any
}
`
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/nitro/og-image/chromium/renderer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Browser } from 'playwright-core'
import type { Renderer } from '../../../types'
import { createBrowser } from '#nuxt-og-image/bindings/chromium'
import { createBrowser } from '#og-image/bindings/chromium'
import { createError } from 'h3'
import { createScreenshot } from './screenshot'

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/nitro/og-image/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
import type ChromiumRenderer from './chromium/renderer'
import type SatoriRenderer from './satori/renderer'
import { useNitroApp } from '#imports'
import { htmlPayloadCache, prerenderOptionsCache } from '#nuxt-og-image-cache'
import { htmlPayloadCache, prerenderOptionsCache } from '#og-image-cache'
import { defu } from 'defu'
import { parse } from 'devalue'
import { createError, getQuery } from 'h3'
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/nitro/og-image/instances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ const satoriRendererInstance: { instance?: typeof SatoriRenderer } = { instance:
const chromiumRendererInstance: { instance?: typeof ChromiumRenderer } = { instance: undefined }

export async function useSatoriRenderer() {
satoriRendererInstance.instance = satoriRendererInstance.instance || await import('#nuxt-og-image/renderers/satori').then(m => m.default)
satoriRendererInstance.instance = satoriRendererInstance.instance || await import('#og-image/renderers/satori').then(m => m.default)
return satoriRendererInstance.instance!
}

export async function useChromiumRenderer() {
chromiumRendererInstance.instance = chromiumRendererInstance.instance || await import('#nuxt-og-image/renderers/chromium').then(m => m.default)
chromiumRendererInstance.instance = chromiumRendererInstance.instance || await import('#og-image/renderers/chromium').then(m => m.default)
return chromiumRendererInstance.instance!
}
8 changes: 4 additions & 4 deletions src/runtime/nitro/og-image/satori/instances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,24 @@ const resvgInstance: { instance?: { initWasmPromise: Promise<void>, Resvg: typeo
const satoriInstance: { instance?: { initWasmPromise: Promise<void>, satori: typeof _satori } } = { instance: undefined }

export async function useResvg() {
resvgInstance.instance = resvgInstance.instance || await import('#nuxt-og-image/bindings/resvg').then(m => m.default)
resvgInstance.instance = resvgInstance.instance || await import('#og-image/bindings/resvg').then(m => m.default)
await resvgInstance.instance!.initWasmPromise
return resvgInstance.instance!.Resvg
}

export async function useSatori() {
satoriInstance.instance = satoriInstance.instance || await import('#nuxt-og-image/bindings/satori').then(m => m.default)
satoriInstance.instance = satoriInstance.instance || await import('#og-image/bindings/satori').then(m => m.default)
await satoriInstance.instance!.initWasmPromise
return satoriInstance.instance!.satori
}

export async function useSharp() {
sharpInstance.instance = sharpInstance.instance || await import('#nuxt-og-image/bindings/sharp').then(m => m.default)
sharpInstance.instance = sharpInstance.instance || await import('#og-image/bindings/sharp').then(m => m.default)
return sharpInstance.instance!
}

export async function useCssInline() {
cssInlineInstance.instance = cssInlineInstance.instance || await import('#nuxt-og-image/bindings/css-inline').then(m => m.default)
cssInlineInstance.instance = cssInlineInstance.instance || await import('#og-image/bindings/css-inline').then(m => m.default)
await cssInlineInstance.instance!.initWasmPromise
return cssInlineInstance.instance!.cssInline
}
2 changes: 1 addition & 1 deletion src/runtime/nitro/og-image/satori/plugins/unocss.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { VNode } from '../../../../types'
import { theme } from '#nuxt-og-image/virtual/unocss-config.mjs'
import { theme } from '#og-image-virtual/unocss-config.mjs'
import { createGenerator } from '@unocss/core'
import presetWind from '@unocss/preset-wind'
import { defineSatoriTransformer } from '../utils'
Expand Down
31 changes: 16 additions & 15 deletions src/runtime/nitro/og-image/satori/renderer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { SatoriOptions } from 'satori'
import type { OgImageRenderEventContext, Renderer, ResolvedFontConfig } from '../../../types'
import { theme } from '#nuxt-og-image/virtual/unocss-config.mjs'
import { fontCache } from '#nuxt-og-image-cache'
import { fontCache } from '#og-image-cache'
import { theme } from '#og-image-virtual/unocss-config.mjs'
import { defu } from 'defu'
import { normaliseFontInput, useOgImageRuntimeConfig } from '../../../shared'
import { loadFont } from './font'
Expand All @@ -15,20 +15,21 @@ async function resolveFonts(event: OgImageRenderEventContext) {
const normalisedFonts = normaliseFontInput([...event.options.fonts || [], ...fonts])
const localFontPromises: Promise<ResolvedFontConfig>[] = []
const preloadedFonts: ResolvedFontConfig[] = []
for (const font of normalisedFonts) {
if (await fontCache.hasItem(font.cacheKey)) {
font.data = await fontCache.getItemRaw(font.cacheKey)
preloadedFonts.push(font)
}
else {
if (!fontPromises[font.cacheKey]) {
fontPromises[font.cacheKey] = loadFont(event, font).then(async (_font) => {
if (_font?.data)
await fontCache.setItemRaw(_font.cacheKey, _font.data)
return _font
})
if (fontCache) {
for (const font of normalisedFonts) {
if (await fontCache.hasItem(font.cacheKey)) {
font.data = await fontCache.getItemRaw(font.cacheKey)
preloadedFonts.push(font)
} else {
if (!fontPromises[font.cacheKey]) {
fontPromises[font.cacheKey] = loadFont(event, font).then(async (_font) => {
if (_font?.data)
await fontCache?.setItemRaw(_font.cacheKey, _font.data)
return _font
})
}
localFontPromises.push(fontPromises[font.cacheKey])
}
localFontPromises.push(fontPromises[font.cacheKey])
}
}
const awaitedFonts = await Promise.all(localFontPromises)
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/nitro/og-image/satori/transforms/emojis.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { NuxtIslandResponse } from 'nuxt/dist/core/runtime/nitro/renderer'
import type { OgImageRenderEventContext } from '../../../../types'
import { emojiCache } from '#nuxt-og-image-cache'
import { emojiCache } from '#og-image-cache'

const charMap = {
'2049': 'exclamation-question-mark',
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/nitro/og-image/templates/html.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { FontConfig, OgImageRenderEventContext } from '../../../types'
import { theme } from '#nuxt-og-image/virtual/unocss-config.mjs'
import { theme } from '#og-image-virtual/unocss-config.mjs'
import { renderSSRHead } from '@unhead/ssr'
import { createHeadCore } from '@unhead/vue'
import { createError } from 'h3'
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/nitro/plugins/prerender.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defineNitroPlugin } from '#imports'
import { prerenderOptionsCache } from '#nuxt-og-image-cache'
import { prerenderOptionsCache } from '#og-image-cache'
import { parseURL } from 'ufo'
import { isInternalRoute } from '../../pure'
import { extractAndNormaliseOgImageOptions, resolvePathCacheKey } from '../og-image/context'
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/nitro/routes/debug.json.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @ts-expect-error virtual module
import compatibility from '#nuxt-og-image/compatibility'
import compatibility from '#og-image/compatibility'
// @ts-expect-error untyped
import { componentNames } from '#nuxt-og-image-virtual/component-names.mjs'
import { componentNames } from '#og-image-virtual/component-names.mjs'

import { defineEventHandler, setHeader } from 'h3'

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/nitro/util/options.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { DefineOgImageInput, OgImageOptions, OgImagePrebuilt } from '../../types'
// @ts-expect-error untyped
import { componentNames } from '#nuxt-og-image-virtual/component-names.mjs'
import { componentNames } from '#og-image-virtual/component-names.mjs'

export function normaliseOptions(_options: DefineOgImageInput): OgImageOptions | OgImagePrebuilt {
const options = { ..._options } as OgImageOptions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/
import { useSiteConfig } from '#imports'
import { useOgImageRuntimeConfig } from '#nuxt-og-image/shared'
import { useOgImageRuntimeConfig } from '#og-image/shared'
import { computed, defineComponent, h, resolveComponent } from 'vue'
// convert to typescript props
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/nuxt/composables/defineOgImageComponent.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { OgImageComponents } from '#nuxt-og-image/components'
import type { OgImageComponents } from '#og-image/components'
import type { ExtractComponentProps, OgImageOptions } from '../../types'
import { defineOgImage } from './defineOgImage'

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/nuxt/composables/mock.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { OgImageComponents } from '#nuxt-og-image/components'
import type { OgImageComponents } from '#og-image/components'
import type { DefineOgImageInput, ExtractComponentProps, OgImageOptions, OgImagePageScreenshotOptions } from '../../types'

export function defineOgImage(_options: DefineOgImageInput = {}) {}
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { OgImageComponents } from '#nuxt-og-image/components'
import type { OgImageComponents } from '#og-image/components'
import type { ResvgRenderOptions } from '@resvg/resvg-js'
import type { AllowedComponentProps, Component, ComponentCustomProps, VNodeProps } from '@vue/runtime-core'
import type { H3Error, H3Event } from 'h3'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/
import { useSiteConfig } from '#imports'
import { useOgImageRuntimeConfig } from '#nuxt-og-image/shared'
import { useOgImageRuntimeConfig } from '#og-image/shared'
import { computed, defineComponent, h, resolveComponent } from 'vue'
// convert to typescript props
Expand Down
27 changes: 13 additions & 14 deletions virtual.d.ts
Original file line number Diff line number Diff line change
@@ -1,65 +1,64 @@
declare module '#nuxt-og-image/components' {
declare module '#og-image/components' {
import type { Component } from 'vue'

const components: Record<string, Component>
export default components
}
declare module '#nuxt-og-image/renderers/satori' {
declare module '#og-image/renderers/satori' {
import type Renderer from './src/runtime/types'

const renderer: Renderer | { __unenv__: true } | undefined
export default renderer
}
declare module '#nuxt-og-image/renderers/chromium' {
declare module '#og-image/renderers/chromium' {
import type Renderer from './src/runtime/types'

const renderer: Renderer | { __unenv__: true } | undefined
export default renderer
}

declare module '#nuxt-og-image/bindings/satori' {
declare module '#og-image/bindings/satori' {
const satori: typeof import('satori').satori
export default satori
}

declare module '#nuxt-og-image/bindings/resvg' {
declare module '#og-image/bindings/resvg' {
interface WasmResvg {
initWasmPromise: Promise<void>
Resvg: import('resvg').Resvg
}
const instance: WasmResvg
export default instance
}
declare module '#nuxt-og-image/bindings/chromium' {
declare module '#og-image/bindings/chromium' {
export const createBrowser: () => Promise<Browser | void>
}

declare module '#nuxt-og-image/bindings/css-inline' {
declare module '#og-image/bindings/css-inline' {
import type _CssInline from 'css-inline'

const cssInline: _CssInline
export default cssInline
}

declare module '#nuxt-og-image/bindings/sharp' {
declare module '#og-image/bindings/sharp' {
import type _sharp from 'sharp'

const sharp: _sharp
export default sharp
}

declare module '#nuxt-og-image-virtual/component-names.mjs' {
declare module '#og-image-virtual/component-names.mjs' {
const componentNames: string[]
export default componentNames
}

declare module '#nuxt-og-image-virtual/unocss-config.mjs' {
const theme: Record<string, any>
export default theme
declare module '#og-image-virtual/unocss-config.mjs' {
export const theme: Record<string, any>
}

declare module '#nuxt-og-image-cache' {
import type { OgImageOptions } from '#nuxt-og-image/types'
declare module '#og-image-cache' {
import type { OgImageOptions } from '#og-image/types'
import type { Storage } from 'unstorage'

export const htmlPayloadCache: Storage<{ expiresAt: number, value: OgImageOptions }>
Expand Down

0 comments on commit e368342

Please sign in to comment.