10

I am looking for a way to create angular 6 (by Angular 6 CLI may be) library with multiple modules that can be imported each one itself according developer`s needs.

Till now I found only tutorials that describe creating simple libraries with a couple components. And all of them must be imported by the library NgModule. But the goal is to be able import some of the modules to the production app without importing all other stuff (components etc.) from the library module.

2
  • Same problem here! I am able to add peerDependencies, but do not seem to be able to actually use them! stackoverflow.com/questions/51164896/…. Surely such a basic use case should be supported! Commented Jul 4, 2018 at 2:14
  • 2
    @Ross that doesn't have to do with working with multiple modules inside of an Angular 6 library. Commented Sep 10, 2018 at 17:42

3 Answers 3

3

Try ng-packagr. Here is the link with an example to create angular library using ng-packagr and export single module. But you can create as many modules as you want and export them.

The exported modules can be imported independently from the library.

Sign up to request clarification or add additional context in comments.

1 Comment

That's it! Thanks for that! After a bunch more digging I realized that angular 6 comes with ng-packagr in-build so creating libraries or modules in angular projects is now much simpler
0

I found pretty useful package @nrwl. One of its features that after creating angular libraries, when you do any changes in any library, they directly represented in components that use them. In my opinion that package is more comfortable for creating libraries than ng-packagr where you should compile you library after doing any changes in it.

Comments

0

sub-entries are what you're looking for. It adds tree shaking support to your library.

The sub-entries facilitated tree-shaking means that when we import something, eg a MyOrgDropdownModule then we’re only getting implementation of that sub-entry (and sub-entries it explicitly imports, eg it may use MyOrgIconModule ) and nothing else!

More so, this also works wonders in the context of lazy loaded modules (so not just main / eager vs lazy) as the sub-entries enable Angular CLI to do further optimization like extracting a sub-entry into a virtual chunk that will only be loaded for a particular set lazy-loaded features that really use it!

I recommend you read this article. As a summary, you'll isolate your modules in separate folders and you'll put a package.json in each of them.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.