Vue CLI plugin that provides Vue Router integration for Vue.js applications with automatic configuration and template generation
npx @tessl/cli install tessl/npm-vue--cli-plugin-router@5.0.0@vue/cli-plugin-router is a Vue CLI plugin that automatically adds Vue Router support to Vue.js applications. It provides code generation functionality to set up routing infrastructure, including router configuration, route definitions, and navigation components for both Vue 2 and Vue 3 projects.
vue add routerThe plugin is typically invoked through Vue CLI rather than imported directly:
vue add routerFor programmatic usage (advanced use cases):
const routerPlugin = require("@vue/cli-plugin-router");
const routerGenerator = require("@vue/cli-plugin-router/generator");
const routerPrompts = require("@vue/cli-plugin-router/prompts");The most common usage is through Vue CLI's add command:
# Add router plugin to existing Vue project
vue add router
# The plugin will prompt for configuration:
# ? Use history mode for router? (Y/n)This automatically:
Main plugin function that integrates with Vue CLI's plugin system.
/**
* Main plugin entry point (currently empty implementation)
* @param api - Vue CLI plugin API instance
* @param options - Plugin configuration options
*/
function routerPlugin(api: PluginAPI, options: PluginOptions): void;
interface PluginOptions {
[key: string]: any;
}Defines interactive prompts shown when the plugin is invoked.
/**
* Plugin prompts configuration for user interaction
*/
const prompts: PromptDefinition[];
interface PromptDefinition {
name: string;
type: 'confirm' | 'input' | 'list' | 'checkbox';
message: string;
description?: string;
default?: any;
choices?: any[];
when?: (answers: any) => boolean;
}The router plugin defines one prompt:
const historyModePrompt: PromptDefinition = {
name: 'historyMode',
type: 'confirm',
message: 'Use history mode for router? (Requires proper server setup for index fallback in production)',
description: 'By using the HTML5 History API, the URLs don\'t need the \'#\' character anymore.'
};Core generator that performs the router setup and file generation.
/**
* Main generator function that configures router in Vue projects
* @param api - Vue CLI Generator API instance
* @param options - User-selected options from prompts
* @param rootOptions - Project-level configuration options
*/
function routerGenerator(
api: GeneratorAPI,
options: RouterOptions,
rootOptions: RootOptions
): void;
interface RouterOptions {
historyMode?: boolean;
}
interface RootOptions {
vueVersion?: '2' | '3';
cssPreprocessor?: string;
bare?: boolean;
}Transforms Vue 3 application entry files to inject router usage.
/**
* AST transformer for injecting router usage in Vue 3 apps
* @param file - File object containing source code
* @param api - Transformation API with jscodeshift instance
* @returns Transformed source code as string
*/
function injectUseRouter(file: FileInfo, api: TransformAPI): string;
interface FileInfo {
path: string;
source: string;
}
interface TransformAPI {
jscodeshift: JSCodeshift;
}The plugin utilizes Vue CLI's Generator API methods for code generation:
interface GeneratorAPI {
/** Inject import statements into the entry file */
injectImports(file: string, imports: string): void;
/** Apply AST transformations to a script file */
transformScript(file: string, transformer: Function): void;
/** Inject options into Vue root instance (Vue 2) */
injectRootOptions(file: string, options: string): void;
/** Add dependencies to package.json */
extendPackage(packageModifications: PackageModifications): void;
/** Render template files to the project */
render(templatePath: string, data: TemplateData): void;
/** Check if another plugin is installed */
hasPlugin(pluginName: string): boolean;
/** Boolean indicating if plugin is being invoked */
invoking: boolean;
/** Path to the main entry file */
entryFile: string;
}
interface PackageModifications {
dependencies?: Record<string, string>;
devDependencies?: Record<string, string>;
scripts?: Record<string, string>;
}
interface TemplateData {
/** Whether to use HTML5 History API (true) or hash mode (false) */
historyMode?: boolean;
/** Whether Babel or TypeScript plugins are present for compilation */
doesCompile?: boolean;
/** Whether TypeScript plugin is installed */
hasTypeScript?: boolean;
}When the plugin runs, it creates the following files:
// src/router/index.js
import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
Vue.use(VueRouter)
const routes = [
{
path: '/',
name: 'home',
component: HomeView
},
{
path: '/about',
name: 'about',
component: () => import('../views/AboutView.vue')
}
]
const router = new VueRouter({
mode: 'history', // Only if historyMode: true
base: process.env.BASE_URL,
routes
})
export default router// src/router/index.js
import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
const routes = [
{
path: '/',
name: 'home',
component: HomeView
},
{
path: '/about',
name: 'about',
component: () => import('../views/AboutView.vue')
}
]
const router = createRouter({
// Uses createWebHistory if historyMode: true, createWebHashHistory if historyMode: false
history: createWebHistory(process.env.BASE_URL), // or createWebHashHistory()
routes
})
export default routerThe plugin generates two basic view components:
src/views/HomeView.vue - Home page componentsrc/views/AboutView.vue - About page component (lazy-loaded)Modifies src/App.vue to include router navigation:
<template>
<div id="app">
<nav>
<router-link to="/">Home</router-link> |
<router-link to="/about">About</router-link>
</nav>
<router-view/>
</div>
</template>Controls the router mode:
historyMode: true - Uses HTML5 History API (clean URLs without #)historyMode: false - Uses hash mode (URLs with # fragment)Automatically detects Vue version and installs appropriate router version:
vue-router@^3.5.1vue-router@^4.0.3When TypeScript plugin is detected, the generated templates include proper type annotations:
RouteConfig (Vue 2) or RouteRecordRaw (Vue 3) types.ts extensions when appropriate@vue/cli-shared-utils@^5.0.9 - Shared utilities for Vue CLI plugins@vue/cli-service@^3.0.0 || ^4.0.0 || ^5.0.0-0 - Vue CLI servicevue-router@^3.5.1 (for Vue 2 projects)vue-router@^4.0.3 (for Vue 3 projects)The plugin gracefully handles various scenarios: