/
module.ts
95 lines (78 loc) · 2.59 KB
/
module.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { resolve } from 'upath'
import defu from 'defu'
import type { Module } from '@nuxt/types'
import { setupStaticGeneration } from './generate'
import { resolveProviders, detectProvider } from './provider'
import { pick, pkg } from './utils'
import type { ModuleOptions, CreateImageOptions } from './types'
const imageModule: Module<ModuleOptions> = async function imageModule (moduleOptions) {
const { nuxt, addPlugin } = this
const defaults: ModuleOptions = {
staticFilename: '[publicPath]/image/[hash][ext]',
provider: 'auto',
presets: {},
dir: resolve(nuxt.options.srcDir, nuxt.options.dir.static),
domains: [],
sharp: {},
// https://tailwindcss.com/docs/breakpoints
screens: {
xs: 320,
sm: 640,
md: 768,
lg: 1024,
xl: 1280,
xxl: 1536,
'2xl': 1536
},
internalUrl: '',
providers: {},
static: {}
}
const options: ModuleOptions = defu(moduleOptions, nuxt.options.image, defaults)
options.provider = detectProvider(options.provider, nuxt.options.target === 'static')
options[options.provider] = options[options.provider] || {}
const imageOptions: Omit<CreateImageOptions, 'providers'> = pick(options, [
'screens',
'presets',
'provider'
])
options.static = options.static || {}
options.static.domains = options.domains
const providers = resolveProviders(nuxt, options)
// Run setup
for (const p of providers) {
if (typeof p.setup === 'function') {
await p.setup(p, options, nuxt)
}
}
// Transpile and alias runtime
const runtimeDir = resolve(__dirname, 'runtime')
nuxt.options.alias['~image'] = runtimeDir
nuxt.options.build.transpile.push(runtimeDir, '@nuxt/image', 'allowlist', 'defu', 'ufo')
// Add plugin
addPlugin({
fileName: 'image.js',
src: resolve(runtimeDir, 'plugin.js'),
options: {
imageOptions,
providers
}
})
// Transform asset urls that pass to `src` attribute on image components
nuxt.options.build.loaders = defu({
vue: { transformAssetUrls: { 'nuxt-img': 'src', 'nuxt-picture': 'src', NuxtPicture: 'src', NuxtImg: 'src' } }
}, nuxt.options.build.loaders || {})
nuxt.hook('generate:before', () => {
setupStaticGeneration(nuxt, options)
})
const LruCache = await import('lru-cache').then(r => r.default || r)
const cache = new LruCache()
nuxt.hook('vue-renderer:context', (ssrContext: any) => {
ssrContext.cache = cache
})
nuxt.hook('listen', (_: any, listener: any) => {
options.internalUrl = `http://localhost:${listener.port}`
})
}
; (imageModule as any).meta = pkg
export default imageModule