-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
module.ts
56 lines (48 loc) · 1.61 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
import {
defineNuxtModule,
addServerHandler,
createResolver,
extendViteConfig,
addServerPlugin,
} from '@nuxt/kit'
import { withQuery } from 'ufo'
import polyfillist from 'polyfillist'
import browserslistToEsbuild from 'browserslist-to-esbuild'
type AnyString = (string & Record<never, never>)
// Module options TypeScript interface definition
export interface ModuleOptions {
target?: string | string[],
host?: 'selfhost' | AnyString,
}
export default defineNuxtModule<ModuleOptions>({
meta: {
name : 'nupolyon',
configKey: 'nupolyon',
},
// Default configuration options of the Nuxt module
defaults: {
host: 'https://polyfill.io/v3/polyfill.min.js',
},
async setup (options, nuxt) {
const resolver = createResolver(import.meta.url)
const features = await polyfillist(options.target)
const isSelfHost = options.host === 'selfhost'
const src = options.host && !isSelfHost
? withQuery(options.host, { features: features.join(',') })
: '/_nupolyon/polyfill' // NOTE: app.baseUrl will be prepended to this path at runtime in the plugin.
nuxt.options.runtimeConfig.nupolyon = { features }
nuxt.options.runtimeConfig.public.nupolyon = { src, isSelfHost }
addServerPlugin(resolver.resolve('./runtime/server/plugins/polyfill'))
if (isSelfHost) {
addServerHandler({
method : 'get',
route : '/_nupolyon/polyfill',
handler: resolver.resolve('./runtime/selfhost')
})
}
extendViteConfig((config) => {
if (config.build)
config.build.target = browserslistToEsbuild(options.target)
})
}
})