From e288e29ecc0ef3d9bc9375d23815d09974ccf075 Mon Sep 17 00:00:00 2001 From: DIYgod Date: Wed, 28 Feb 2024 20:53:29 +0800 Subject: [PATCH] feat: lazy load route --- lib/routes.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/routes.ts b/lib/routes.ts index b834ef8e59..7458881206 100644 --- a/lib/routes.ts +++ b/lib/routes.ts @@ -1,16 +1,17 @@ import { directoryImport } from 'directory-import'; import type { Hono, Handler } from 'hono'; import { serveStatic } from '@hono/node-server/serve-static'; +import * as path from 'node:path'; import index from '@/v3/index'; import robotstxt from '@/v3/robots.txt'; type Root = { - get: (path: string, handler: Handler) => void; + get: (routePath: string, filePath: string) => void; }; const imports = directoryImport({ - targetDirectoryPath: './v3', + targetDirectoryPath: './v2', importPattern: /router\.js$/, }); @@ -27,13 +28,14 @@ export default function (app: Hono) { for (const name in routes) { const subApp = app.basePath(`/${name}`); routes[name]({ - get: (path, handler) => { + get: (routePath, filePath) => { const wrapedHandler: Handler = async (ctx, ...args) => { if (!ctx.get('data')) { + const handler = require(path.join(__dirname, 'v2', name, filePath)); await handler(ctx, ...args); } }; - subApp.get(path, wrapedHandler); + subApp.get(routePath, wrapedHandler); }, }); }