From f321233d579695db83838ed7277138c280213257 Mon Sep 17 00:00:00 2001 From: Anderson Feitosa Date: Tue, 2 Jul 2024 13:05:39 -0300 Subject: [PATCH 1/3] feat(router): add error message when default export is missing from component --- packages/router/src/lib/routes.ts | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/packages/router/src/lib/routes.ts b/packages/router/src/lib/routes.ts index 3c60bd85c..f47c6fd48 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) => { + const hasModuleDefault = !!m.default; + if ( + process.env['NODE_ENV'] !== 'production' && + !hasModuleDefault + ) { + throw new Error( + `[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 }; From 160d7fa5b37d681a2dc06fbcb06e69cf4768c3ef Mon Sep 17 00:00:00 2001 From: Anderson Feitosa Date: Tue, 2 Jul 2024 14:15:48 -0300 Subject: [PATCH 2/3] feat(router): add unit test to missing default export --- packages/router/src/lib/routes.spec.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/packages/router/src/lib/routes.spec.ts b/packages/router/src/lib/routes.spec.ts index b196b193a..87a082849 100644 --- a/packages/router/src/lib/routes.spec.ts +++ b/packages/router/src/lib/routes.spec.ts @@ -605,4 +605,22 @@ 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 files: Files = { + '/app/routes/index.ts': () => + Promise.resolve({} as unknown as RouteExport), + }; + + const routes = createRoutes(files); + const route = routes[0]; + + try { + await route.loadChildren?.(); + } catch (error) { + expect(error?.toString()).toMatch('Missing default export'); + } + }); + }); }); From 28c603d1cf0396eb1bd5e987a83f7e56a9848e05 Mon Sep 17 00:00:00 2001 From: Anderson Feitosa Date: Tue, 2 Jul 2024 16:05:58 -0300 Subject: [PATCH 3/3] feat(router): change throw error to console.warn and useimport.meta to verify production environment --- packages/router/src/lib/routes.spec.ts | 17 ++++++++++------- packages/router/src/lib/routes.ts | 16 ++++++++-------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/packages/router/src/lib/routes.spec.ts b/packages/router/src/lib/routes.spec.ts index 87a082849..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'; @@ -608,19 +609,21 @@ describe('routes', () => { 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 = { - '/app/routes/index.ts': () => - Promise.resolve({} as unknown as RouteExport), + [fileName]: () => Promise.resolve({} as unknown as RouteExport), }; const routes = createRoutes(files); const route = routes[0]; - try { - await route.loadChildren?.(); - } catch (error) { - expect(error?.toString()).toMatch('Missing default export'); - } + 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 f47c6fd48..b110475d0 100644 --- a/packages/router/src/lib/routes.ts +++ b/packages/router/src/lib/routes.ts @@ -183,14 +183,14 @@ function toRoutes(rawRoutes: RawRoute[], files: Files): Route[] { path: rawRoute.segment, loadChildren: () => module!().then((m) => { - const hasModuleDefault = !!m.default; - if ( - process.env['NODE_ENV'] !== 'production' && - !hasModuleDefault - ) { - throw new Error( - `[Analog] Missing default export at ${rawRoute.filename}` - ); + if (!import.meta.env.PROD) { + const hasModuleDefault = !!m.default; + + if (!hasModuleDefault) { + console.warn( + `[Analog] Missing default export at ${rawRoute.filename}` + ); + } } return [