A Vite plugin for automatically generating Vue router configuration based on file structure.
- 🚀 Auto-scan Vue files in specified directory
- 📁 Generate route configuration based on file structure
- ⚙️ Support custom scan and output directories
- 🔍 Support file exclusion configuration
- 🔄 Support hot update in development
- 📦 Support latest versions of Vite and Vue Router
- 🎯 Generate route files named by directory
npm install @lorcan-store/vue-auto-router -D
# or
yarn add @lorcan-store/vue-auto-router -D
# or
pnpm add @lorcan-store/vue-auto-router -D
Configure in vite.config.ts
:
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import VueAutoRouter from '@lorcan-store/vue-auto-router'
export default defineConfig({
plugins: [
vue(),
VueAutoRouter({
// Configuration options
scanDir: 'src/pages', // Directory to scan
outputDir: 'src/router/', // Output directory
exclude: ['layout'], // Directories to exclude
layoutPath: '@/pages/layout/index.vue', // Layout component path
forceOverwrite: ['home'], // Directories to force overwrite
language: 'EN' // Console output language (EN or CN)
})
]
})
Option | Type | Default | Description |
---|---|---|---|
scanDir | string | 'src/pages' | Root directory to scan |
outputDir | string | 'src/router/' | Output directory for route files |
exclude | string[] | [] | Directory names to exclude |
layoutPath | string | '@/pages/layout/index.vue' | Layout component path |
forceOverwrite | string[] | [] | Directory names to force overwrite |
routeTemplate | string | undefined | Custom route template file path |
language | 'EN' | 'CN' | 'EN' | Console output language |
src/
pages/ # scanDir points here
layout/ # Layout component directory (usually excluded)
index.vue # Layout component
home/ # Home module (generates home.ts)
index.vue # Home page
about.vue # About page
user_manager/ # User management module (generates user_manager.ts)
index.vue # List page
form.vue # Form page
-
File Import Rules
- Vue files are converted to dynamic imports
- Component names use PascalCase
- Import paths are converted to @ alias form
- Example:
- house-build.vue -> const HouseBuild
- user_profile.vue -> const UserProfile
- data_analysis-chart.vue -> const DataAnalysisChart
-
Route Path Rules
- Paths use lowercase directory names, keeping original separators
- index.vue maps to empty path
- Other file names are used as sub-paths
- Example:
- /pages/user-center/index.vue -> path: ''
- /pages/user-center/user-info.vue -> path: 'user-info'
- /pages/data_analysis/data_chart.vue -> path: 'data_chart'
-
Route Name Rules
- Uses directory name in PascalCase
- Sub-routes append file name in PascalCase
- Example:
- user-center/index.vue -> name: 'UserCenter'
- user-center/user-info.vue -> name: 'UserCenterUserInfo'
- data_analysis/data_chart.vue -> name: 'DataAnalysisDataChart'
You can specify a custom route template file:
💡 Tip: You can find the complete template example in the GitHub repository.
The template file should export a function that receives three parameters:
- dirName: string - Current directory name
- files: string[] - Vue file paths in the directory
- context: Object - Additional context information
For more details and examples, check the template file in the repository.
- Make sure you have the latest version of
vue-router
installed - Layout component is expected at
src/pages/layout/index.vue
by default - Route files are generated in the specified output directory
- Each directory generates a separate route file
- Existing route files won't be overwritten unless in forceOverwrite list
- After deleting existing route files, restart the server to regenerate
The plugin will check and generate route configuration in these cases:
- When development server starts
- When adding new .vue files
- When deleting .vue files
- When modifying .vue files
- When building starts