Sub topic 15
Migrating an Existing Codebase Step by Step
This chapter assumes you have read the earlier chapters on feature modules, public APIs, path aliases, and ESLint boundary rules. It also assumes you can run git mv, edit tsconfig.json, and read a pull request diff. The goal is to move a working flat React project to a feature-based layout without a freeze and without a rewrite.
Why not move everything at once
A single large restructuring pull request looks tidy, but it fails in predictable ways:
- It touches hundreds of files, so reviewers approve it without reading it.
- Every open branch conflicts with it.
- If it breaks something subtle, such as an import order or a circular dependency, you cannot tell which move caused it.
- It cannot ship until all of it is finished.
The safer approach is incremental. The old and new layouts exist side by side, and each pull request moves one feature. This is often called the strangler pattern: the new structure grows around the old one until the old one is empty.
The starting point
A typical flat project groups files by type:
src/
components/
CartList.tsx
CartList.test.tsx
OrderTable.tsx
Button.tsx
hooks/
useCart.ts
useOrders.ts
api/
cart.ts
orders.ts
utils/
formatDate.ts
App.tsxThe target is the layout from earlier chapters: app/, features/, and shared/. You do not need to reach it in one step.
Step 1: Prepare the ground without moving anything
Do the setup work first, in small pull requests that change no feature code.
- Add the
@/*path alias totsconfig.json, and add the matching setting to your bundler and test runner. A TypeScriptpathsentry alone does not change how Vite, webpack, or Jest resolve imports. - Create the empty
src/featuresandsrc/sharedfolders. - Add the ESLint boundary rule in
warnmode, covering no features yet. - Record a baseline: the current number of files in the flat folders and the number of lint warnings. You will use these to track progress.
Adding the alias first matters. Once imports use @/, moving a file no longer breaks relative paths such as ../../hooks/useCart in the files that import it.
Step 2: Choose the first feature carefully
Pick a feature that is small, has clear edges, and is touched often enough that the team benefits soon. Avoid these for the first move:
- A feature that everything else imports. It forces many import edits at once.
- Anything with unclear ownership, such as a "common" component used by three features.
A good first candidate has a page, a few components, one or two hooks, and one API file, with few importers. Cart and orders in the earlier examples fit this.
Step 3: Move files with history intact
Use git mv so Git records the move as a rename. Move the files in one commit with no content changes, then make edits in a second commit. Git detects renames by content similarity, and a combined move and edit can fall below that threshold and lose the file history.
mkdir -p src/features/cart/components src/features/cart/hooks
git mv src/components/CartList.tsx src/features/cart/components/CartList.tsx
git mv src/components/CartList.test.tsx src/features/cart/components/CartList.test.tsx
git mv src/hooks/useCart.ts src/features/cart/hooks/useCart.ts
git mv src/api/cart.ts src/features/cart/api.ts
git commit -m "Move cart files into features/cart (no content changes)"The test file moves with its component. This keeps colocated tests together and keeps the test's relative import of ./CartList working with no edit.
Step 4: Add the public API, then fix importers
Create src/features/cart/index.ts and export only what other code needs:
// src/features/cart/index.ts
export { CartList } from "./components/CartList";
export { useCart } from "./hooks/useCart";
export type { CartItem } from "./types";Then update every file outside the feature that imported from the old paths. Search for them first:
grep -rn "components/CartList\|hooks/useCart\|api/cart" src --include=*.ts --include=*.tsxReplace each with import { CartList, useCart } from "@/features/cart". If the TypeScript compiler reports an error, the export list is missing something. Do not export the missing file's internals by reflex. First ask whether the importer really needs it, or whether it belongs in another feature or in shared.
Inside the feature, imports between its own files stay relative (../api). Only code outside the feature uses the alias and the index.
Step 5: Ratchet the lint rule
Now turn enforcement on for the feature you just migrated, and only for that feature. This is the ratchet: each migrated feature becomes protected, and unmigrated code is not yet held to the rule.
// eslint.config.js
export default [
{
files: ["src/**/*.{ts,tsx}"],
rules: {
"no-restricted-imports": [
"error",
{
patterns: [
{
group: ["@/features/cart/*"],
message: "Import from @/features/cart instead of its internals.",
},
// Add each feature here as it is migrated.
],
},
],
},
},
];When all features are migrated, replace the list with the general pattern @/features/*/*. You can also add the legacy folders, such as @/components/*, to the restricted list once they are empty. That stops anyone from recreating them.
The loop
Because each loop ends in a merge, the application ships normally the whole time.
Edge cases and common errors
Circular imports surface during the move
The flat layout hides dependency cycles, because everything can import everything. Once boundaries exist, a cycle such as cart importing from orders while orders imports from cart becomes visible. Resolve it by moving the shared piece, often a type, into shared. You can also merge the two features if they are really one. Do not fix it by importing from a feature's internals.
Shared code that is not really shared
A component in src/components used by only one feature belongs in that feature. A component used by two or more features, and with no feature-specific logic, belongs in shared/ui. Decide by listing the importers. If a "shared" component imports from a feature, it is not shared. Move it into that feature, or pass the feature-specific data in as props.
The alias works in the editor but not in tests
The editor reads tsconfig.json, but Jest and Vitest need their own resolution settings. If @/features/cart fails in tests but compiles, check the test runner's alias configuration (moduleNameMapper in Jest, resolve.alias in Vitest).
Barrel files and bundle size
An index.ts that re-exports everything can pull unneeded code into a bundle if your bundler cannot tree-shake it. Keep the index limited to the real public surface. For route-level code, import the page component directly with lazy, as in the code-splitting chapter, so the page loads on demand.
Merge conflicts with open branches
Moves with no content change are easy for Git to follow in most merges. Announce each migration pull request, merge it quickly, and ask authors of open branches to merge or rebase after it lands. Moving one feature at a time keeps the conflict window small.
Tracking progress
Use the baseline from Step 1. Useful signals include the number of files left in src/components, src/hooks, and src/api, and the number of features covered by the lint rule. A short table in the project's README keeps the work visible:
| Feature | Moved | Index file | Lint rule |
|---|---|---|---|
| cart | yes | yes | error |
| orders | yes | yes | warn |
| settings | no | no | no |
When the flat folders are empty, delete them and generalize the lint rule. The migration is then complete.
Exercise
In a flat project, the file src/components/Avatar.tsx is imported by the profile page and the comments page. It also imports useCurrentUser from src/hooks/useCurrentUser.ts, which only the profile page and the settings page otherwise use. Where should you move Avatar, and what should you check before deciding?
Show answer
Check its importers and its own imports. Two features use it, so it is not owned by a single feature. If it contains no feature-specific logic, move it to shared/ui. But it imports useCurrentUser, which is user data, not presentation. A shared UI component that depends on feature or app state would make shared depend on a feature, which breaks the layering. Change Avatar to take the user's name and image URL as props, move it to shared/ui, and let each page or feature component call useCurrentUser and pass the values in.