Title: compiler-cli: ngtools should accept `useFactory` (lazy routes) · Issue #15305 · angular/angular · GitHub
Open Graph Title: compiler-cli: ngtools should accept `useFactory` (lazy routes) · Issue #15305 · angular/angular
X Title: compiler-cli: ngtools should accept `useFactory` (lazy routes) · Issue #15305 · angular/angular
Description: Bug Report or Feature Request (mark with an x) - [X] bug report -> please search issues before submitting - [ ] feature request Versions. @angular/cli: 1.0.0-rc.2 node: 6.9.1 os: win32 x64 @angular/common: 4.0.0-rc.5 @angular/compiler: 4...
Open Graph Description: Bug Report or Feature Request (mark with an x) - [X] bug report -> please search issues before submitting - [ ] feature request Versions. @angular/cli: 1.0.0-rc.2 node: 6.9.1 os: win32 x64 @angular...
X Description: Bug Report or Feature Request (mark with an x) - [X] bug report -> please search issues before submitting - [ ] feature request Versions. @angular/cli: 1.0.0-rc.2 node: 6.9.1 os: win32 x64 @angu...
Opengraph URL: https://github.com/angular/angular/issues/15305
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"compiler-cli: ngtools should accept `useFactory` (lazy routes)","articleBody":"\u003c!--\r\nIF YOU DON'T FILL OUT THE FOLLOWING INFORMATION YOUR ISSUE MIGHT BE CLOSED WITHOUT INVESTIGATING\r\n-->\r\n### Bug Report or Feature Request (mark with an `x`)\r\n```\r\n- [X] bug report -> please search issues before submitting\r\n- [ ] feature request\r\n```\r\n\r\n### Versions.\r\n@angular/cli: 1.0.0-rc.2\r\nnode: 6.9.1\r\nos: win32 x64\r\n@angular/common: 4.0.0-rc.5\r\n@angular/compiler: 4.0.0-rc.5\r\n@angular/core: 4.0.0-rc.5\r\n@angular/forms: 4.0.0-rc.5\r\n@angular/http: 4.0.0-rc.5\r\n@angular/platform-browser: 4.0.0-rc.5\r\n@angular/platform-browser-dynamic: 4.0.0-rc.5\r\n@angular/router: 4.0.0-rc.5\r\n@angular/compiler-cli: 4.0.0-rc.5\r\n\r\n\r\n### Repro steps.\r\n\r\n> I'm reposting this issue to @angular repository, because I've found out that the issue is more related to `compiler-cli` than `angular-cli` itself.\r\n\r\nFirst of all, I'd like to let you know that there were already two discussions related to this topic ([#4686](https://github.com/angular/angular-cli/issues/4686) and [#4541](https://github.com/angular/angular-cli/issues/4541)), however [#4686](https://github.com/angular/angular-cli/issues/4686) was closed by @filipesilva due to the similarities with [#4541](https://github.com/angular/angular-cli/issues/4541).\r\n\r\nActually, [#4541](https://github.com/angular/angular-cli/issues/4541) points to slightly different topic (*it's a feature request*), which could potentially cause **ignoring the issue** that I'm trying to report.\r\n\r\nIn order to reproduce this error, **first** install the **`@nglibs/i18n-router`** package (*which is aimed to be a route internationalization solution for Angular v2.x.x and v4.x.x*), and then set up the module configuration in **app.module.ts**.\r\n\r\nI'll not copy the whole code here, as solution files are located on angular-cli branch of [@nglibs/example-app](https://github.com/nglibs/example-app/tree/angular-cli). The solution depends on **`@angular`** v**2.4.7** & **`@angular/cli`** v**1.0.0-beta.30** but the issue occurs on **`@angular`** v**4.0.0-rc.5** &**`@angular/cli`** v**1.0.0-rc.2** as well.\r\n\r\n**Then**, in a feature module (*here used `AboutModule` as an example*), use the **`@nglibs/i18n-router`** by importing `I18NRouterModule` and replace the `RouterModule.forChild(routes)` import with `I18NRouterModule.forChild(routes, 'about')` - which provides `ROUTES` and `ANALYZE_FOR_ENTRY_COMPONENTS` to Angular the same way that the router module does (see below):\r\n\r\n- https://github.com/angular/angular/blob/master/packages/router/src/router_module.ts#L209\r\n\r\n#### about.module.ts\r\n```\r\nimport { I18NRouterModule } from '@nglibs/i18n-router';\r\n\r\n@NgModule({\r\n imports: [\r\n CommonModule,\r\n // RouterModule.forChild(routes)\r\n I18NRouterModule.forChild(routes, 'about')\r\n ],\r\n declarations: [\r\n AboutComponent,\r\n AboutUsComponent,\r\n AboutBananaComponent,\r\n AboutApplePearComponent\r\n ]\r\n})\r\n```\r\n\r\n### The log given by the failure.\r\n> Error: Cannot read property 'loadChildren' of undefined\r\n\r\n### Mention any other details that might be useful.\r\nFirst of all, this issue does not occur when compiling the app on **JiT**. Also, using `ngc` from the command line, and `ng-router-loader` in webpack configuration results with success on **AoT**.\r\n\r\nThe issue occurs **only** with **`@ngtools/webpack`**.\r\n\r\nThe solution files to repro this issue are located on angular-cli branch of [@nglibs/example-app](https://github.com/nglibs/example-app/tree/angular-cli).\r\n\r\nAfter some research, and in-depth digging of **`@ngtools/webpack`** source code, I've found the lines of code which is responsible for the issue.\r\n\r\nPassing a factory method (`useFactory`) for the DI token `ROUTES` causes the discovery of lazy-routes to fail:\r\n\r\n### [i18n-router.ts](https://github.com/nglibs/i18n-router/blob/master/index.ts#L91)\r\n\r\n```\r\nstatic forChild(routes: Routes, moduleKey: string): ModuleWithProviders {\r\n return {\r\n ngModule: I18NRouterModule,\r\n providers: [\r\n provideRoutes([]),\r\n\r\n ...\r\n\r\n {\r\n provide: ROUTES,\r\n useFactory: (provideChildRoutes),\r\n deps: [I18NRouterService, RAW_ROUTES, MODULE_KEY],\r\n multi: true\r\n },\r\n {\r\n provide: ANALYZE_FOR_ENTRY_COMPONENTS,\r\n useValue: routes,\r\n multi: true\r\n }\r\n ]\r\n }\r\n}\r\n```\r\n\r\nHowever, if I provide child routes unprocessed (*simply pass `useValue: routes` instead of `useFactory: ..., deps: ...`*), then the @ngtools/webpack can find the lazy routes.\r\n\r\nAnalyzing the issue line-by-line, I came across with the **`@ngtools/webpack`**, the `listLazyRoutesOfModule` function.\r\n\r\n- https://github.com/angular/angular/blob/4.0.0-rc.5/packages/compiler-cli/src/ngtools_impl.ts#L57\r\n\r\n### Desired functionality.\r\nThe `listLazyRoutesOfModule` function of **`@ngtools/webpack`** should be able to discover lazy routes supplied by a factory method (`useFactory`) for the DI token `ROUTES`.\r\n\r\n","author":{"url":"https://github.com/fulls1z3","@type":"Person","name":"fulls1z3"},"datePublished":"2017-03-20T14:50:06Z","url":"https://github.com/angular/angular/issues/15305"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:813bf91c-8667-6b5b-a671-84a820759fd7 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B416:882E2:CA23FB:10C1E69:6B00F2B7 |
| html-safe-nonce | febd9292d8ff6bef73fb93e589bf7a809408ed3ff52aa6c4aa6126c93186cefd |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCNDE2Ojg4MkUyOkNBMjNGQjoxMEMxRTY5OjZCMDBGMkI3IiwidmlzaXRvcl9pZCI6IjQxNjMzODM1NDQzODg4NDIxNjciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | f4fc4f6aa4860004bb667c4e9a3694826c1e7611b05ea1b5e19ec4698a6ec298 |
| hovercard-subject-tag | issue:215444514 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/angular/angular/15305/issue_layout |
| twitter:image | https://opengraph.githubassets.com/155667fb14f3d68db86f0596ee373378bbd3b05c4939963f9438bbd761429b92/angular/angular/issues/15305 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/155667fb14f3d68db86f0596ee373378bbd3b05c4939963f9438bbd761429b92/angular/angular/issues/15305 |
| og:image:alt | Bug Report or Feature Request (mark with an x) - [X] bug report -> please search issues before submitting - [ ] feature request Versions. @angular/cli: 1.0.0-rc.2 node: 6.9.1 os: win32 x64 @angular... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | fulls1z3 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4ef498cbabcbe3c50befc7a5b743e7072ee770b4daf0e15e54801180d1fa91fc |
| turbo-cache-control | no-preview |
| go-import | github.com/angular/angular git https://github.com/angular/angular.git |
| octolytics-dimension-user_id | 139426 |
| octolytics-dimension-user_login | angular |
| octolytics-dimension-repository_id | 24195339 |
| octolytics-dimension-repository_nwo | angular/angular |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 24195339 |
| octolytics-dimension-repository_network_root_nwo | angular/angular |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 9983dd5da06943f833c971dd5b6bc988e2858be5 |
| ui-target | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width