@@ -31,13 +31,54 @@ export function universalApiPlugin(opts?: UniversalApiOptions): Plugin {
3131 let logger : Logger = new Logger ( Constants . PLUGIN_NAME , "info" ) ;
3232 let currentHandler : ( ( req : IncomingMessage , res : ServerResponse , next : Connect . NextFunction ) => Promise < void > ) | null = null ;
3333 let registeredOnServer : HttpServer | null = null ;
34- let configResolvedPromise : Promise < void > | null = null ;
34+ let asyncInitPromise : Promise < void > | null = null ;
35+
36+ function runAsyncInit ( ) : Promise < void > {
37+ if ( asyncInitPromise !== null ) {
38+ return asyncInitPromise ;
39+ }
40+
41+ asyncInitPromise = ( async ( ) => {
42+ if ( ! options . fsDir || ! ( await Utils . files . isDirExists ( options . fullFsDir ! ) ) ) {
43+ options . fullFsDir = null ;
44+ logger . info ( `Directory with path ${ options . fsDir } doesn't exist.` ) ;
45+ }
46+ if ( options . endpointPrefix . length === 0 ) {
47+ logger . warn ( `Endpoint prefix empty or invalid` ) ;
48+ options . disable = true ;
49+ }
50+
51+ logger . info ( `plugin ${ options . disable ? "disabled" : "started" } ` ) ;
52+ logger . debug ( "Vite configResolved: FINISH" ) ;
53+
54+ /* v8 ignore start */
55+ /**
56+ * INFO
57+ * plugin print on process.stdout/stderr with console. If the process
58+ * is running in background, when the client disconnects the session,
59+ * stdout/stderr receive EIO/EPIPE errors.
60+ * Register a handler to avoid killing the process on uncaughtException.
61+ */
62+ const suppressIoError = ( err : NodeJS . ErrnoException ) => {
63+ if ( err . code !== 'EIO' && err . code !== 'EPIPE' ) {
64+ throw err ;
65+ }
66+ } ;
67+ if ( process . stdout . listenerCount ( 'error' ) === 0 ) {
68+ process . stdout . on ( 'error' , suppressIoError ) ;
69+ }
70+ if ( process . stderr . listenerCount ( 'error' ) === 0 ) {
71+ process . stderr . on ( 'error' , suppressIoError ) ;
72+ }
73+ /* v8 ignore stop */
74+ } ) ( ) ;
75+
76+ return asyncInitPromise ;
77+ }
3578
3679 // INFO Shared logic for configureServer and configurePreviewServer.
3780 async function setupServer ( server : ViteDevServer | PreviewServer , hookName : "configureServer" | "configurePreviewServer" ) : Promise < void > {
38- if ( configResolvedPromise ) {
39- await configResolvedPromise ;
40- }
81+ await runAsyncInit ( ) ;
4182
4283 const isPreview = hookName === "configurePreviewServer" ;
4384
@@ -96,43 +137,6 @@ export function universalApiPlugin(opts?: UniversalApiOptions): Plugin {
96137 logger = new Logger ( Constants . PLUGIN_NAME , options . logLevel ) ;
97138 logger . debug ( "Vite configResolved: START" ) ;
98139 logger . info ( `plugin initializing ...` ) ;
99-
100- configResolvedPromise = ( async ( ) => {
101- if ( ! options . fsDir || ! ( await Utils . files . isDirExists ( options . fullFsDir ! ) ) ) {
102- options . fullFsDir = null ;
103- logger . info ( `Directory with path ${ options . fsDir } doesn't exist.` ) ;
104- }
105- if ( options . endpointPrefix . length === 0 ) {
106- logger . warn ( `Endpoint prefix empty or invalid` ) ;
107- options . disable = true ;
108- }
109-
110- logger . info ( `plugin ${ options . disable ? "disabled" : "started" } ` ) ;
111- logger . debug ( "Vite configResolved: FINISH" ) ;
112- /* v8 ignore start */
113- /**
114- * INFO
115- * plugin print on process.stdout/stderr with console. If the process
116- * is running in background, when the client disconnects the session,
117- * stdout/stderr receive EIO/EPIPE errors.
118- * Register a handler to avoid killing the process on uncaughtException.
119- */
120- const suppressIoError = ( err : NodeJS . ErrnoException ) => {
121- if ( err . code !== 'EIO' && err . code !== 'EPIPE' ) {
122- throw err ;
123- }
124- } ;
125- if ( process . stdout . listenerCount ( 'error' ) === 0 ) {
126- process . stdout . on ( 'error' , suppressIoError ) ;
127- }
128- if ( process . stderr . listenerCount ( 'error' ) === 0 ) {
129- process . stderr . on ( 'error' , suppressIoError ) ;
130- }
131- /* v8 ignore stop */
132- } ) ( ) ;
133-
134- // INFO Return the promise so Vite awaits it when it supports async configResolved.
135- return configResolvedPromise ;
136140 } ,
137141 configureServer ( server ) {
138142 // INFO Return a function so Vite defers its execution to after the built-in middlewares are registered
0 commit comments