diff --git a/package.json b/package.json index 2f190f2..f5c1622 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vite-plugin-api-routes", - "version": "1.1.10", + "version": "1.1.11", "type": "module", "description": "A Vite.js plugin that creates API routes by mapping the directory structure, similar to Next.js API Routes. This plugin enhances the functionality for backend development using Vite.", "keywords": [ @@ -72,4 +72,4 @@ "typescript": "^5.4.4", "vite": "^2.0.0" } -} \ No newline at end of file +} diff --git a/src/plugin/main.ts b/src/plugin/main.ts index 3e82523..9011edd 100644 --- a/src/plugin/main.ts +++ b/src/plugin/main.ts @@ -60,20 +60,23 @@ export const pluginImpl = (config: PluginConfig): PluginOption => { }); //@ts-ignore configure.viteServerBefore?.(devServer.middlewares, devServer, vite); - middlewares.use(baseApi, async (req: any, res, next) => { - try { - const module = await ssrLoadModule(config.handler, { - fixStacktrace: true, - }); - module.handler(req, res, next); - } catch (error) { - ssrFixStacktrace(error as Error); - process.exitCode = 1; - next(error); - } - }); - //@ts-ignore - configure.viteServerAfter?.(devServer.middlewares, devServer, vite); + return () => { + // Register Proxy After Vite Inicialize + middlewares.use(baseApi, async (req, res, next) => { + try { + const { handler } = await ssrLoadModule(config.handler, { + fixStacktrace: true, + }); + handler(req, res, next); + } catch (error) { + ssrFixStacktrace(error as Error); + process.exitCode = 1; + next(error); + } + }); + //@ts-ignore + configure.viteServerAfter?.(devServer.middlewares, devServer, vite); + }; }, writeBundle: async () => { if (process.env.IS_API_BUILD) return;