or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-vue--cli-plugin-router

Vue CLI plugin that provides Vue Router integration for Vue.js applications with automatic configuration and template generation

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/@vue/cli-plugin-router@5.0.x

To install, run

npx @tessl/cli install tessl/npm-vue--cli-plugin-router@5.0.0

index.mddocs/

@vue/cli-plugin-router

@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.

Package Information

  • Package Name: @vue/cli-plugin-router
  • Package Type: npm
  • Language: JavaScript
  • Installation: vue add router

Core Imports

The plugin is typically invoked through Vue CLI rather than imported directly:

vue add router

For 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");

Basic Usage

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:

  • Installs the appropriate vue-router version (3.x for Vue 2, 4.x for Vue 3)
  • Creates router configuration files
  • Generates basic route components (Home, About)
  • Updates App.vue with navigation
  • Configures the application entry point

Capabilities

Plugin Entry Point

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;
}

Plugin Prompts Configuration

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.'
};

Generator Function

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;
}

Vue 3 AST Transformer

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;
}

Generator API Methods

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;
}

Generated Project Structure

When the plugin runs, it creates the following files:

Router Configuration (Vue 2)

// 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

Router Configuration (Vue 3)

// 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 router

View Components

The plugin generates two basic view components:

  • src/views/HomeView.vue - Home page component
  • src/views/AboutView.vue - About page component (lazy-loaded)

Updated App Component

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>

Configuration Options

History Mode

Controls the router mode:

  • historyMode: true - Uses HTML5 History API (clean URLs without #)
  • historyMode: false - Uses hash mode (URLs with # fragment)

Vue Version Detection

Automatically detects Vue version and installs appropriate router version:

  • Vue 2: vue-router@^3.5.1
  • Vue 3: vue-router@^4.0.3

TypeScript Support

When TypeScript plugin is detected, the generated templates include proper type annotations:

  • Router configuration uses RouteConfig (Vue 2) or RouteRecordRaw (Vue 3) types
  • Generated files use .ts extensions when appropriate
  • Imports include type definitions from vue-router
  • Converts generated files to TypeScript automatically during invocation

Integration Features

  • Babel Integration: Enables dynamic imports for code splitting
  • CSS Preprocessor Support: Adapts styling in generated components
  • Bare Project Mode: Simplified setup for minimal projects

Dependencies

Runtime Dependencies

  • @vue/cli-shared-utils@^5.0.9 - Shared utilities for Vue CLI plugins

Peer Dependencies

  • @vue/cli-service@^3.0.0 || ^4.0.0 || ^5.0.0-0 - Vue CLI service

Generated Dependencies

  • vue-router@^3.5.1 (for Vue 2 projects)
  • vue-router@^4.0.3 (for Vue 3 projects)

Error Handling

The plugin gracefully handles various scenarios:

  • Automatically detects Vue version and uses appropriate router API
  • Integrates with existing plugins (Babel, TypeScript) when present
  • Converts files to TypeScript during invocation if TypeScript plugin exists
  • Provides appropriate fallbacks for different build configurations