@@ -74,47 +74,6 @@ export function VitePluginLocal(options: VitePluginLocalOptions): Plugin {
74
74
let cleanupPromise : Promise < void > | null = null
75
75
let server : ViteDevServer | undefined
76
76
77
- // sudo check in plugin initialization
78
- let sudoPromise : Promise < void > | null = null
79
-
80
- if ( enabled ) {
81
- sudoPromise = ( async ( ) => {
82
- const config = buildConfig ( options )
83
- const domain = config . to
84
-
85
- const needsSudo = await needsSudoAccess ( options , domain )
86
- if ( needsSudo ) {
87
- hasSudoAccess = await checkInitialSudo ( )
88
-
89
- if ( ! hasSudoAccess ) {
90
- // Temporarily disable console.log to prevent VitePress output
91
- const origLog = console . log
92
- console . log = ( ) => { }
93
-
94
- process . stdout . write ( '\nSudo access required for proxy setup.\n' )
95
-
96
- hasSudoAccess = await new Promise < boolean > ( ( resolve ) => {
97
- const sudo = spawn ( 'sudo' , [ 'true' ] , {
98
- stdio : 'inherit' ,
99
- } )
100
-
101
- sudo . on ( 'exit' , ( code ) => {
102
- resolve ( code === 0 )
103
- } )
104
- } )
105
-
106
- // Restore console.log
107
- console . log = origLog
108
-
109
- if ( ! hasSudoAccess ) {
110
- console . error ( 'Failed to get sudo access. Please try again.' )
111
- process . exit ( 1 )
112
- }
113
- }
114
- }
115
- } ) ( )
116
- }
117
-
118
77
const debug = ( ...args : any [ ] ) => {
119
78
if ( verbose )
120
79
originalConsole . log ( '[vite-plugin-local]' , ...args )
@@ -130,7 +89,6 @@ export function VitePluginLocal(options: VitePluginLocalOptions): Plugin {
130
89
debug ( 'Starting cleanup process' )
131
90
132
91
try {
133
- // Store the cleanup promise
134
92
cleanupPromise = cleanup ( {
135
93
domains,
136
94
hosts : typeof cleanupOpts === 'boolean' ? cleanupOpts : cleanupOpts ?. hosts ,
@@ -144,7 +102,7 @@ export function VitePluginLocal(options: VitePluginLocalOptions): Plugin {
144
102
}
145
103
catch ( error ) {
146
104
console . error ( 'Error during cleanup:' , error )
147
- throw error // Re-throw to ensure process exits with error
105
+ throw error
148
106
}
149
107
finally {
150
108
isCleaningUp = false
@@ -164,35 +122,64 @@ export function VitePluginLocal(options: VitePluginLocalOptions): Plugin {
164
122
process . exit ( 1 )
165
123
}
166
124
167
- if ( server ?. httpServer ) {
125
+ if ( server ?. httpServer )
168
126
server . httpServer . close ( )
169
- }
170
127
171
- // Only exit if we're handling a signal
172
- if ( signal !== 'CLOSE' ) {
128
+ if ( signal !== 'CLOSE' )
173
129
process . exit ( 0 )
174
- }
175
130
}
176
131
177
132
return {
178
133
name : 'vite-plugin-local' ,
179
134
enforce : 'pre' ,
135
+ apply : 'serve' ,
136
+
137
+ configResolved ( resolvedConfig ) {
138
+ // Early exit if we're in build mode
139
+ if ( resolvedConfig . command === 'build' )
140
+ return
141
+ } ,
180
142
181
143
async configureServer ( viteServer : ViteDevServer ) {
182
144
if ( ! enabled )
183
145
return
184
146
185
- // Wait for sudo access before continuing
186
- if ( sudoPromise ) {
187
- await sudoPromise
188
- }
189
-
190
147
server = viteServer
191
-
192
- // Store original console for debug
193
148
originalConsole = { ...console }
194
149
195
- // Add cleanup handlers for the server
150
+ // Move sudo check here
151
+ const config = buildConfig ( options )
152
+ const domain = config . to
153
+
154
+ const needsSudo = await needsSudoAccess ( options , domain )
155
+ if ( needsSudo ) {
156
+ hasSudoAccess = await checkInitialSudo ( )
157
+
158
+ if ( ! hasSudoAccess ) {
159
+ const origLog = console . log
160
+ console . log = ( ) => { }
161
+
162
+ process . stdout . write ( '\nSudo access required for proxy setup.\n' )
163
+
164
+ hasSudoAccess = await new Promise < boolean > ( ( resolve ) => {
165
+ const sudo = spawn ( 'sudo' , [ 'true' ] , {
166
+ stdio : 'inherit' ,
167
+ } )
168
+
169
+ sudo . on ( 'exit' , ( code ) => {
170
+ resolve ( code === 0 )
171
+ } )
172
+ } )
173
+
174
+ console . log = origLog
175
+
176
+ if ( ! hasSudoAccess ) {
177
+ console . error ( 'Failed to get sudo access. Please try again.' )
178
+ process . exit ( 1 )
179
+ }
180
+ }
181
+ }
182
+
196
183
server . httpServer ?. on ( 'close' , ( ) => {
197
184
debug ( 'Server closing, cleaning up...' )
198
185
handleSignal ( 'CLOSE' )
0 commit comments