Skip to content

Commit b27543f

Browse files
committedMar 26, 2025·
fix: safer mock resolves
Maybe fixes #345
1 parent 24b718a commit b27543f

File tree

8 files changed

+27
-26
lines changed

8 files changed

+27
-26
lines changed
 

‎src/build/build.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { dirname } from 'pathe'
99
import { applyNitroPresetCompatibility, getPresetNitroPresetCompatibility, resolveNitroPreset } from '../compatibility'
1010

1111
// we need all of the runtime dependencies when using build
12-
export async function setupBuildHandler(config: ModuleOptions, resolve: Resolver['resolve'], nuxt: Nuxt = useNuxt()) {
12+
export async function setupBuildHandler(config: ModuleOptions, resolve: Resolver, nuxt: Nuxt = useNuxt()) {
1313
nuxt.options.nitro.storage = nuxt.options.nitro.storage || {}
1414
if (typeof config.runtimeCacheStorage === 'object')
1515
nuxt.options.nitro.storage['og-image'] = config.runtimeCacheStorage
@@ -18,12 +18,12 @@ export async function setupBuildHandler(config: ModuleOptions, resolve: Resolver
1818
await applyNitroPresetCompatibility(nitroConfig, { compatibility: config.compatibility?.runtime, resolve })
1919
// patch implicit dependencies:
2020
// - playwright-core
21-
nitroConfig.alias!.electron = resolve('./runtime/mock/proxy-cjs')
22-
nitroConfig.alias!.bufferutil = resolve('./runtime/mock/proxy-cjs')
23-
nitroConfig.alias!['utf-8-validate'] = resolve('./runtime/mock/proxy-cjs')
21+
nitroConfig.alias!.electron = await resolve.resolvePath('./runtime/mock/proxy-cjs')
22+
nitroConfig.alias!.bufferutil = await resolve.resolvePath('./runtime/mock/proxy-cjs')
23+
nitroConfig.alias!['utf-8-validate'] = await resolve.resolvePath('./runtime/mock/proxy-cjs')
2424
// - image-size
25-
nitroConfig.alias!.queue = resolve('./runtime/mock/proxy-cjs')
26-
nitroConfig.alias!['chromium-bidi'] = resolve('./runtime/mock/proxy-cjs')
25+
nitroConfig.alias!.queue = await resolve.resolvePath('./runtime/mock/proxy-cjs')
26+
nitroConfig.alias!['chromium-bidi'] = await resolve.resolvePath('./runtime/mock/proxy-cjs')
2727
})
2828

2929
// HACK: we need to patch the compiled output to fix the wasm resolutions using esmImport

‎src/build/dev.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { useNuxt } from '@nuxt/kit'
55
import { applyNitroPresetCompatibility } from '../compatibility'
66

77
// we need all of the runtime dependencies when using build
8-
export function setupDevHandler(options: ModuleOptions, resolve: Resolver['resolve'], nuxt: Nuxt = useNuxt()) {
8+
export function setupDevHandler(options: ModuleOptions, resolve: Resolver, nuxt: Nuxt = useNuxt()) {
99
nuxt.hooks.hook('nitro:config', async (nitroConfig) => {
1010
await applyNitroPresetCompatibility(nitroConfig, { compatibility: options.compatibility?.dev, resolve })
1111
})

‎src/build/generate.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { applyNitroPresetCompatibility } from '../compatibility'
66

77
// we don't need any of the runtime dependencies when we use nuxt generate
88
// same as dev but we inject the aliases into the prerender config and noop the regular nitro
9-
export function setupGenerateHandler(options: ModuleOptions, resolve: Resolver['resolve'], nuxt: Nuxt = useNuxt()) {
9+
export function setupGenerateHandler(options: ModuleOptions, resolve: Resolver, nuxt: Nuxt = useNuxt()) {
1010
nuxt.hooks.hook('nitro:config', async (nitroConfig) => {
1111
// bindings
1212
await applyNitroPresetCompatibility(nitroConfig, {

‎src/build/prerender.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { applyNitroPresetCompatibility } from '../compatibility'
66

77
// prerender will always be called when using nuxi generate and sometimes be used when using nuxi build
88

9-
export function setupPrerenderHandler(options: ModuleOptions, resolve: Resolver['resolve'], nuxt: Nuxt = useNuxt()) {
9+
export function setupPrerenderHandler(options: ModuleOptions, resolve: Resolver, nuxt: Nuxt = useNuxt()) {
1010
nuxt.hooks.hook('nitro:init', async (nitro) => {
1111
nitro.hooks.hook('prerender:config', async (nitroConfig) => {
1212
// bindings

‎src/compatibility.ts

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { Resolver } from '@nuxt/kit'
12
import type { Nuxt } from '@nuxt/schema'
23
import type { NitroConfig } from 'nitropack'
34
import type { CompatibilityFlags, RuntimeCompatibilitySchema } from './runtime/types'
@@ -129,7 +130,7 @@ export function getPresetNitroPresetCompatibility(target: string) {
129130
return compatibility
130131
}
131132

132-
export async function applyNitroPresetCompatibility(nitroConfig: NitroConfig, options: { compatibility?: CompatibilityFlags, resolve: (s: string) => string, overrides?: RuntimeCompatibilitySchema }): Promise<Partial<Omit<RuntimeCompatibilitySchema, 'wasm'>>> {
133+
export async function applyNitroPresetCompatibility(nitroConfig: NitroConfig, options: { compatibility?: CompatibilityFlags, resolve: Resolver, overrides?: RuntimeCompatibilitySchema }): Promise<Partial<Omit<RuntimeCompatibilitySchema, 'wasm'>>> {
133134
const target = resolveNitroPreset(nitroConfig)
134135
const compatibility: RuntimeCompatibilitySchema = getPresetNitroPresetCompatibility(target)
135136

@@ -141,11 +142,12 @@ export async function applyNitroPresetCompatibility(nitroConfig: NitroConfig, op
141142
const satoriEnabled = typeof options.compatibility?.satori !== 'undefined' ? !!options.compatibility.satori : !!compatibility.satori
142143
const chromiumEnabled = typeof options.compatibility?.chromium !== 'undefined' ? !!options.compatibility.chromium : !!compatibility.chromium
143144
// renderers
144-
nitroConfig.alias!['#og-image/renderers/satori'] = satoriEnabled ? resolve('./runtime/server/og-image/satori/renderer') : resolve('./runtime/mock/empty')
145-
nitroConfig.alias!['#og-image/renderers/chromium'] = chromiumEnabled ? resolve('./runtime/server/og-image/chromium/renderer') : resolve('./runtime/mock/empty')
145+
const emptyMock = await resolve.resolvePath('./runtime/mock/empty')
146+
nitroConfig.alias!['#og-image/renderers/satori'] = satoriEnabled ? await resolve.resolvePath('./runtime/server/og-image/satori/renderer') : emptyMock
147+
nitroConfig.alias!['#og-image/renderers/chromium'] = chromiumEnabled ? await resolve.resolvePath('./runtime/server/og-image/chromium/renderer') : emptyMock
146148

147149
const resolvedCompatibility: Partial<Omit<RuntimeCompatibilitySchema, 'wasm'>> = {}
148-
function applyBinding(key: keyof Omit<RuntimeCompatibilitySchema, 'wasm'>) {
150+
async function applyBinding(key: keyof Omit<RuntimeCompatibilitySchema, 'wasm'>) {
149151
let binding = options.compatibility?.[key]
150152
if (typeof binding === 'undefined')
151153
binding = compatibility[key]
@@ -160,15 +162,15 @@ export async function applyNitroPresetCompatibility(nitroConfig: NitroConfig, op
160162
// @ts-expect-error untyped
161163
resolvedCompatibility[key] = binding
162164
return {
163-
[`#og-image/bindings/${key}`]: binding === false ? resolve('./runtime/mock/empty') : resolve(`./runtime/server/og-image/bindings/${key}/${binding}`),
165+
[`#og-image/bindings/${key}`]: binding === false ? emptyMock : await resolve.resolvePath(`./runtime/server/og-image/bindings/${key}/${binding}`),
164166
}
165167
}
166168
nitroConfig.alias = defu(
167-
applyBinding('chromium'),
168-
applyBinding('satori'),
169-
applyBinding('resvg'),
170-
applyBinding('sharp'),
171-
applyBinding('css-inline'),
169+
await applyBinding('chromium'),
170+
await applyBinding('satori'),
171+
await applyBinding('resvg'),
172+
await applyBinding('sharp'),
173+
await applyBinding('css-inline'),
172174
nitroConfig.alias || {},
173175
)
174176
// if we're using any wasm modules we need to enable the wasm runtime

‎src/module.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@ export default defineNuxtModule<ModuleOptions>({
169169
}
170170
},
171171
async setup(config, nuxt) {
172-
const { resolve } = createResolver(import.meta.url)
172+
const resolver = createResolver(import.meta.url)
173+
const { resolve } = resolver
173174
const { version } = await readPackageJSON(resolve('../package.json'))
174175
logger.level = (config.debug || nuxt.options.debug) ? 4 : 3
175176
if (config.enabled === false) {
@@ -611,19 +612,19 @@ declare module '#og-image/unocss-config' {
611612

612613
// Setup playground. Only available in development
613614
if (nuxt.options.dev) {
614-
setupDevHandler(config, resolve)
615+
setupDevHandler(config, resolver)
615616
setupDevToolsUI(config, resolve)
616617
}
617618
else if (isNuxtGenerate()) {
618-
setupGenerateHandler(config, resolve)
619+
setupGenerateHandler(config, resolver)
619620
}
620621
else if (nuxt.options.build) {
621-
await setupBuildHandler(config, resolve)
622+
await setupBuildHandler(config, resolver)
622623
}
623624
// no way to know if we'll prerender any routes
624625
if (nuxt.options.build)
625626
addServerPlugin(resolve('./runtime/server/plugins/prerender'))
626627
// always call this as we may have routes only discovered at build time
627-
setupPrerenderHandler(config, resolve)
628+
setupPrerenderHandler(config, resolver)
628629
},
629630
})

‎src/runtime/mock/empty.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// @ts-expect-error missing types
21
import empty from 'mocked-exports/empty'
32

43
export default empty

‎src/runtime/mock/proxy-cjs.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// @ts-expect-error missing types
21
import proxy from 'mocked-exports/proxy-cjs'
32

43
export default proxy

0 commit comments

Comments
 (0)
Please sign in to comment.