Skip to content

Commit 0695aeb

Browse files
committed
feat: use nitro virtual modules for middlewares
1 parent e0a58ea commit 0695aeb

File tree

2 files changed

+39
-39
lines changed

2 files changed

+39
-39
lines changed

playground/nuxt.config.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
import { defineNuxtConfig } from 'nuxt'
2-
import Module from '../src/module'
3-
41
// https://v3.nuxtjs.org/api/configuration/nuxt.config
52
export default defineNuxtConfig({
6-
modules: [Module],
3+
modules: ['nuxt-proxy'],
74
runtimeConfig: {
85
proxy: {
96
options: [
@@ -31,5 +28,5 @@ export default defineNuxtConfig({
3128
},
3229
],
3330
},
34-
}
31+
},
3532
})

src/module.ts

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,19 @@
11
import { fileURLToPath } from 'url'
2-
import { addServerHandler, addTemplate, defineNuxtModule } from '@nuxt/kit'
2+
import { addServerHandler, defineNuxtModule } from '@nuxt/kit'
33
import type { Options } from 'http-proxy-middleware'
4-
import { join } from 'pathe'
5-
import dedent from 'dedent'
64
import { hash, objectHash } from 'ohash'
75

8-
function createProxyMiddleware(buildDir: string, filename: string, options: Options, index?: number) {
9-
addTemplate({
10-
filename,
11-
write: true,
12-
getContents: () => dedent`
13-
import { createProxyMiddleware } from 'nuxt-proxy/middleware'
14-
import { defu } from 'defu'
15-
import { useRuntimeConfig } from '#imports'
16-
17-
const buildtimeOptions = ${JSON.stringify(options)}
18-
const runtimeOptions = [].concat(useRuntimeConfig().proxy?.options)[${JSON.stringify(index)} ?? 0]
19-
20-
export default createProxyMiddleware(defu(runtimeOptions, buildtimeOptions))
21-
`,
22-
})
23-
24-
addServerHandler({
25-
handler: join(buildDir, filename),
26-
middleware: true,
27-
})
6+
function createProxyMiddleware(options: Options, index?: number) {
7+
return `
8+
import { createProxyMiddleware } from 'nuxt-proxy/middleware'
9+
import { defu } from 'defu'
10+
import { useRuntimeConfig } from '#imports'
11+
12+
const buildtimeOptions = ${JSON.stringify(options)}
13+
const runtimeOptions = [].concat(useRuntimeConfig().proxy?.options)[${JSON.stringify(index)} ?? 0]
14+
15+
export default createProxyMiddleware(defu(runtimeOptions, buildtimeOptions))
16+
`
2817
}
2918

3019
export interface ModuleOptions {
@@ -41,20 +30,34 @@ export default defineNuxtModule<ModuleOptions>({
4130
},
4231
setup(options, nuxt) {
4332
const runtimeDir = fileURLToPath(new URL('./runtime', import.meta.url))
44-
nuxt.options.build.transpile.push(runtimeDir)
33+
nuxt.options.build.transpile.push(runtimeDir, '#proxy-handler')
4534

4635
const finalConfig = (nuxt.options.runtimeConfig.proxy = nuxt.options.runtimeConfig.proxy || { options: options.options }) as ModuleOptions
4736

48-
if (Array.isArray(finalConfig.options)) {
49-
finalConfig.options.forEach((options, index) => {
50-
const filename = `proxy/handler_${hash(objectHash(options))}.ts`
51-
createProxyMiddleware(nuxt.options.buildDir, filename, options, index)
52-
})
53-
}
54-
else {
55-
const filename = 'proxy_handler.ts'
56-
createProxyMiddleware(nuxt.options.buildDir, filename, finalConfig.options)
57-
}
37+
nuxt.hook('nitro:config', (nitroConfig) => {
38+
nitroConfig.virtual = nitroConfig.virtual || {}
39+
40+
if (Array.isArray(finalConfig.options)) {
41+
finalConfig.options.forEach((options, index) => {
42+
const handler = `#proxy-handler-${hash(objectHash(options))}`
43+
nitroConfig.virtual![handler] = createProxyMiddleware(options, index)
44+
45+
addServerHandler({
46+
handler,
47+
middleware: true,
48+
})
49+
})
50+
}
51+
else {
52+
const handler = '#proxy-handler'
53+
nitroConfig.virtual[handler] = createProxyMiddleware(finalConfig.options)
54+
55+
addServerHandler({
56+
handler,
57+
middleware: true,
58+
})
59+
}
60+
})
5861
},
5962
})
6063

0 commit comments

Comments
 (0)