1

I'm developing an angular 2 app with angular-cli

angular 2   - 2.1.1
angular-cli - 1.0.0-beta.18

One functionality of the app is it lists down all the events and clicking on one event displays event details in 3 tabs - Overview, Photos & Tickets

The way I've developed this is I've all the events stored in my db and also the content for Overview, Photos and Tickets tabs are in DB. So this way I can just have one component which renders 3 tabs and populates each tab content fetched from DB. Now the interesting bit is most of the events will have same layout and sections, but for some there can be different layout required. So to address this what I'm doing is along with each of the 2 tabs content (overview, photos, tickets), I also store the name of the component which will render this event.

I somehow managed to get the component from the name of the component using ComponentFactoryResolver and ViewContainerRef and all works fine, but problem arises when I try to make a build for production environment where angular-cli kicks tree-shaking and with that all my components which are not actually referenced/imported in the code but are dynamically created/rendered based on the data retrieved from db are dropped and my app crashes since it cannot find those components.

So my question is/are

  1. Is there a way to exclude certain components from getting removed/dropped during tree shaking? a. If YES, then how?
    b. If NO then what's the alternative?
Community
  • 1
  • 1
Jay
  • 1,539
  • 1
  • 16
  • 27

1 Answers1

0

Add these components to

@NgModule({
  entryComponents: [...],
  ...
})

https://angular.io/docs/ts/latest/api/core/index/NgModule-interface.html#!#entryComponents-anchor

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • That doesn't help. I already have it, without that angular wont be able to find such dynamic component even in dev mode/environment. – Jay Oct 31 '16 at 13:25
  • That should be enough to prevent it from being tree-shaken. If it isn't it's a bug somewhere in the tools. – Günter Zöchbauer Oct 31 '16 at 15:03
  • 1
    That was my understanding as well and may be you are right, its a bug in tooling. Will raise a ticket on github and will see what the cli team has to say – Jay Oct 31 '16 at 15:14
  • 1
    @GünterZöchbauer I would appreciate it if you could have a look at http://stackoverflow.com/questions/42537138/angular2-cli-tree-shaking-removing-dynamicly-created-ngmodule which is like a follow-up of this question. Maybe you can deduce more from the code I provided there. Thanks! – Sebastian Mar 02 '17 at 09:01
  • Sorry, can't help there. – Günter Zöchbauer Mar 02 '17 at 09:03