Sub topic 15
Migrating an existing app one feature at a time
This chapter assumes you have read the earlier chapters on feature folders, lazy routes, barrels, and lint rules. It also assumes you have an existing Angular app grouped by file type, with folders such as components/, services/, and models/.
Why not rewrite everything
A big-bang move touches every import in the app. The pull request becomes unreviewable and conflicts with every other branch. It also gives you no working app to test until the very end.
A better approach is to move one feature at a time. After each move the app builds, tests pass, and you can ship.
Pick the first feature
Choose a feature with these traits:
- It has a clear business meaning, such as orders or invoices.
- It has few inbound dependencies from other features.
- It already maps to one or two routes.
Avoid starting with the feature that everything else imports. Leaf features are cheaper to move, and they teach you the pattern.
Steps for one feature
- Create
src/app/orders/withfeature/,data-access/, andui/subfolders. - Move the components, services, and models that only orders uses. Use your editor's move refactoring, or
git mvfollowed by fixing imports. This keeps file history. - Convert the module to standalone components if it is not already, and create
orders.routes.ts. - Replace the eager route with
loadChildren. - Create
index.tsexporting only what other code needs. - Update outside imports to use the alias, for example
@app/orders.
Before the move, the app route is eager:
import { Routes } from '@angular/router';
import { OrderListComponent } from './components/order-list.component';
export const routes: Routes = [
{ path: 'orders', component: OrderListComponent },
];After the move, it only points at the feature:
import { Routes } from '@angular/router';
export const routes: Routes = [
{
path: 'orders',
loadChildren: () =>
import('./orders/orders.routes').then((m) => m.ORDERS_ROUTES),
},
];Handling shared code you find on the way
You will find files in services/ that three features use. Do not move them into the first feature. Sort each one:
| Situation | Where it goes |
|---|---|
| Used by one feature only | Into that feature's `data-access/` or `ui/` |
| Used by several features, no business meaning | `shared/` |
| App-wide singleton such as auth or an HTTP interceptor | `core/` |
| Used by several features, with business meaning | Leave it in place for now, and decide later |
The last row matters. If you cannot classify a file, leave it and record it in a list. Forcing a bad decision creates a second migration later.
Keep the old paths working temporarily
If many files still import a moved file by its old path, leave a re-export at the old location. This lets you migrate imports in separate, small commits.
// src/app/services/order.service.ts (temporary shim)
export { OrdersStore as OrderService } from '../orders/data-access/orders.store';Delete each shim once nothing imports it. A shim left in place becomes a hidden dependency that bypasses the barrel.
Lock in each gain
After each feature, add its boundary to your lint rules. Otherwise someone will import orders/data-access/orders.store directly from another feature a week later. Tag the new code and extend the depConstraints so that checking happens in CI, not in review.
Track progress with a simple count, such as the number of files left in components/ and services/. When both folders are empty, delete them.
Exercise
Your app has an invoices feature that imports CustomerService from the old services/ folder, and CustomerService is also used by orders and reports. You are migrating invoices first. What do you do with CustomerService, and why?
Show answer
Leave CustomerService where it is, or add it to a list of undecided shared code, and keep importing it from the old path (or a shim). It has business meaning and is used by several features, so moving it into invoices would create a dependency from orders and reports on invoices. Decide its permanent home (for example a customers feature with a public API) in a separate step.