-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfiguration.ts
More file actions
184 lines (163 loc) · 5.11 KB
/
configuration.ts
File metadata and controls
184 lines (163 loc) · 5.11 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import type { Nuxt } from '@nuxt/schema'
import type { Resolver } from '@nuxt/kit'
import { addPlugin, addPluginTemplate } from '@nuxt/kit'
import type { HttpClientHintsOptions } from '../types'
import type { ResolvedHttpClientHintsOptions } from '../runtime/shared-types/types'
type PluginType = 'detect' | 'user-agent' | 'network' | 'device' | 'critical'
export interface HttpClientHintsContext {
resolver: Resolver
logger: ReturnType<typeof import('@nuxt/kit')['useLogger']>
options: HttpClientHintsOptions
resolvedOptions: ResolvedHttpClientHintsOptions
clientDependsOn: PluginType[]
serverDependsOn: PluginType[]
}
export function configure(ctx: HttpClientHintsContext, nuxt: Nuxt) {
const {
options,
resolvedOptions,
resolver,
logger,
clientDependsOn,
serverDependsOn,
} = ctx
const runtimeDir = resolver.resolve('./runtime')
const {
userAgent,
network,
device,
critical,
} = options
if (userAgent) {
if (userAgent === true) {
resolvedOptions.userAgent.push(
'architecture',
'bitness',
'model',
'platformVersion',
'fullVersionList',
)
}
else if (Array.isArray(userAgent)) {
resolvedOptions.userAgent.push(...userAgent)
}
else {
resolvedOptions.userAgent.push(userAgent)
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const clientOnly = nuxt.options.nitro.static || (nuxt.options as any)._generate /* TODO: remove in future */ || !nuxt.options.ssr
// we register the client detector only if needed and not in SSR mode
if ((options.detectBrowser || options.detectOS || resolvedOptions.userAgent.length) && clientOnly) {
nuxt.options.build.transpile.push(runtimeDir)
nuxt.hook('prepare:types', ({ references }) => {
references.push({ path: resolver.resolve(runtimeDir, 'plugins/types') })
})
addPlugin(resolver.resolve(runtimeDir, 'plugins/detect.client'))
return
}
// servers plugin work only with SSR
if (clientOnly) {
logger.warn('http-client-hints module is only supported in SSR mode')
return
}
nuxt.options.build.transpile.push(runtimeDir)
if (network) {
if (network === true) {
resolvedOptions.network.push('savedata', 'downlink', 'ect', 'rtt')
}
else if (Array.isArray(network)) {
resolvedOptions.network.push(...network)
}
else {
resolvedOptions.network.push(network)
}
if (resolvedOptions.network.length) {
serverDependsOn.push('network')
}
}
if (device) {
if (device === true) {
resolvedOptions.device.push('memory')
}
else if (Array.isArray(device)) {
resolvedOptions.device.push(...device)
}
else {
resolvedOptions.device.push(device)
}
if (resolvedOptions.device.length) {
serverDependsOn.push('device')
}
}
if (critical) {
resolvedOptions.critical = critical
serverDependsOn.push('critical')
}
if (!serverDependsOn.length) {
return
}
nuxt.hook('prepare:types', ({ references }) => {
references.push({ path: resolver.resolve(runtimeDir, 'plugins/types') })
})
if (options.detectOS) {
resolvedOptions.detectOS = options.detectOS
}
nuxt.options.runtimeConfig.public.httpClientHints = resolvedOptions
addPlugin(resolver.resolve(runtimeDir, 'plugins/init.server'))
if (options.detectBrowser || options.detectOS || resolvedOptions.userAgent.length) {
clientDependsOn.push('detect')
serverDependsOn.push('detect')
addPlugin(resolver.resolve(runtimeDir, 'plugins/detect.client'))
addPlugin(resolver.resolve(runtimeDir, 'plugins/detect.server'))
}
if (serverDependsOn.includes('network')) {
addPlugin(resolver.resolve(runtimeDir, 'plugins/network.server'))
}
if (serverDependsOn.includes('device')) {
addPlugin(resolver.resolve(runtimeDir, 'plugins/device.server'))
}
if (serverDependsOn.includes('critical')) {
addPlugin(resolver.resolve(runtimeDir, 'plugins/critical.server'))
}
if (clientDependsOn.length) {
// @ts-expect-error missing at build time
addClientHintsPlugin('client', clientDependsOn.map(p => `http-client-hints:${p}-client:plugin`))
}
// @ts-expect-error missing at build time
addClientHintsPlugin('server', serverDependsOn.map(p => `http-client-hints:${p}-server:plugin`))
}
function addClientHintsPlugin(
mode: 'client' | 'server',
dependsOn: import('#app').NuxtAppLiterals['pluginName'][],
) {
const name = `http-client-hints:${mode}:plugin`
addPluginTemplate({
filename: `http-client-hints.${mode}.mjs`,
name,
mode: `${mode}`,
write: false,
getContents() {
return `import { defineNuxtPlugin, readonly, useState } from '#imports'
export default defineNuxtPlugin({
name: '${name}',
order: 'pre',
dependsOn: ${JSON.stringify(dependsOn)},
parallel: false,
async setup(nuxtApp) {
const clientHints = useState('http-client-hints:state')
await nuxtApp.hooks.callHook(
'http-client-hints:${mode === 'server' ? 'ssr-' : ''}client-hints',
clientHints.value
)
return {
provide: {
httpClientHints: readonly(clientHints.value),
}
}
}
})
`
},
})
}