stacks/vite-plugin-routes
publicClone
Push over the same URL. A password will not work: create a token under access tokens and use it in place of one.
- .github
- .vscode
- .zed
- docs
- src
- tests
- .gitignore 131 B
- build.ts 568 B
- bun.lock 283.3 KB
- CHANGELOG.md 49.4 KB
- client.d.ts 2.0 KB
- eslint.config.ts 719 B
- LICENSE 1.1 KB
- package.json 4.5 KB
- README.md 6.4 KB
- renovate.json 50 B
- route.schema.json 699 B
- tsconfig.json 1.7 KB
- tsconfig.typecheck.json 74 B
- tsdown-runtime.config.ts 617 B
- tsdown.config.ts 536 B
- vitest.config.ts 1.3 KB
- vitest.workspace.js 104 B
vite-plugin-routes

File based typed routing for Vue Router with TypeScript support ✨
Features
- 📁 File Based Routes are automatically generated from your files
- 🔄 TypeScript Support Full type safety for your routes
- 🌐 Multiple Frameworks Works with Vite, Webpack, Rollup, and esbuild
- 🎨 Meta Configuration Configure routes via route blocks in your pages
- 🔌 Vue Router Integration Seamless integration with Vue Router 4
- 📱 HMR Optimized Fast hot module replacement
- 🛠️ Flexible Configuration Customize route patterns, exclusions, and more
Install
npm install -D vite-plugin-routes
# or
yarn add -D vite-plugin-routes
# or
pnpm add -D vite-plugin-routes
# or
bun add -D vite-plugin-routesGet Started
Add VueRouter plugin before Vue plugin:
Vite
// vite.config.ts
import VueRouter from 'vite-plugin-routes/vite'
export default defineConfig({
plugins: [
VueRouter({
/_ options _/
}),
// ⚠️ Vue must be placed after VueRouter()
Vue(),
],
})Rollup
// rollup.config.js
import VueRouter from 'vite-plugin-routes/rollup'
export default {
plugins: [
VueRouter({
/_ options _/
}),
// ⚠️ Vue must be placed after VueRouter()
Vue(),
],
}Webpack
// webpack.config.js
module.exports = {
/_ ... _/
plugins: [
require('vite-plugin-routes/webpack')({
/_ options _/
}),
],
}Vue CLI
// vue.config.js
module.exports = {
configureWebpack: {
plugins: [
require('vite-plugin-routes/webpack')({
/_ options _/
}),
],
},
}esbuild
// esbuild.config.js
import { build } from 'esbuild'
import VueRouter from 'vite-plugin-routes/esbuild'
build({
plugins: [VueRouter()],
})Setup
After installing, you should run your dev server(usually npm run dev)to generate the first version of the types. Then you need to add the types to your tsconfig.json.
{
"include": [
// ...
"./typed-router.d.ts"
],
// ...
"compilerOptions": {
// ...
"moduleResolution": "Bundler"
// ...
}
}Then, if you have an env.d.ts file like the one created by npm vue create <my-project>, add the vite-plugin-routes/client types to it:
// env.d.ts
/// <reference types="vite/client" />
/// <reference types="vite-plugin-routes/client" />If you don't have an env.d.ts file, you can create one and add the vite-plugin-routes types to it or you can add them to the types property in your tsconfig.json:
{
"compilerOptions": {
// ...
"types": ["vite-plugin-routes/client"]
}
}Finally, import the generated routes from vue-router/auto-routes and pass them to the router:
import { createRouter, createWebHistory } from 'vue-router'
+import { routes } from 'vue-router/auto-routes'
createRouter({
history: createWebHistory(),
// pass the generated routes written by the plugin ��
+ routes,
})Configuration
// vite.config.ts
import VueRouter from 'vite-plugin-routes/vite'
export default defineConfig({
plugins: [
VueRouter({
// Customize your routes
routesFolder: ['src/pages'], // default: 'src/pages'
exclude: [], // Patterns to exclude from route generation
// ... other options
}),
Vue(),
],
})Available Options
| Option | Type | Default | Description |
|---|---|---|---|
routesFolder | string | string[] | 'src/pages' | Path(s) to the pages directory. Supports globs. |
exclude | string[] | [] | Patterns to exclude from route generation. |
filePatterns | string[] | ['**/*.vue'] | File patterns to include for route generation. |
Testing
bun testChangelog
Please see our releases page for more information on what has changed recently.
Contributing
Please review the Contributing Guide for details.
Community
For help, discussion about best practices, or any other conversation that would benefit from being searchable:
For casual chit-chat with others using this package:
Join the Stacks Discord Server
Postcardware
"Software that is free, but hopes for a postcard." We love receiving postcards from around the world showing where vite-plugin-routes is being used! We showcase them on our website too.
Our address: Stacks.js, 12665 Village Ln #2306, Playa Vista, CA 90094, United States 🌎
Sponsors
We would like to extend our thanks to the following sponsors for funding Stacks development. If you are interested in becoming a sponsor, please reach out to us.
Credits
License
The MIT License (MIT). Please see LICENSE for more information.
Made with 💙
[npm-downloads-href]: https://npmjs.com/package/vite-plugin-routes --> [codecov-href]: https://codecov.io/gh/stacksjs/vite-plugin-routes -->