Sub topic 15
Migrating an Existing Codebase Step by Step
This chapter assumes you know React hooks, TypeScript imports and exports, and the feature-module layout from earlier chapters. It also assumes you have a working app with a flat structure, such as top-level components/, hooks/, and utils/ folders. The goal is to move that app to feature modules without freezing development.
Why not rewrite everything at once
A big-bang move touches hundreds of files. It creates merge conflicts with every open branch, and it hides real regressions inside a huge diff. Migrate in small steps that each leave the app working and shippable.
Step 1: Add tooling first
Add the @/ path alias to tsconfig.json and to the bundler. Add the restricted-imports rule, but start it as a warn instead of error. Existing violations then show up without breaking the build.
Step 2: Pick one feature
Choose a feature with clear edges and few dependents, such as cart or settings. Avoid starting with the most tangled area. An early win teaches the team the process.
Step 3: Move the files
Create src/features/cart/ and move the related components, hooks, tests, and API code into it. Use git mv so history follows each file. Then add an index.ts that exports 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";Step 4: Update the imports
Find every import of the moved files and point it at the public API. Let TypeScript find the broken ones. Run tsc --noEmit after each move.
// before
import { CartList } from "../../components/CartList";
// after
import { CartList } from "@/features/cart";If outside code needs something you did not export, decide whether it belongs in the public API or whether the caller is reaching too far. Do not export everything just to make errors go away.
Step 5: Lock in the boundary
Once a feature has no violations, change its lint rule to error. A flat warn rule applies to everything, so scope the error level to finished features with a separate config block.
// eslint.config.js (excerpt)
export default [
{
files: ["src/**/*.{ts,tsx}"],
rules: {
"no-restricted-imports": [
"warn",
{ patterns: [{ group: ["@/features/*/*"], message: "Use the feature's index file." }] },
],
},
},
{
files: ["src/**/*.{ts,tsx}"],
ignores: ["src/features/legacy-*/**"],
rules: {
"no-restricted-imports": [
"error",
{ patterns: [{ group: ["@/features/cart/*", "@/features/orders/*"], message: "Use the feature's index file." }] },
],
},
},
];Later blocks override earlier ones, so cart and orders are now strict while the rest only warn. Add each migrated feature to the strict list.
Common problems
- Circular imports. Moving files can expose cycles between features. Break them by moving the shared piece to
shared/or by passing data through props. - Shared code hiding in features. If three features use one helper, move it to
shared/lib/. - Mixed commits. Keep moves separate from behavior changes. A pure move is easy to review.
- Long-lived branches. Merge or rebase before moving a feature, and announce which folders are in motion.
Exercise
Why does the chapter start the restricted-imports rule as a warning, and when should it become an error?
Show answer
A warning reports existing violations without breaking the build, so the team can migrate gradually. A feature's boundary should become an error once that feature has no violations, and the global rule can become an error when the warning count reaches zero.
Exercise
You moved the orders feature and a helper from it is used by cart and settings. Where should the helper go?
Show answer
Move it to shared/lib/ (or a shared UI folder if it is a component), because several features depend on it. Features should not import each other's internals.