Skip to content

[compiler-cli] Change references from NgModule to NgModuleFactory at build-time #14005

Closed

Description

@SamVerschueren

I'm submitting a ... (check one with "x")

[ ] bug report => search github for a similar issue or PR before submitting
[x] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior
From the discussion #11075 and PR #13909 (comment).

So let me wrap up these discussions. It's possible to encapsulate routes in a feature module like this.

import { Routes, RouterModule, PreloadAllModules } from '@angular/router';
import { HomeComponent } from './home/home.component';
import { ProfileModule } from './profile/profile.module';

export function loadProfileModule() {
	return ProfileModule;
}

export const routes: Routes = [
	{ path: '', component: HomeComponent, pathMatch: 'full' },
	{ path: 'profile', loadChildren: loadProfileModule}
];

export const routing = RouterModule.forRoot(routes);

The ProfileModule on its turn has child routes as well. This isn't meant for lazy loading, it's just a way of letting the profile module decide how the routing internally is done.

However, currently this feature is not usable with AoT. The reason is pointed out by @DzmitryShylovich and is that after AoT, we should return the module factory instead of the module like this.

import { Routes, RouterModule, PreloadAllModules } from '@angular/router';
import { HomeComponent } from './home/home.component';
import { ProfileModuleNgFactory } from '../compiled/src/app/profile.module.ngfactory';

export function loadProfileModule() {
	return ProfileModuleNgFactory;
}

export const routes: Routes = [
	{ path: '', component: HomeComponent, pathMatch: 'full' },
	{ path: 'profile', loadChildren: loadProfileModule}
];

export const routing = RouterModule.forRoot(routes);

Currently, this requires manual steps or another build tool to change the references from NgModule to NgModuleFactory.

Expected behavior
I guess this is a perfect fit for the compiler-cli to do instead of a manual process or another gulp/npm task.

Minimal reproduction of the problem with instructions
I created a quick example here.

  1. git clone https://github.com/SamVerschueren/ng2-hello-world-routing
  2. cd ng2-hello-world-routing
  3. git checkout load-children
  4. npm install

If you run npm start, you will notice that you can navigate from Home to Contact and back while it uses the approach mentioned in the initial post.

Now if you build it with npm run build, it will output a production bundle in the builds directory. So navigate to that directory and start a local server (http-server, lite-server, ...). Open the url and open devtools. If you click on Contact, it will fail to load and shows an error about r.create not being a function.

What is the motivation / use case for changing the behavior?
If this would be possible, it would make life of developers who are targetting for production with AoT a lot easier and would actually make this feature usuable. Currently it doesn't feel usuable because of the manual steps that has to be performed in order to use it. So either you don't use it when compiling with AoT, or you don't compile with AoT.

Please tell us about your environment:
macOS, VS Code, npm/yarn, http-server

  • Angular version: 2.4.3

  • Browser: all

  • Language: TypeScript

  • Node (for AoT issues): node --version = v6.9.1

Activity

  1. DzmitryShylovich commented on Jan 24, 2017

    @DzmitryShylovich
    Contributor

    #13909 (comment)

    The reason is that we never change user code. We only generate new code. If we change the user code it would break dependency tracking tools such as build systems, since many of them do not allow to have two copies of a source file or rewriting the source file. So if we did that we would limit the kinds of build tools which could be used.

  2. DzmitryShylovich commented on Jan 25, 2017

    @DzmitryShylovich
    Contributor

    @SamVerschueren I think it can be closed as it's not actionable ☝️

  3. SamVerschueren commented on Jan 25, 2017

    @SamVerschueren
    ContributorAuthor

    There's somewhat a hacky workaround for this posted in the angular-cli repository. In case people are linked to this topic: angular/angular-cli#4192 (comment).

    Thanks for following up on this @DzmitryShylovich !

  4. 1456118242 commented on Mar 26, 2018

    @1456118242

    @DzmitryShylovich Your head is very beautiful. Is it your picture?

  5. angular-automatic-lock-bot commented on Sep 13, 2019

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

  6. locked and limited conversation to collaborators on Sep 13, 2019
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions