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

五迷三道 提交于 2020-06-28 08:30:23

问题


I'm wondering what are the benefits of working with libraries rathen than modules in Angular, as nx.dev recommands 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 recommands 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

回答1:


NX recommand 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



来源:https://stackoverflow.com/questions/60186268/benefits-of-angular-libraries-vs-angular-modules-in-a-monorepo-nx-architecture

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!