Skip to content

Commit bebe104

Browse files
authored
refactor!: remove unused runtime utilities and simplify code (#1816)
1 parent c4bba1b commit bebe104

File tree

19 files changed

+142
-214
lines changed

19 files changed

+142
-214
lines changed

knip.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@
55
"entry": [
66
"build.config.ts",
77
"src/module.ts",
8-
"src/runtime/**",
8+
"src/runtime/index.ts",
9+
"src/runtime/components/**",
10+
"src/runtime/composables/**",
11+
"src/runtime/plugins/**",
12+
"src/runtime/providers/**",
13+
"src/runtime/server/**",
914
"scripts/*"
1015
],
1116
"ignoreUnresolved": [
12-
"#app/nuxt",
1317
"#internal/nuxt-image"
1418
],
1519
"ignoreDependencies": [
@@ -38,8 +42,7 @@
3842
"ohash",
3943
"@vueuse/core",
4044
"@vueuse/nuxt",
41-
"nuxt-og-image",
42-
"perfect-debounce"
45+
"nuxt-og-image"
4346
]
4447
},
4548
"example": {
@@ -54,9 +57,6 @@
5457
"{app,error}.vue",
5558
"layers/**",
5659
"*.ts"
57-
],
58-
"ignoreDependencies": [
59-
"@nuxt/image"
6060
]
6161
}
6262
}

