Common Starting Points
Most projects start with one of these anti-patterns:- Everything in components/ - All components in a single flat directory
- Technical grouping - Organized by type (components/, containers/, views/)
- Module-based - Using framework modules (NgModules, Pages Router)
- Random organization - No clear pattern at all
The Refactoring Process
Step 1: Analyze Current Structure
Document what you have:Step 2: Identify Features
Group components by business functionality:| Component | Feature | Notes |
|---|---|---|
| ProductCard | Shop | Shows product info |
| ProductList | Shop | Lists products |
| ProductFilter | Shop | Filters products |
| CartItem | Cart | Shows cart item |
| CartSummary | Cart | Cart total |
| LoginForm | Auth | Login page |
| RegisterForm | Auth | Register page |
| SocialLogin | Auth | Used by login & register |
| Dashboard | Dashboard | Main dashboard |
| DashboardStats | Dashboard | Dashboard metrics |
| UserTable | Users | User management |
| UserForm | Users | Edit users |
| Button | UI | Used everywhere |
| Card | UI | Used everywhere |
| Header | Layout | Used everywhere |
| Footer | Layout | Used everywhere |
Step 3: Analyze Component Usage
For each component, count where it’s used:Step 4: Create Target Structure
Based on analysis, design the new structure:- Next.js
- Angular
Step 5: Execute Migration
Migrate in phases to minimize disruption:Phase 1: Move Shared Components
Start with clearly shared components (used by 2+ features):Phase 2: Create Feature Directories
Phase 3: Move Feature-Specific Components
Migrate one feature at a time:Phase 4: Update Path Aliases
Add or updatetsconfig.json:
Phase 5: Clean Up
Step 6: Validate
Run checks to ensure the refactoring is correct:Before & After Comparison
Before: The “Everything in Components” Anti-pattern
- No clear feature boundaries
- Can’t tell what the app does
- Hard to find related code
- Merge conflicts common
- Can’t delete features easily
After: Scope Rule Architecture
- Clear feature boundaries
- Structure “screams” functionality
- Related code co-located
- Easy to find and modify
- Can delete features by removing directory
- Shared code clearly marked
Migration Checklist
Refactoring Checklist
- Analyze current structure and document it
- Identify business features
- Count component usage across features
- Design target structure
- Create new directory structure
- Move shared components first
- Update shared component imports
- Move feature components one feature at a time
- Update feature component imports
- Configure path aliases
- Run build to check for errors
- Run tests
- Remove old directories
- Update documentation
- Commit changes
Common Pitfalls
Pitfall 1: Moving Too Fast
Wrong: Move everything at once, break the build, spend days fixing imports. Right: Migrate one feature at a time, validate each step.Pitfall 2: Over-Sharing
Wrong: Move components to shared “just in case” they’ll be reused. Right: Keep local until 2nd feature actually needs it, then extract.Pitfall 3: Ignoring Tests
Wrong: Update component paths but forget to update test imports. Right: Update tests alongside components to keep them passing.Pitfall 4: Breaking Running Development
Wrong: Refactor on main branch, break everyone’s development. Right: Create a feature branch, migrate in phases, merge when complete.Automated Migration Tools
Create scripts to automate repetitive tasks:Post-Migration
After completing the migration:- Document the new structure - Update README with architecture decisions
- Train the team - Explain the Scope Rule to all developers
- Set up linting rules - Enforce the structure with ESLint rules
- Monitor for violations - Watch for components in wrong places during code review
