File System Based Routing for Vite

0.33.3 · active · verified Sun Apr 19

vite-plugin-pages is a Vite plugin that enables file system-based routing for modern JavaScript frameworks, currently supporting Vue 3, React, and SolidJS applications. As of version 0.33.3, the project maintains an active development pace, regularly updating to support the latest Vite versions (e.g., v8, v7, v6, v5 were recently added). Its core value proposition is simplifying route management by automatically generating routes from a predefined directory structure, eliminating the need for manual route configuration in large applications. However, a significant recommendation is that for Vue users, the official `unplugin-vue-router` is generally preferred due to its deeper integration with `vue-router`'s internals and advanced features like auto-generated route types with autocomplete, offering a more tailored developer experience. This positions `vite-plugin-pages` as a robust, general-purpose solution particularly valuable for React and SolidJS ecosystems, or for simpler Vue setups where advanced `vue-router` integration might not be a primary concern.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to configure `vite-plugin-pages` in `vite.config.ts` and integrate the auto-generated routes into a basic Vue 3 application, including common options.

import { defineConfig } from 'vite';
import Pages from 'vite-plugin-pages';
import Vue from '@vitejs/plugin-vue'; // Example for Vue, also supports React/Solid
import { createRouter, createWebHistory } from 'vue-router';
import { createApp } from 'vue';

// vite.config.ts
export default defineConfig({
  plugins: [
    Vue(),
    Pages({
      dirs: 'src/pages', // Default directory for pages
      extensions: ['vue', 'js', 'ts', 'jsx', 'tsx'], // Supported file extensions
      exclude: ['**/components/**/*.vue'], // Exclude components from being treated as routes
    }),
  ],
  // Optional: alias for ~pages for better IDE support, though usually auto-resolved
  resolve: {
    alias: {
      '~pages': '/src/pages'
    }
  }
});

// src/main.ts (Example usage in a Vue app)
import App from './App.vue';
import routes from '~pages'; // Import the auto-generated routes

const router = createRouter({
  history: createWebHistory(),
  routes,
});

createApp(App).use(router).mount('#app');

// src/App.vue (Minimal example)
<template>
  <router-view />
</template>

<script setup lang="ts">
// Application logic here
</script>

view raw JSON →