src/ipx.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export const ipxSetup: IPXSetupT = setupOptions => (providerOptions, moduleOptio
6363
const ipxHandler = <NitroEventHandler>{
6464
route: `${ipxBaseURL}/**`,
6565
middleware: false,
66-
handler: resolver.resolve('./runtime/ipx'),
66+
handler: resolver.resolve('./runtime/server/routes/_ipx'),
6767
}
6868

6969
if (!setupOptions?.isStatic) {

src/module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ ${BuiltInProviders.map(p => ` ${JSON.stringify(p)}: ReturnType<typeof
195195

196196
if (options.inject) {
197197
// Add runtime plugin
198-
addPlugin({ src: resolver.resolve('./runtime/plugin') })
198+
addPlugin({ src: resolver.resolve('./runtime/plugins/image') })
199199
}
200200

201201
// TODO: Transform asset urls that pass to `src` attribute on image components

src/runtime/components/NuxtImg.vue

Lines changed: 34 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,14 @@
22
<img
33
v-if="!custom"
44
ref="imgEl"
5-
:class="placeholder && !placeholderLoaded ? placeholderClass : undefined"
6-
v-bind="{
7-
...isServer ? { onerror: 'this.setAttribute(\'data-error\', 1)' } : {},
8-
...imgAttrs,
9-
...attrs,
10-
}"
5+
:class="placeholder ? placeholderClass : undefined"
6+
v-bind="imgAttrs"
117
:src="src"
128
>
139
<slot
1410
v-else
1511
v-bind="{
16-
...isServer ? { onerror: 'this.setAttribute(\'data-error\', 1)' } : {},
17-
imgAttrs: {
18-
...imgAttrs,
19-
...attrs,
20-
},
12+
imgAttrs,
2113
isLoaded: placeholderLoaded,
2214
src,
2315
}"
@@ -30,11 +22,10 @@ import type { ImgHTMLAttributes } from 'vue'
3022
import type { ProviderDefaults, ConfiguredImageProviders } from '@nuxt/image'
3123
3224
import { useImage } from '../composables'
33-
import { parseSize } from '../utils'
3425
import { prerenderStaticImages } from '../utils/prerender'
3526
import { markFeatureUsage } from '../utils/performance'
36-
import { useImageModifiers, useNormalisedAttrs, useProviderOptions } from './_base'
37-
import type { BaseImageProps } from './_base'
27+
import { useImageProps } from '../utils/props'
28+
import type { BaseImageProps } from '../utils/props'
3829
3930
import { useHead, useNuxtApp, useRequestEvent } from '#imports'
4031
@@ -44,86 +35,73 @@ export interface ImageProps<Provider extends keyof ConfiguredImageProviders> ext
4435
placeholderClass?: string
4536
}
4637
47-
const attrs = useAttrs() as ImgHTMLAttributes
4838
const props = defineProps<ImageProps<Provider>>()
4939
5040
const emit = defineEmits<{
5141
(event: 'load', payload: Event): unknown
5242
(event: 'error', payload: string | Event): unknown
5343
}>()
5444
55-
defineSlots<{
56-
default(props: {
57-
imgAttrs: ImgHTMLAttributes
58-
isLoaded: boolean
59-
src?: string
60-
}): any
61-
}>()
62-
63-
const isServer = import.meta.server
64-
65-
const modifiers = useImageModifiers(props)
45+
defineSlots<{ default(props: DefaultSlotProps): any }>()
6646
6747
const $img = useImage()
68-
const providerOptions = useProviderOptions(props)
48+
const { providerOptions, normalizedAttrs, imageModifiers } = useImageProps(props)
6949
7050
const sizes = computed(() => $img.getSizes(props.src!, {
7151
...providerOptions.value,
7252
sizes: props.sizes,
7353
densities: props.densities,
74-
modifiers: {
75-
...modifiers.value,
76-
width: parseSize(props.width),
77-
height: parseSize(props.height),
78-
},
54+
modifiers: imageModifiers.value,
7955
}))
8056
8157
const placeholderLoaded = ref(false)
82-
const baseAttrs = useNormalisedAttrs(props)
8358
59+
const attrs = useAttrs() as ImgHTMLAttributes
8460
const imgAttrs = computed(() => ({
85-
...baseAttrs.value,
61+
...normalizedAttrs.value,
8662
'data-nuxt-img': '',
8763
...(!props.placeholder || placeholderLoaded.value)
8864
? { sizes: sizes.value.sizes, srcset: sizes.value.srcset }
8965
: {},
66+
...import.meta.server ? { onerror: 'this.setAttribute(\'data-error\', 1)' } : {},
67+
...attrs,
9068
}))
9169
9270
const placeholder = computed(() => {
93-
let placeholder = props.placeholder
94-
95-
if (placeholder === '') {
96-
placeholder = true
71+
if (placeholderLoaded.value) {
72+
return false
9773
}
9874
99-
if (!placeholder || placeholderLoaded.value) {
75+
const placeholder = props.placeholder === '' ? [10, 10] : props.placeholder
76+
77+
if (!placeholder) {
10078
return false
10179
}
10280
10381
if (typeof placeholder === 'string') {
10482
return placeholder
10583
}
10684
107-
const size = (Array.isArray(placeholder)
85+
const [width = 10, height = width, quality = 50, blur = 3] = Array.isArray(placeholder)
10886
? placeholder
109-
: (typeof placeholder === 'number' ? [placeholder, placeholder] : [10, 10])) as [w: number, h: number, q: number, b: number]
87+
: typeof placeholder === 'number' ? [placeholder] : []
11088
11189
return $img(props.src!, {
112-
...modifiers.value,
113-
width: size[0],
114-
height: size[1],
115-
quality: size[2] || 50,
116-
blur: size[3] || 3,
90+
...imageModifiers.value,
91+
width,
92+
height,
93+
quality,
94+
blur,
11795
}, providerOptions.value)
11896
})
11997
12098
const mainSrc = computed(() =>
12199
props.sizes
122100
? sizes.value.src
123-
: $img(props.src!, modifiers.value, providerOptions.value),
101+
: $img(props.src!, imageModifiers.value, providerOptions.value),
124102
)
125103
126-
const src = computed(() => placeholder.value ? placeholder.value : mainSrc.value)
104+
const src = computed(() => placeholder.value || mainSrc.value)
127105
128106
if (import.meta.server && props.preload) {
129107
const isResponsive = Object.values(sizes.value).every(v => v)
@@ -203,3 +181,11 @@ onMounted(() => {
203181
}
204182
})
205183
</script>
184+
185+
<script lang="ts">
186+
export interface DefaultSlotProps {
187+
imgAttrs: ImgHTMLAttributes
188+
isLoaded: boolean
189+
src?: string
190+
}
191+
</script>

0 commit comments

Comments
 (0)