|
| 1 | +# Next.js App Router Integration - Technical Documentation |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This document describes the Next.js App Router support feature implemented in Shadiff v1.2.0, which automatically detects Next.js projects and intelligently handles app directory files to prevent overwriting actual application code. |
| 6 | + |
| 7 | +## Architecture |
| 8 | + |
| 9 | +### NextJsDetector Class |
| 10 | + |
| 11 | +**Location**: `src/utils/nextjs-detector.ts` |
| 12 | + |
| 13 | +The `NextJsDetector` class provides static methods for: |
| 14 | + |
| 15 | +- **Project Detection**: Identifies Next.js projects by checking for configuration files |
| 16 | +- **App Directory Detection**: Determines if files are within app directories |
| 17 | +- **Path Transformation**: Converts app directory paths to examples/ targets |
| 18 | + |
| 19 | +### Key Methods |
| 20 | + |
| 21 | +```typescript |
| 22 | +static isNextJsProject(rootDir: string): boolean |
| 23 | +// Checks for next.config.js, next.config.mjs, or next.config.ts |
| 24 | + |
| 25 | +static isInAppDirectory(filePath: string, rootDir: string): boolean |
| 26 | +// Detects files in app/ or src/app/ directories |
| 27 | + |
| 28 | +static getNextJsTargetPath(filePath: string, rootDir: string): string |
| 29 | +// Transforms app paths to examples/ subdirectories |
| 30 | +``` |
| 31 | + |
| 32 | +## Integration Points |
| 33 | + |
| 34 | +### 1. Registry Generator Enhancement |
| 35 | + |
| 36 | +**File**: `src/core/registry-generator.ts` |
| 37 | + |
| 38 | +- Added `isNextJsProject` property to track detection state |
| 39 | +- Enhanced constructor to perform Next.js detection on initialization |
| 40 | +- Modified file processing loop to apply special targeting for app directory files |
| 41 | +- Added informative console logging for transparency |
| 42 | + |
| 43 | +### 2. Module Export Structure |
| 44 | + |
| 45 | +**File**: `src/utils/index.ts` |
| 46 | + |
| 47 | +Note: `NextJsDetector` uses direct import rather than export through the index file to avoid module resolution issues during development. |
| 48 | + |
| 49 | +## Detection Logic |
| 50 | + |
| 51 | +### Config File Detection |
| 52 | + |
| 53 | +The system checks for these Next.js configuration files: |
| 54 | + |
| 55 | +- `next.config.js` |
| 56 | +- `next.config.mjs` |
| 57 | +- `next.config.ts` |
| 58 | + |
| 59 | +### App Directory Patterns |
| 60 | + |
| 61 | +Files are considered app directory files if they match: |
| 62 | + |
| 63 | +- `app/**/*` |
| 64 | +- `src/app/**/*` |
| 65 | + |
| 66 | +## Target Path Transformation |
| 67 | + |
| 68 | +### Input → Output Examples |
| 69 | + |
| 70 | +| Original Path | Target Path | |
| 71 | +|---------------|-------------| |
| 72 | +| `app/page.tsx` | `examples/app/page.tsx` | |
| 73 | +| `src/app/layout.tsx` | `examples/src/app/layout.tsx` | |
| 74 | +| `app/dashboard/page.tsx` | `examples/app/dashboard/page.tsx` | |
| 75 | +| `src/app/about/page.tsx` | `examples/src/app/about/page.tsx` | |
| 76 | + |
| 77 | +### Non-App Files (No Change) |
| 78 | + |
| 79 | +| File Type | Path | Behavior | |
| 80 | +|-----------|------|----------| |
| 81 | +| Components | `src/components/button.tsx` | Normal processing | |
| 82 | +| Utils | `src/lib/utils.ts` | Normal processing | |
| 83 | +| Pages Router | `pages/index.tsx` | Normal processing | |
| 84 | + |
| 85 | +## Console Output |
| 86 | + |
| 87 | +When Next.js detection occurs, users see: |
| 88 | + |
| 89 | +```bash |
| 90 | +🔥 Next.js project detected! App directory files will be targeted to examples/ |
| 91 | +📂 Next.js app file detected: src/app/page.tsx -> examples/src/app/page.tsx |
| 92 | +``` |
| 93 | + |
| 94 | +## Benefits |
| 95 | + |
| 96 | +1. **Safety**: Prevents overwriting actual Next.js app code |
| 97 | +2. **Transparency**: Clear console messages about what's happening |
| 98 | +3. **Automation**: Zero configuration required |
| 99 | +4. **Flexibility**: Supports both `app/` and `src/app/` structures |
| 100 | +5. **Preservation**: App files become reusable examples |
| 101 | + |
| 102 | +## Testing |
| 103 | + |
| 104 | +### Unit Tests |
| 105 | + |
| 106 | +**File**: `test/nextjs-detector.test.js` |
| 107 | + |
| 108 | +Comprehensive test suite covering: |
| 109 | + |
| 110 | +- ✅ Next.js project detection |
| 111 | +- ✅ App directory file identification |
| 112 | +- ✅ Path transformation accuracy |
| 113 | +- ✅ Non-Next.js project handling |
| 114 | + |
| 115 | +### Integration Tests |
| 116 | + |
| 117 | +- ✅ Full registry generation with Next.js detection |
| 118 | +- ✅ Console output verification |
| 119 | +- ✅ Registry structure validation |
| 120 | +- ✅ Example script functionality |
| 121 | + |
| 122 | +## Version History |
| 123 | + |
| 124 | +- **v1.2.0**: Initial Next.js App Router support implementation |
| 125 | +- Added automatic detection and intelligent targeting |
| 126 | +- Comprehensive test coverage and documentation |
| 127 | + |
| 128 | +## Future Enhancements |
| 129 | + |
| 130 | +Potential improvements for future versions: |
| 131 | + |
| 132 | +- Configurable target directory (instead of hardcoded `examples/`) |
| 133 | +- Support for additional Next.js project structures |
| 134 | +- Custom exclusion patterns for specific app directory files |
| 135 | +- Integration with Next.js metadata and configuration parsing |
0 commit comments