question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AOT broken when using loadChildren and a function : Replace modules import by modules factories ?

See original GitHub issue

When I try to build my app with AOT since I’m using loadChildren with a function it breaks (not at compilation but when running the app in the browser, complaining that r.create is not a function).

According to https://github.com/angular/angular/pull/13909 it seems that instead of having

image

we should rather import the factory (which is not available when coding, only when building) :

image

SamVerschueren asked if it could be replaced at build time (and opened an issue https://github.com/angular/angular/issues/14005 for that).

mhevery explained why it can’t be handled at build time by angular https://github.com/angular/angular/pull/13909#issuecomment-274650555.

I’m wondering if this can be done by angular-cli ? Or as soon as we use loadChildren with a function (don’t want AOT or AOT is broken due to some other bugs for example) we have to manually do that ?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:17
  • Comments:32 (6 by maintainers)

github_iconTop GitHub Comments

35reactions
Meligycommented, Jan 24, 2017

I have been involved in attempts to get something like this working. The core team guys suggest it’s a tough nut to crack. It’s something I imagine the compiler-cli should be doing, changing the output of the compilation, it is compilation (translation) when you think about it.

For now, the workaround is to change the loadChildren assignment to a string (lazy loading), but keep the function around to make the module get included in the same bundle instead of a separate one. Keeping the exported function not just the import makes sure it doesn’t get tree shaked / minified.

Like:

import { ProfileModule } from './profile/profile.module';

// Do not delete. Used to ensure ProfileModule is loaded in the same bundle.
// Referencing the function directly in `loadChildren` breaks AoT compiler.
export function loadProfileModule() {
    return ProfileModule;
}

export const routes: Routes = [
    // ... other routes ...
    { path: 'profile', loadChildren: './profile/profile.module#ProfileModule' }
];

Check the app-routing.module.ts in my routing test project for a working example:

https://github.com/meligy/routing-angular-cli

Specifically:

https://github.com/Meligy/routing-angular-cli/blob/deep-lazy-loading/src/app/app-routing.module.ts#L18

17reactions
wctigercommented, Sep 6, 2018

Function with loadChildren is still not working with AOT as of 9/6/2018, CLI version 6.1.1 - have to switch to lazy loaded path to enable AOT and bypass error of ‘Runtime compiler is not loaded’.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular 8 lazy loading routes from custom library broken at ...
I assume that it's related with loadChildren attribute, when I build the lib, ng-packgr failed with new Angular 8 lazy-loading synthaxe, arrow ...
Read more >
Providing Module Configuration Using forRoot() And Ahead ...
With the Ahead-of-Time (AoT) compiler, however, this approach breaks with the following error: Function calls are not supported in decorators.
Read more >
Automatically upgrade lazy-loaded Angular modules for Ivy!
When Angular encounters a route with a loadChildren property on it, it uses the RouterConfigLoader to try and figure out how to do...
Read more >
Ahead-of-time (AOT) compilation - Angular
This guide explains how to specify metadata and apply available compiler options to compile your applications efficiently using the AOT compiler.
Read more >
Module core - UI-Router
Lazy loading uses a placeholder state which is removed (and replaced) after the module is loaded. The NgModule should include a state with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found