@@ -8,8 +8,11 @@ import { CaddyInstant } from './caddy'
88
99let config : ResolvedConfig
1010
11+ let caddy : CaddyInstant
12+
1113export const unpluginFactory : UnpluginFactory < Options > = options => ( {
1214 name : 'unplugin-https-reverse-proxy' ,
15+ enforce : 'post' ,
1316 vite : {
1417 configResolved ( _config ) {
1518 config = _config
@@ -62,19 +65,33 @@ export const unpluginFactory: UnpluginFactory<Options> = options => ({
6265 console . log ( 'please provide target' )
6366 return
6467 }
65- const source = `${ compiler . options . devServer ?. host || '127.0.0.1' } :${ compiler . options . devServer ?. port || '8080' } `
66-
6768 try {
68- const caddy = new CaddyInstant ( )
69+ // @ts -expect-error vuecli
70+ const devServer = compiler . options . devServer || process . VUE_CLI_SERVICE . projectOptions . devServer
71+ const source = `${ devServer ?. host || '127.0.0.1' } :${ devServer ?. port || '8080' } `
72+
73+ if ( caddy )
74+ return
75+
76+ caddy = new CaddyInstant ( )
77+
6978 caddy . run ( source , target ) . then ( ( stop ) => {
7079 const colorUrl = ( url : string ) => c . green ( url . replace ( / : ( \d + ) \/ / , ( _ , port ) => `:${ c . bold ( port ) } /` ) )
71- // eslint-disable-next-line no-console
72- console . log ( ` ${ c . green ( '➜' ) } ${ c . bold ( 'run caddy reverse proxy success' ) } : ${ colorUrl ( `https://${ target } ` ) } ` )
80+
81+ compiler . hooks . done . tap ( 'unplugin-https-reverse-proxy' , ( stats ) => {
82+ if ( stats . hasErrors ( ) )
83+ return
84+
85+ // eslint-disable-next-line no-console
86+ console . log ( ` ${ c . green ( '➜' ) } ${ c . bold ( 'run caddy reverse proxy success' ) } : ${ colorUrl ( `https://${ target } ` ) } ` )
87+ } )
7388
7489 process . on ( 'SIGINT' , async ( ) => {
7590 await stop ( )
7691 process . exit ( )
7792 } )
93+ } ) . catch ( ( e ) => {
94+ throw e
7895 } )
7996 }
8097 catch ( e ) {
0 commit comments