Skip to content

Initial dynamic import support #9515

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 16, 2018
Merged

Initial dynamic import support #9515

merged 3 commits into from
Feb 16, 2018

Conversation

clydin
Copy link
Member

@clydin clydin commented Feb 5, 2018

This adds the additionalLazyModules option to the AngularCompilerPlugin which allows a developer to add support for additional lazy modules not found via static analysis of the router configuration.

Also adds a lazyModules option to the app section of .angular-cli.json. Paths are relative to the applications root configuration option.

lazyModules: [
  "app/lazy/lazy.module"
]

Example Component:

import { Component, SystemJsNgModuleLoader } from '@angular/core';

// Only use for type information and the module will not be eagerly loaded
import { LazyModule } from './lazy/lazy.module';

@Component({
  selector: 'app-example',
})
export class ExampleComponent {
  constructor(loader: SystemJsNgModuleLoader) {
    // Module will be split at build time and loaded when requested below
    loader.load('app/lazy/lazy.module#LazyModule')
      .then((factory: NgModuleFactory<LazyModule>) => { /* Use factory here */ });
  }
}

The module path passed to the load method should be the full name of the module specified in the application configuration plus the # separator and class name from the file; or nothing additional for the default export. This is the same format that the loadChildren router configuration option uses.

@clydin clydin force-pushed the dynamic-imports branch 2 times, most recently from 2d8eec2 to 2f4c8f5 Compare February 7, 2018 13:35
@Brocco Brocco requested a review from hansl February 12, 2018 21:11
@clydin clydin force-pushed the dynamic-imports branch 3 times, most recently from 2796f86 to db6554e Compare February 12, 2018 22:51
@hansl hansl merged commit 3be17e7 into angular:master Feb 16, 2018
@clydin clydin deleted the dynamic-imports branch February 16, 2018 22:09
@sujithgowda
Copy link

Hello clydin,
This creates factory class chunk for only the module. what if I need a component along with the module?

Regards,
Sujith

@clydin
Copy link
Member Author

clydin commented Mar 20, 2018

You need to use the module factory to create a module reference which in turn can be used to create component factories for the module's defined entry components. Components are not loaded independently.

@sujithgowda
Copy link

yes, we are currently doing it by hacking the loadchildren of route module, which creates a chunk which contains ngfactory of lazy module and all the referenced component's ngfactories.
when I refer the lazy module in lazyroutes array of angular-cli.json file, the chunk created contains only the ngfactory of the lazy module and not the ngfactories of the component referenced inside the lazy module.

@angular-automatic-lock-bot
Copy link

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.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 12, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants