diff --git a/packages/router/src/lib/routes.spec.ts b/packages/router/src/lib/routes.spec.ts index b196b193a..187a57b8e 100644 --- a/packages/router/src/lib/routes.spec.ts +++ b/packages/router/src/lib/routes.spec.ts @@ -1,5 +1,6 @@ import { Route } from '@angular/router'; import { of } from 'rxjs'; +import { expect, vi } from 'vitest'; import { RouteExport, RouteMeta } from './models'; import { createRoutes, Files } from './routes'; import { ROUTE_META_TAGS_KEY } from './meta-tags'; @@ -605,4 +606,24 @@ describe('routes', () => { expect(routes[1].path).toBe('static'); }); }); + + describe('a route without default export', () => { + it('should throw error when default export is falsy', async () => { + const fileName = '/app/routes/index.ts'; + const files: Files = { + [fileName]: () => Promise.resolve({} as unknown as RouteExport), + }; + + const routes = createRoutes(files); + const route = routes[0]; + + const spy = vi.spyOn(console, 'warn'); + + await route.loadChildren?.(); + + expect(spy).toHaveBeenCalledWith( + `[Analog] Missing default export at ${fileName}` + ); + }); + }); }); diff --git a/packages/router/src/lib/routes.ts b/packages/router/src/lib/routes.ts index 3c60bd85c..b110475d0 100644 --- a/packages/router/src/lib/routes.ts +++ b/packages/router/src/lib/routes.ts @@ -182,15 +182,27 @@ function toRoutes(rawRoutes: RawRoute[], files: Files): Route[] { ? { path: rawRoute.segment, loadChildren: () => - module!().then((m) => [ - { - path: '', - component: m.default, - ...toRouteConfig(m.routeMeta as RouteMeta | undefined), - children, - [ANALOG_META_KEY]: analogMeta, - }, - ]), + module!().then((m) => { + if (!import.meta.env.PROD) { + const hasModuleDefault = !!m.default; + + if (!hasModuleDefault) { + console.warn( + `[Analog] Missing default export at ${rawRoute.filename}` + ); + } + } + + return [ + { + path: '', + component: m.default, + ...toRouteConfig(m.routeMeta as RouteMeta | undefined), + children, + [ANALOG_META_KEY]: analogMeta, + }, + ]; + }), } : { path: rawRoute.segment, children };