KevinTale
KevinTale

Reputation: 1898

Benefits of Angular libraries vs Angular modules in a monorepo? NX architecture

I'm wondering what are the benefits of working with libraries rather than modules in Angular, as nx.dev recommends for an monorepo architecture.

I understand the benefits for a npm publishable feature like interfaces that another repo will consume, but why would I want to make a library out of a business related feature, like a homepage, for example :

myorg/
├── apps/
│   ├── todos/
│   └── todos-e2e/
├── libs/
      ├── todos/
        ├── home/   <-- why nx recommends making a library here?
          ├── src/
            ├── lib/
              ├── home.component.html/ts/scss
              ├── home.module.ts
├── tools/
├── README.md
├── workspace.json
├── nx.json
├── package.json

rather than

myorg/
├── apps/
│   ├── todos/
         [...]
         ├── home/   <-- just a simple lazy loaded module here
           ├── home.component.html/ts/scss
           ├── home.module.ts
│   └── todos-e2e/
├── libs/
├── tools/
├── README.md
├── workspace.json
├── nx.json
├── package.json
└── tsconfig.json

Upvotes: 2

Views: 3444

Answers (1)

Fateh Mohamed
Fateh Mohamed

Reputation: 21397

NX recommends that you put more than 90% of your code in libs and the the purpose of those libs is not only sharing code between the different applications we can create an lib even if the code is not shared and use app folders only for configurations like routings and environments.

we have to mention the benefits of separating your code into small units (libs) that you can test and build them individually.

so in your case it can be a lib that can be lazy loaded into your app it can manage it's own routes, here is the list of libs types taken from their book

  • Feature libraries: Developers should consider feature libraries as libraries that implement smart UI (with injected services) for specific business use cases or pages in an application.

  • UI libraries: A UI library contains only presentational components.

  • Data-access libraries: A data-access library contains services and utilities for interacting with a back-end system. It also includes all the code related to State management.

  • Utility libraries: A utility library contains common utilities and services used by many libraries and applications.

when you see all those types of libs you understand that the idea is to have the max of code in libs insteand of apps.

i invite you to check this when Victor Savkin talked about libs

https://youtu.be/qYNiOKDno_I?t=6m35s

Upvotes: 9

Related Questions