--- name: feature-lib description: Create a complete feature library in libs/ — generator command, folder layout, tags, routes, store and barrel exports. Use when adding a new feature to the workspace, or when unsure where a new page, service or component belongs. libs/profile is the async reference, libs/settings the synchronous one. --- # Skill: Creating a Feature Library Use this guide every time you need to create a new feature library (e.g., `libs/orders`, `libs/settings`). The reference implementation is `libs/profile`. Follow it exactly. --- ## 1. Generate the library Use the workspace generator — it produces the complete shape described in this skill in one step: ```sh npx nx g @app/workspace-plugin:feature-lib # local state (reference: libs/settings) npx nx g @app/workspace-plugin:feature-lib --shape=async # HTTP + rxMethod (reference: libs/profile) ``` Besides the files below, it registers the `scope:` tag in `depConstraints` (including the app's allow-list), adds the `@libs/` path alias, creates the Vitest `test` target with the 95/90/90/95 coverage floor, and seeds the page's i18n keys in every bundle. The one step left for you is wiring the route into the app — it prints the exact line. Everything it generates is placeholder content shaped like the reference libs; replace the state, page and translations with the real feature. --- ## 2. Required folder structure ``` libs// └── src/ ├── index.ts ← public API (exports ONLY the routes) └── lib/ ├── +state/ │ ├── .store.ts ← NgRx SignalStore │ └── .store.spec.ts ├── molecules/ ← compound components │ └── / │ ├── .ts │ ├── .html │ └── .scss ├── pages/ ← route-level components │ ├── / │ │ ├── .ts │ │ ├── .html │ │ └── .scss │ └── .routes.ts └── services/ └── / └── .service.ts ``` --- ## 3. index.ts — export only the routes The `index.ts` is the **only public API** of the library. **Never export components, services, or state from here** if this is a lazy-loaded feature. Exporting a component would break lazy loading. ```typescript // libs//src/index.ts export * from './lib/pages/.routes'; ``` See `libs/profile/src/index.ts` — it exports only `profileRoutes`. --- ## 4. Routes file Import the page component **directly** inside the routes file. This import stays inside the library boundary and does not break lazy loading, because the routes file itself is only loaded lazily from outside. The route is also where the feature's state and services are provided, so they are created when the feature is entered and destroyed when the user leaves. ```typescript // libs//src/lib/pages/.routes.ts import { Routes } from '@angular/router'; import { FeatureStore } from '../+state/.store'; import { FeatureService } from '../services//.service'; import { FeaturePage } from './/'; export const Routes: Routes = [ { path: '', providers: [FeatureStore, FeatureService], component: FeaturePage, }, ]; ``` --- ## 5. Feature state — SignalStore Each feature owns its state in a single `signalStore`. See the `ngrx-state` skill for the full pattern, including collections, error handling and testing. ```typescript // libs//src/lib/+state/.store.ts import { inject } from '@angular/core'; import { tapResponse } from '@ngrx/operators'; import { patchState, signalStore, withMethods, withState } from '@ngrx/signals'; import { rxMethod } from '@ngrx/signals/rxjs-interop'; import { pipe, switchMap, tap } from 'rxjs'; import { FeatureService } from '../services//.service'; export const FeatureStore = signalStore( withState({ data: null, loading: false, error: null }), withMethods((store, service = inject(FeatureService)) => ({ fetch: rxMethod( pipe( tap(() => patchState(store, { loading: true, error: null })), switchMap(() => service.fetch().pipe( tapResponse({ next: (data) => patchState(store, { data, loading: false }), error: (error) => patchState(store, { error, loading: false }), }), ), ), ), ), })), ); ``` **Do not** add a slice to the global Redux store — that store exists for router state only. --- ## 6. Page component The page component injects the store and reads its signals directly. ```typescript // libs//src/lib/pages//.ts import { ChangeDetectionStrategy, Component, inject } from '@angular/core'; import { FeatureStore } from '../../+state/.store'; @Component({ selector: 'lib-', imports: [], templateUrl: './.html', styleUrls: ['./.scss'], changeDetection: ChangeDetectionStrategy.OnPush, }) export class FeaturePage { private readonly store = inject(FeatureStore); readonly data = this.store.data; readonly loading = this.store.loading; readonly error = this.store.error; constructor() { this.store.fetch(); } } ``` --- ## 7. Wire up lazy loading in the app In the app's pages routes file, load the feature lazily: ```typescript // apps/demo-app/src/app/pages/pages.routes.ts { path: '', loadChildren: () => import('@libs/').then((m) => m.Routes), } ``` **Do not** import the feature component or module directly here. Only import the routes exported from the library's `index.ts`. --- ## 8. Checklist before finishing - [ ] `index.ts` exports only the routes, not components or services - [ ] Routes are registered as `loadChildren` in the app, not `component` - [ ] State is a `signalStore`, not a slice of the global Redux store - [ ] Store and service are listed in the route's `providers`, not in the component's - [ ] Service is **not** `providedIn: 'root'`, so it stays in the lazy chunk - [ ] The template renders loading, error and success — not just the happy path - [ ] `tsconfig.base.json` has the `@libs/` path alias - [ ] Library is tagged correctly in `project.json`