From 70e6695c5bb481e203fca72d08d246fed606a77e Mon Sep 17 00:00:00 2001 From: Romain Lanz Date: Thu, 20 Jun 2024 11:36:02 +0200 Subject: [PATCH] feat: throw if HMR is enabled and controller is directly imported --- src/exceptions.ts | 6 ++++++ src/router/route.ts | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/src/exceptions.ts b/src/exceptions.ts index 28d5b6e..fc6b5ed 100644 --- a/src/exceptions.ts +++ b/src/exceptions.ts @@ -22,6 +22,12 @@ export const E_CANNOT_LOOKUP_ROUTE = createError<[routeIdentifier: string]>( 500 ) +export const E_DIRECT_CONTROLLER_IMPORT = createError<[controllerName: string]>( + 'Cannot import controller "%s" directly with HMR enabled. Make sure to lazy import it', + 'E_DIRECT_CONTROLLER_IMPORT', + 500 +) + export const E_HTTP_EXCEPTION = class HttpException extends Exception { body: any static code = 'E_HTTP_EXCEPTION' diff --git a/src/router/route.ts b/src/router/route.ts index 6893110..d2b0ee3 100644 --- a/src/router/route.ts +++ b/src/router/route.ts @@ -16,6 +16,7 @@ import { moduleCaller, moduleImporter } from '@adonisjs/fold' import { execute } from './executor.js' import { dropSlash } from '../helpers.js' +import { E_DIRECT_CONTROLLER_IMPORT } from '../exceptions.js' import type { Constructor, LazyImport, OneOrMore } from '../types/base.js' import type { @@ -162,6 +163,11 @@ export class Route = any> extends Macroable * The first item of the tuple is a class constructor */ if (is.class(handler[0])) { + // @ts-expect-error - Dynamic property added by hot-hook + if (import.meta.hot) { + throw new E_DIRECT_CONTROLLER_IMPORT([handler[0].name]) + } + return { reference: handler, ...moduleCaller(handler[0], (handler[1] || 'handle') as string).toHandleMethod(),