I recently added some functionality to an application that is set up to use the old browser (Webpack-based) builder that dynamically generates routes via a factory function.
The factory function is as follows, and is used in an NgModule:
@NgModule({
...(truncating for brevity)
providers: [
{
provide: ROUTES,
useFactory: docsRoutesFactory(Library.VizComponents),
deps: [DocsConfigService],
multi: true,
},
],
})
// separate file
export function docsRoutesFactory(lib: Library) {
...
const routes: Routes = [
{
path: '',
children: [],
},
];
config.items.forEach((item: string) => {
const componentName = ...
routes[0].children.push({
path: item,
loadComponent: () =>
import(
`../../${lib}/${item}-docs/${item}-docs.component`
).then((m) => m[componentName]),
});
});
return routes;
};
}
I then tried to replicate this in a newer application that came with the application (Vite-based) builder enabled/set up in angular.json, and it would not work at run time. I tried switching the older application over to the application builder and got the same result. I get the following warning at compilation:
The glob pattern import("../../**/*/**/*-docs/**/*-docs.component") did not match any files [empty-glob]
projects/demo-app/src/app/manual-documentation/core/routing/manual-docs-routing-factory.ts:17:44:
17 │ ...=> import(`../../${lib}/${item}-documentation/${item}-docs...
and at runtime, I get the following error in the console if I try to activate one of the routes:
core.mjs:7185 ERROR Error: Module not found in bundle: ../../viz-components/bars-docs/bars-docs.component
at chunk-QKQ47GQZ.js:4:9
at Object.loadComponent (manual-docs-routing-factory.ts:24:13)
Any ideas how to do this kind of dynamic routing with the new builder?
Thanks!