Sub topic 14
Scaling up with workspaces and libraries
This chapter assumes you know the feature folder layout, barrels, path aliases and the feature, ui, data-access and util layers from earlier chapters. It also assumes you can run Angular CLI commands. Here you move the same boundaries out of folders and into a workspace of libraries.
Why move from folders to libraries
Folders and barrels rely on discipline. Nothing stops a developer from importing a private file with a long relative path. A library makes the boundary physical. Other code can only reach what the library exports, and tooling can see which libraries depend on which.
A workspace is a single repository that holds one or more applications and many libraries. Nx is a common tool for this. The Angular CLI also supports workspaces with libraries through ng generate library, but it has no built-in rule for restricting imports between libraries. This chapter uses Nx for that reason.
From feature folder to library
Each feature folder becomes one library per layer. The orders feature from earlier chapters could split like this:
| Library | Tag | Contents |
|---|---|---|
| `orders/feature` | `type:feature` | Routes, smart components |
| `orders/ui` | `type:ui` | Presentational components |
| `orders/data-access` | `type:data-access` | `OrdersStore`, `Order` model |
| `shared/util` | `type:util` | Pure helper functions |
The library's index.ts is the same barrel you already wrote. The path alias in tsconfig.base.json points to it, and Nx generates that alias when you create the library.
// libs/orders/feature/src/index.ts
export { ORDERS_ROUTES } from './lib/orders.routes';The app then lazy-loads the feature through the alias instead of a relative path:
import { Routes } from '@angular/router';
export const routes: Routes = [
{
path: 'orders',
loadChildren: () =>
import('@shop/orders/feature').then((m) => m.ORDERS_ROUTES),
},
];Lazy loading still works. The bundler splits on the dynamic import(), and the library code lands in its own chunk.
Tags and enforced boundaries
Tags in each library's project.json describe its layer. The lint rule from the previous chapter reads them.
{
"name": "orders-ui",
"tags": ["type:ui", "scope:orders"]
}A second tag dimension, scope, stops one feature from reaching into another. Add constraints that combine both dimensions:
{
"@nx/enforce-module-boundaries": [
"error",
{
"depConstraints": [
{
"sourceTag": "scope:orders",
"onlyDependOnLibsWithTags": ["scope:orders", "scope:shared"]
},
{
"sourceTag": "scope:customers",
"onlyDependOnLibsWithTags": ["scope:customers", "scope:shared"]
}
]
}
]
}Now orders/ui cannot import from customers/data-access, and the lint step fails if someone tries.
Common errors
- Circular dependencies. If
orders/featureimportscustomers/featureand the reverse, the linter reports a cycle. Move the shared piece into asharedlibrary, or use the communication patterns from the earlier chapter. - Too many tiny libraries. Every library adds configuration and a path alias. Split by layer only when a feature is large enough, or when you need to restrict who can import it.
- A giant
sharedlibrary. If everything unrelated ends up there, every change affects every feature. Keep shared libraries small and split them by purpose. - Deep imports. Importing
@shop/orders/feature/src/lib/internalbypasses the barrel. Nx flags this, but only when the rule is enabled.
Build speed
Because Nx knows the dependency graph, nx affected -t test runs tests only for libraries that a change touches, directly or through dependencies. On a large repository this is the main practical reason to adopt a workspace, beyond enforcement.
Exercise
A developer on the customers team imports OrdersStore directly from libs/orders/data-access/src/lib/orders.store. Which two mechanisms in this chapter should stop or catch that import, and what should the developer do instead?
Show answer
The barrel index.ts should be the only entry point, and the @nx/enforce-module-boundaries rule flags deep imports and imports across scope tags (scope:customers may only depend on scope:customers and scope:shared). Instead, the developer should avoid depending on the orders feature directly, either by moving the shared piece into a shared library or by using a communication pattern such as a shared service interface or events.