Skip to content

compiler-cli: ngtools should accept useFactory (lazy routes) #15305

Closed

Description

@fulls1z3

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.0.0-rc.5
@angular/core: 4.0.0-rc.5
@angular/forms: 4.0.0-rc.5
@angular/http: 4.0.0-rc.5
@angular/platform-browser: 4.0.0-rc.5
@angular/platform-browser-dynamic: 4.0.0-rc.5
@angular/router: 4.0.0-rc.5
@angular/compiler-cli: 4.0.0-rc.5

Repro steps.

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.

First of all, I'd like to let you know that there were already two discussions related to this topic (#4686 and #4541), however #4686 was closed by @filipesilva due to the similarities with #4541.

Actually, #4541 points to slightly different topic (it's a feature request), which could potentially cause ignoring the issue that I'm trying to report.

In 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.

I'll not copy the whole code here, as solution files are located on angular-cli branch of @nglibs/example-app. The solution depends on @angular v2.4.7 & @angular/cli v1.0.0-beta.30 but the issue occurs on @angular v4.0.0-rc.5 &@angular/cli v1.0.0-rc.2 as well.

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):

about.module.ts

import { I18NRouterModule } from '@nglibs/i18n-router';

@NgModule({
  imports: [
    CommonModule,
    // RouterModule.forChild(routes)
    I18NRouterModule.forChild(routes, 'about')
  ],
  declarations: [
    AboutComponent,
    AboutUsComponent,
    AboutBananaComponent,
    AboutApplePearComponent
  ]
})

The log given by the failure.

Error: Cannot read property 'loadChildren' of undefined

Mention any other details that might be useful.

First 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.

The issue occurs only with @ngtools/webpack.

The solution files to repro this issue are located on angular-cli branch of @nglibs/example-app.

After some research, and in-depth digging of @ngtools/webpack source code, I've found the lines of code which is responsible for the issue.

Passing a factory method (useFactory) for the DI token ROUTES causes the discovery of lazy-routes to fail:

i18n-router.ts

static forChild(routes: Routes, moduleKey: string): ModuleWithProviders {
  return {
    ngModule: I18NRouterModule,
    providers: [
      provideRoutes([]),

      ...

      {
          provide: ROUTES,
          useFactory: (provideChildRoutes),
          deps: [I18NRouterService, RAW_ROUTES, MODULE_KEY],
          multi: true
        },
        {
          provide: ANALYZE_FOR_ENTRY_COMPONENTS,
          useValue: routes,
          multi: true
        }
      ]
  }
}

However, if I provide child routes unprocessed (simply pass useValue: routes instead of useFactory: ..., deps: ...), then the @ngtools/webpack can find the lazy routes.

Analyzing the issue line-by-line, I came across with the @ngtools/webpack, the listLazyRoutesOfModule function.

Desired functionality.

The listLazyRoutesOfModule function of @ngtools/webpack should be able to discover lazy routes supplied by a factory method (useFactory) for the DI token ROUTES.

Activity

  1. sod commented on Mar 20, 2017

    @sod
    Contributor

    I also once tried to get that into angular #10983.

    I heard the angular team has internationalization in focus for angular 5.0.0. That might bring solutions.

  2. fulls1z3 commented on Mar 20, 2017

    @fulls1z3
    Author

    Hi @sod, I've read the details of your PR and with some luck debugging the ngtools_impl.ts, I've found out the solution.

    While the angular team has their own i18n focus, I'll publish a PR and hope it will be merged during the next release.

  3. added this to the milestone on Mar 20, 2017
  4. changed the title [-]compiler-cli: @ngtools/webpack should accept `useFactory` for the DI token `ROUTES`[/-] [+]compiler-cli: ngtools should accept `useFactory` (lazy routes)[/+] on Mar 21, 2017
  5. ocombe commented on Mar 27, 2017

    @ocombe
    Contributor

    Actually I think that adding the locale to the routes should probably be something that angular offers for i18n. What do you think @vicb ?

  6. removed this from the milestone on Oct 5, 2017
  7. added this to the Backlog milestone on Jan 23, 2018
  8. added
    area: compilerIssues related to `ngc`, Angular's template compiler
    and removed
    area: coreIssues related to the framework runtime
    on May 22, 2020
  9. 1 remaining item

  10. modified the milestones: Backlog, needsTriage on May 28, 2020
  11. alxhub commented on May 28, 2020

    @alxhub
    Member

    useFactory functions are not statically analyzable, so the original solution as proposed does not work. But there's a larger question here of how router and i18n collaborate on locales in the route itself.

  12. modified the milestones: needsTriage, Backlog on Jul 21, 2020
  13. added
    P3An issue that is relevant to core functions, but does not impede progress. Important, but not urgent
    and removed on Oct 1, 2020
  14. petebacondarwin commented on Oct 23, 2020

    @petebacondarwin
    Contributor

    @fulls1z3 - I am sorry that this issue has been left so long. I feel that it is possibly outdated and no longer relevant. Since this issue was created, the way that lazy routes are loaded has changed, ivy has been released, i18n has been implemented and updated. Can you clarify if this is still an issue for you and maybe update it to be relevant to the current version of Angular?

  15. added
    needs: clarificationThis issue needs additional clarification from the reporter before the team can investigate.
    on Oct 23, 2020
  16. petebacondarwin commented on Nov 23, 2020

    @petebacondarwin
    Contributor

    Closing due to lack of response.

  17. angular-automatic-lock-bot commented on Dec 24, 2020

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  18. locked and limited conversation to collaborators on Dec 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3An issue that is relevant to core functions, but does not impede progress. Important, but not urgentarea: compilerIssues related to `ngc`, Angular's template compilerarea: i18nIssues related to localization and internationalizationarea: routerfreq1: lowneeds: clarificationThis issue needs additional clarification from the reporter before the team can investigate.type: use-case

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions