1
1
import { fileURLToPath } from 'url'
2
- import { addServerHandler , addTemplate , defineNuxtModule } from '@nuxt/kit'
2
+ import { addServerHandler , defineNuxtModule } from '@nuxt/kit'
3
3
import type { Options } from 'http-proxy-middleware'
4
- import { join } from 'pathe'
5
- import dedent from 'dedent'
6
4
import { hash , objectHash } from 'ohash'
7
5
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
+ `
28
17
}
29
18
30
19
export interface ModuleOptions {
@@ -41,20 +30,34 @@ export default defineNuxtModule<ModuleOptions>({
41
30
} ,
42
31
setup ( options , nuxt ) {
43
32
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' )
45
34
46
35
const finalConfig = ( nuxt . options . runtimeConfig . proxy = nuxt . options . runtimeConfig . proxy || { options : options . options } ) as ModuleOptions
47
36
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
+ } )
58
61
} ,
59
62
} )
60
63
0 commit comments