Skip to content

Commit 5266d22

Browse files
authored
fix: do not load <NuxtImg> stub if it is not installed (#261)
1 parent 9f25fa1 commit 5266d22

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

src/module.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import fs from 'node:fs'
2-
import { defineNuxtModule, extendViteConfig, addComponent, addComponentsDir, createResolver, addServerHandler, addTemplate, addImports, addServerImports, hasNuxtModule } from '@nuxt/kit'
2+
import { defineNuxtModule, extendViteConfig, addComponent, addComponentsDir, createResolver, addServerHandler, addTemplate, addImports, addServerImports } from '@nuxt/kit'
33
import { defu } from 'defu'
44
import { resolve } from 'pathe'
55
import type { BundledLanguage } from 'shiki'
@@ -168,11 +168,10 @@ export default defineNuxtModule<ModuleOptions>({
168168
})
169169
}
170170

171-
if (hasNuxtModule('@nuxt/image')) {
172-
nuxt.options.runtimeConfig.public.mdc = defu(nuxt.options.runtimeConfig.public.mdc, {
173-
useNuxtImage: true
174-
})
175-
}
171+
addTemplate({
172+
filename: 'mdc-image-component.mjs',
173+
getContents: ({ app }) => app.components.some(c => c.pascalName === 'NuxtImg' && !c.filePath.includes('nuxt/dist/app')) ? 'export { NuxtImg } from "#components"' : 'export default "img"'
174+
})
176175

177176
// Update Vite optimizeDeps
178177
extendViteConfig((config) => {
@@ -258,7 +257,6 @@ declare module '@nuxt/schema' {
258257
map: Record<string, string>
259258
}
260259
headings: ModuleOptions['headings']
261-
useNuxtImage?: boolean
262260
}
263261
}
264262

src/runtime/components/prose/ProseImg.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<component
3-
:is="imgComponent"
3+
:is="ImageComponent"
44
:src="refinedSrc"
55
:alt="props.alt"
66
:width="props.width"
@@ -10,9 +10,9 @@
1010

1111
<script setup lang="ts">
1212
import { withTrailingSlash, withLeadingSlash, joinURL } from 'ufo'
13-
import { useRuntimeConfig, computed, resolveComponent } from '#imports'
13+
import { useRuntimeConfig, computed } from '#imports'
1414
15-
const imgComponent = useRuntimeConfig().public.mdc.useNuxtImage ? resolveComponent('NuxtImg') : 'img'
15+
import ImageComponent from '#build/mdc-image-component.mjs'
1616
1717
const props = defineProps({
1818
src: {

0 commit comments

Comments
 (0)