Skip to content

Commit 3041371

Browse files
authored
feat!: typed providers + modifiers (#1802)
1 parent b78c0ea commit 3041371

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+1518
-3033
lines changed

build.config.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1+
import { cp, readdir, rm } from 'node:fs/promises'
12
import { defineBuildConfig } from 'unbuild'
23

34
export default defineBuildConfig({
4-
rollup: {
5-
emitCJS: false,
6-
cjsBridge: false,
7-
},
85
externals: [
96
'ipx',
107
],
8+
hooks: {
9+
'rollup:done': async function () {
10+
// temporary solution for this PR while I create fix in `mkdist` (relative module extensions not correct when referring outside)
11+
for (const file of await readdir('dist')) {
12+
if (file.endsWith('.mjs') || file.endsWith('.d.mts')) {
13+
await cp(`dist/${file}`, `dist/${file.replace('.mjs', '.js').replace('.d.mts', '.d.ts')}`)
14+
await rm(`dist/${file}`)
15+
}
16+
}
17+
},
18+
},
1119
})

docs/content/4.advanced/1.custom-provider.md

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,24 @@ The runtime will receive a source, image modifiers and its provider options. It
99

1010
```ts [providers/my-provider.ts]
1111
import { joinURL } from 'ufo'
12-
import type { ProviderGetImage } from '@nuxt/image'
13-
import { createOperationsGenerator } from '#image'
12+
import { createOperationsGenerator, defineProvider } from '#image'
1413

1514
const operationsGenerator = createOperationsGenerator()
1615

17-
export const getImage: ProviderGetImage = (
18-
src,
19-
{ modifiers = {}, baseURL } = {}
20-
) => {
21-
if (!baseURL) {
22-
// also support runtime config
23-
baseURL = useRuntimeConfig().public.siteUrl
24-
}
16+
export default defineProvider<{ baseURL?: string }>({
17+
getImage (src, { modifiers, baseURL }) => {
18+
if (!baseURL) {
19+
// also support runtime config
20+
baseURL = useRuntimeConfig().public.siteUrl
21+
}
2522

26-
const operations = operationsGenerator(modifiers)
23+
const operations = operationsGenerator(modifiers)
2724

28-
return {
29-
url: joinURL(baseURL, src + (operations ? '?' + operations : ''))
25+
return {
26+
url: joinURL(baseURL, src + (operations ? '?' + operations : ''))
27+
}
3028
}
31-
}
29+
})
3230
```
3331

3432
### Parameters

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
"sideEffects": false,
1212
"type": "module",
1313
"exports": {
14-
".": "./dist/module.mjs"
14+
".": "./dist/module.js"
1515
},
16-
"main": "./dist/module.mjs",
16+
"main": "./dist/module.js",
1717
"typesVersions": {
1818
"*": {
1919
".": [
20-
"./dist/module.d.mts"
20+
"./dist/module.d.ts"
2121
]
2222
}
2323
},

playground/pages/provider/[provider].vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
:key="sample.src"
1010
>
1111
<nuxt-img
12-
:provider="provider.name"
12+
:provider="provider.name as any"
1313
v-bind="sample"
1414
:preload="{ fetchPriority: 'auto' }"
1515
/>

playground/providers.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import type { ImageProviders } from '@nuxt/image'
2+
13
export interface Provider {
2-
name: string
4+
name: keyof ImageProviders
35
samples: {
46
src: string
57
from?: string

playground/providers/custom/index.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { joinURL } from 'ufo'
2-
import type { ProviderGetImage } from '../../../src/types' // '@nuxt/image'
2+
import { defineProvider } from '#image'
33

4-
export const getImage: ProviderGetImage = (src, { modifiers = {}, baseURL = '/' } = {}) => {
5-
const operationsString = `w_${modifiers.width}&h_${modifiers.height}`
6-
return {
7-
url: joinURL(baseURL, operationsString, src),
8-
}
9-
}
4+
export default defineProvider<{ baseURL?: string }>({
5+
getImage: (src, { modifiers, baseURL = '/' }) => {
6+
const operationsString = `w_${modifiers.width}&h_${modifiers.height}`
7+
return {
8+
url: joinURL(baseURL, operationsString, src),
9+
}
10+
},
11+
})

0 commit comments

Comments
 (0)