File System Based Routing for Vite
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
-
TypeError: Pages is not a function
cause Attempting to `require('vite-plugin-pages')` in a CommonJS context after v0.32.0, where the package is ESM-only.fixChange `const Pages = require('vite-plugin-pages')` to `import Pages from 'vite-plugin-pages'` in your `vite.config.ts` and ensure your project is configured for ESM. -
Module '~pages' has no exported member 'routes'. Did you mean to use a default import?
cause Incorrect import of the virtual routes module, typically trying to use named import (`import { routes } from '~pages'`) instead of default import.fixUse the correct default import syntax: `import routes from '~pages'` (for Vue), `import routes from '~react-pages'` (for React), or `import routes from '~solid-pages'` (for Solid). -
Failed to parse Vite config: [vite-plugin-pages] Unknown option: 'oldDeprecatedOption'
cause Using a configuration option that was deprecated and subsequently removed in `vite-plugin-pages` v0.33.0 or later.fixRemove the unrecognized option from your `vite.config.ts`. Consult the official `vite-plugin-pages` documentation for the latest configuration options.
Warnings
- gotcha For Vue.js users, the official `unplugin-vue-router` is recommended over `vite-plugin-pages` due to its deeper integration with `vue-router`, improved type generation, and advanced features. While `vite-plugin-pages` still supports Vue, `unplugin-vue-router` offers a superior developer experience for Vue applications.
- breaking In v0.33.0, all previously deprecated options were removed. Projects upgrading to or past this version must update their `vite-plugin-pages` configuration to use current options or remove invalid ones, which will result in build errors if not addressed.
- breaking Version 0.32.0 migrated the package to be an ESM-only module. This means CommonJS `require()` syntax is no longer supported for importing `vite-plugin-pages`, and projects not configured for ESM will encounter `ERR_REQUIRE_ESM`.
- breaking Since v0.19.0, `vite-plugin-pages` only supports `react-router` version 6 and above. Users on `react-router` v5 will encounter incompatibilities and must either upgrade `react-router` or use an older version of `vite-plugin-pages`.
Install
-
npm install vite-plugin-pages -
yarn add vite-plugin-pages -
pnpm add vite-plugin-pages
Imports
- Pages
const Pages = require('vite-plugin-pages')import Pages from 'vite-plugin-pages'
- routes (Vue)
import { routes } from '~pages'import routes from '~pages'
- routes (React)
import { routes } from '~react-pages'import routes from '~react-pages'
- routes (Solid)
import { routes } from '~solid-pages'import routes from '~solid-pages'
- Type References (Vue/React)
import type {} from 'vite-plugin-pages/client'/// <reference types="vite-plugin-pages/client" /> // For Vue /// <reference types="vite-plugin-pages/client-react" /> // For React
Quickstart
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>