From 7c9a9c93309b409e6053d1771155b402a21bb614 Mon Sep 17 00:00:00 2001 From: Willyams Yujra Date: Thu, 14 Mar 2024 20:53:38 -0400 Subject: [PATCH] + FIX https://github.com/yracnet/vite-plugin-api-routes/issues/20 --- package.json | 4 ++-- src/plugin/main.ts | 10 +++++++--- src/plugin/write/common.ts | 7 ++++--- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index ba1464b..1d26967 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vite-plugin-api-routes", - "version": "1.1.2-beta", + "version": "1.1.3-beta", "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.3.3", "vite": "^2.0.0" } -} \ No newline at end of file +} diff --git a/src/plugin/main.ts b/src/plugin/main.ts index ff7b071..04f9e60 100644 --- a/src/plugin/main.ts +++ b/src/plugin/main.ts @@ -54,12 +54,16 @@ export const pluginImpl = (config: PluginConfig): PluginOption => { watcher.on("add", onReload); watcher.on("change", onReload); const baseApi = path.join(vite.base, config.routeBase); - const configure = await ssrLoadModule(config.configure, { fixStacktrace: true }); + const configure = await ssrLoadModule(config.configure, { + fixStacktrace: true, + }); //@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 }); + const module = await ssrLoadModule(config.handler, { + fixStacktrace: true, + }); module.handler(req, res, next); } catch (error) { ssrFixStacktrace(error as Error); @@ -103,7 +107,7 @@ export const pluginImpl = (config: PluginConfig): PluginOption => { onwarn: (warning: any, handler: any) => { if ( warning.code === "MISSING_EXPORT" && - warning.id === config.routersFile + warning.id.startsWith(config.cacheDir) ) { return; } diff --git a/src/plugin/write/common.ts b/src/plugin/write/common.ts index 2f6b679..a4e1e9a 100644 --- a/src/plugin/write/common.ts +++ b/src/plugin/write/common.ts @@ -29,10 +29,10 @@ export type FileRouter = { }; const sortURLParams = (a: string, b: string) => { - a = a.replace(/\:|\$|\[/gi, 'zz'); - b = b.replace(/\:|\$|\[/gi, 'zz'); + a = a.replace(/\:|\$|\[/gi, "zz"); + b = b.replace(/\:|\$|\[/gi, "zz"); return a.localeCompare(b); -} +}; export const getFileRouters = (config: PluginConfig): FileRouter[] => { let { dirs, include, exclude } = config; @@ -55,6 +55,7 @@ export const getFileRouters = (config: PluginConfig): FileRouter[] => { route = path.join("/", route); const pathName = path.join("/", config.routeBase, route); file = path.join(it.dir, file); + file = path.relative(config.root, file); return { order: jx, name: `_${ix}_${jx}`,