Skip to content

generateRouter

Auto-generate router configuration and TypeScript type declarations from uni-app's pages.json.

Import Methods

typescript
// Submodule import (recommended)
import { generateRouter } from '@meng-xi/vite-plugin/plugins/generate-router'

// Barrel import
import { generateRouter } from '@meng-xi/vite-plugin'

Quick Start

typescript
import { defineConfig } from 'vite'
import { generateRouter } from '@meng-xi/vite-plugin'

export default defineConfig({
	plugins: [generateRouter()]
})

Options

OptionTypeDefaultDescription
pagesJsonPathstring'src/pages.json'Path to pages.json
outputPathstring'src/router.config.ts'Output file path
nameStrategyNameStrategy'camelCase'Route naming strategy
includeSubPackagesbooleantrueInclude sub-package routes
dtsstring | booleanfalseRoute type declaration file output path
preserveRouteChangesbooleantruePreserve user modifications to route configs

Inherits BasePluginOptions: enabled, logLevel, errorStrategy

Advanced Options

OptionTypeDefaultDescription
outputFormat'ts' | 'js''ts'Output format
customNameGenerator(path: string) => string-Custom name generator
watchbooleantrueWatch for changes
metaMappingRecord<string, string>See belowStyle to meta field mapping
exportTypesbooleantrueExport type definitions (TS)
fileHeaderbooleanfalseAdd comment header at file top

Route Naming Strategies

StrategyDescriptionExample PathGenerated Name
camelCaseCamel case/pages/user/profilepagesUserProfile
pascalCasePascal case/pages/user/profilePagesUserProfile
pathPath underscore/pages/user/profilepages_user_profile
customCustom function--

Default metaMapping

typescript
{
  navigationBarTitleText: 'title',
  requireAuth: 'requireAuth'
}

preserveRouteChanges Route Modification Preservation

When enabled, the plugin reads the existing file during regeneration and merges user modifications, avoiding overwriting manually added content.

Merge Strategy:

FieldBehavior
pathAlways follows pages.json, cannot be overridden
nameUser-modified values take priority
metaUser-modified values take priority, new fields from pages.json are auto-added
Non-standard propertiesUser-added custom properties like beforeEnter, component are fully preserved

Example: Suppose pages.json adds a new page, and the user has added beforeEnter to an existing route:

typescript
// User-modified route config
export const routes: RouteConfig[] = [
  {
    path: '/pages/index/index',
    name: 'pagesIndexIndex',
    meta: { title: 'Custom Title' },
    beforeEnter: (to, from, next) => { next() }  // User-added guard
  }
]

After regeneration:

typescript
export const routes: RouteConfig[] = [
  {
    path: '/pages/index/index',
    name: 'pagesIndexIndex',
    meta: { title: 'Custom Title', isTab: true },  // User title preserved, new isTab auto-added
    beforeEnter: (to, from, next) => { next() }     // Custom property preserved
  },
  {
    path: '/pages/new/page',                         // New page auto-generated
    name: 'pagesNewPage',
    meta: { title: 'New Page' }
  }
]

dts Type Declarations

Control whether to generate route type declaration files (.d.ts), extending the RouteNameMap interface for the @meng-xi/uni-router module to enable type-safe route navigation.

ValueDescription
falseDon't generate type declaration file (default)
trueUse default path src/router.d.ts
stringGenerate type declaration file at specified path

Generated type declaration file example:

typescript
import '@meng-xi/uni-router'

declare module '@meng-xi/uni-router' {
	interface RouteNameMap {
		/** Home */
		pagesIndexIndex: { path: '/pages/index/index'; meta: { title: string; isTab: true } }
		/** Profile */
		pagesUserProfile: { path: '/pages/user/profile'; meta: { title: string; requireAuth: true } }
	}
}

Examples

Output JavaScript

typescript
generateRouter({
	outputFormat: 'js',
	outputPath: 'src/router.config.js'
})

Custom Route Names

typescript
generateRouter({
	nameStrategy: 'custom',
	customNameGenerator: path => `route_${path.replace(/\//g, '_')}`
})

Custom Meta Mapping

typescript
generateRouter({
	metaMapping: {
		navigationBarTitleText: 'title',
		requireAuth: 'requireAuth',
		customField: 'custom'
	}
})

Exclude Sub-packages

typescript
generateRouter({
	includeSubPackages: false
})

Generate Route Type Declarations

typescript
generateRouter({
	dts: true // Use default path src/router.d.ts
})

// Or custom path
generateRouter({
	dts: 'src/types/router.d.ts'
})

Output Example

typescript
export interface RouteMeta {
	title?: string
	isTab?: boolean
	requireAuth?: boolean
	[key: string]: unknown
}

export interface RouteConfig {
	path: string
	name?: string
	meta?: RouteMeta
}

export const routes: RouteConfig[] = [
	{
		path: '/pages/index/index',
		name: 'pagesIndexIndex',
		meta: { title: 'Home', isTab: true }
	},
	{
		path: '/pages/user/profile',
		name: 'pagesUserProfile',
		meta: { title: 'Profile', requireAuth: true }
	}
]

export default routes

Notes

  • customNameGenerator is required when nameStrategy is 'custom'
  • TabBar pages automatically get isTab: true
  • preserveRouteChanges: true preserves user modifications to routes array
  • Supports parsing pages.json with comments

Released under the MIT License.