Skip to content

Commit 24a5390

Browse files
committedMar 26, 2025·
fix: support disable component auto imports
Fixes #337
1 parent 7456e18 commit 24a5390

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed
 

‎src/module.ts

+17-2
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ export interface ModuleOptions {
109109
/**
110110
* Extra component directories that should be used to resolve components.
111111
*
112-
* @default ['OgImage', 'OgImageTemplate']
112+
* @default ['OgImage', 'og-image', 'OgImageTemplate']
113113
*/
114114
componentDirs: string[]
115115
/**
@@ -146,6 +146,8 @@ function isProviderEnabledForEnv(provider: keyof CompatibilityFlags, nuxt: Nuxt,
146146
return (nuxt.options.dev && config.compatibility?.dev?.[provider] !== false) || (!nuxt.options.dev && (config.compatibility?.runtime?.[provider] !== false || config.compatibility?.prerender?.[provider] !== false))
147147
}
148148

149+
const defaultComponentDirs = ['OgImage', 'og-image', 'OgImageTemplate']
150+
149151
export default defineNuxtModule<ModuleOptions>({
150152
meta: {
151153
name: 'nuxt-og-image',
@@ -168,7 +170,7 @@ export default defineNuxtModule<ModuleOptions>({
168170
// default is to cache the image for 3 day (72 hours)
169171
cacheMaxAgeSeconds: 60 * 60 * 24 * 3,
170172
},
171-
componentDirs: ['OgImage', 'OgImageTemplate'],
173+
componentDirs: defaultComponentDirs,
172174
fonts: [],
173175
runtimeCacheStorage: true,
174176
debug: isDevelopment,
@@ -476,6 +478,19 @@ export default defineNuxtModule<ModuleOptions>({
476478
addPlugin({ mode: 'server', src: resolve(`${basePluginPath}/route-rule-og-image.server`) })
477479
addPlugin({ mode: 'server', src: resolve(`${basePluginPath}/og-image-canonical-urls.server`) })
478480

481+
for (const componentDir of config.componentDirs) {
482+
const path = resolve(nuxt.options.srcDir, 'components', componentDir)
483+
if (existsSync(path)) {
484+
addComponentsDir({
485+
path,
486+
island: true,
487+
})
488+
}
489+
else if (!defaultComponentDirs.includes(componentDir)) {
490+
logger.warn(`The configured component directory \`./${relative(nuxt.options.rootDir, path)}\` does not exist. Skipping.`)
491+
}
492+
}
493+
479494
// we're going to expose the og image components to the ssr build so we can fix prop usage
480495
const ogImageComponentCtx: { components: OgImageComponent[] } = { components: [] }
481496
nuxt.hook('components:extend', (components) => {

0 commit comments

Comments
 (0)
Please sign in to comment.