Section 16 of 17

Growing into it and migrating

Contents

Nobody starts with seventy libraries. Fleetline started as one Angular application with a src/app folder; the steps below are the order that worked.

16.1. Migration in eight steps

  1. Create the workspace around the app. nx init in the existing repository. Nothing moves yet.
  2. Draw the domains on paper. Use the business words: what do planners, customers and finance talk about? Expect six to ten domains. Write them into a README.
  3. Turn on the rule as a warning. Add the lint config of section 8.2 with 'warn'. Count the warnings; that is your baseline.
  4. Extract model and util first. They have no dependencies, so moving them is safe, and every later step needs them. Use nx g @nx/workspace:move.
  5. Extract ui per domain. Every component that only has inputs and outputs moves. Components that inject a store stay for now.
  6. Carve data-access per domain. Split the root store by domain; move each store feature, its effects and HTTP into the domain's data-access and register it on the domain's route.
  7. Move pages into feature-* and routes into feature-shell. The app shrinks to its routes and providers.
  8. Switch the rule to 'error' and add CODEOWNERS. From now on, a new crossing fails CI.

Steps 4 to 7 run domain by domain, and the application keeps shipping between them. In Fleetline the whole migration took four months next to feature work, one domain at a time, starting with the smallest one (notifications) to learn the moves.

16.2. What usually goes wrong

  • A domain called common or core. It becomes the new shared/. Every concept has a business owner; find it.
  • Domains cut by page instead of by business language. "Dashboard" is not a domain; it is a feature that reads from several domains through their APIs.
  • API libraries that re-export everything. Then the boundary is only cosmetic. Export what another team actually uses, and nothing else.
  • Disabled lint lines. One eslint-disable for the boundary rule becomes forty within a year. Exceptions go into the config, with a reason in the pull request.

16.3. When to split a library

  • A data-access with more than three or four store features, or owned by two teams: split by sub-domain.
  • A ui library imported by only one feature: merge it back into that feature or keep it, but do not move it to shared.
  • A feature with several unrelated routes: one library per use case.
  • A domain whose libraries are mostly owned by another team: the domain is cut wrong, or the team boundaries are.

16.4. When this is too much

For one application, one team and one or two domains, the full structure costs more than it saves. Start with folders inside one project instead (src/domains/<domain>/feature-…, ui-…, data-access, model) and enforce them with Sheriff (section 8.4). Moving those folders into Nx libraries later is a mechanical step, because the dependency rules are already true.