All Projects → brattonross → Vite Plugin Voie

brattonross / Vite Plugin Voie

Licence: mit
File system based routing plugin for Vite

Programming Languages

typescript
32286 projects

Labels

Projects that are alternatives of or similar to Vite Plugin Voie

Svelte Navigator
Simple, accessible routing for Svelte
Stars: ✭ 125 (-27.33%)
Mutual labels:  routing
Nextjs Dynamic Routes
[Deprecated] Super simple way to create dynamic routes with Next.js
Stars: ✭ 145 (-15.7%)
Mutual labels:  routing
Router
⚡️ A lightning fast HTTP router
Stars: ✭ 158 (-8.14%)
Mutual labels:  routing
Vpnfailsafe
IP leak prevention for OpenVPN
Stars: ✭ 130 (-24.42%)
Mutual labels:  routing
Grip
The microframework for writing powerful web applications.
Stars: ✭ 137 (-20.35%)
Mutual labels:  routing
Surge Rules
🦄 🎃 👻 Surge 规则集(DOMAIN-SET 和 RULE-SET),兼容 Surge for iOS 和 Surge for Mac 客户端。
Stars: ✭ 151 (-12.21%)
Mutual labels:  routing
Bide
A simple routing library for ClojureScript
Stars: ✭ 124 (-27.91%)
Mutual labels:  routing
Riptide
Client-side response routing for Spring
Stars: ✭ 169 (-1.74%)
Mutual labels:  routing
Frr
The FRRouting Protocol Suite
Stars: ✭ 2,009 (+1068.02%)
Mutual labels:  routing
Aspnetcoresubdomain
Simple usage lib for subdomain routing in ASP.NET Core/Framework MVC
Stars: ✭ 157 (-8.72%)
Mutual labels:  routing
Express Routemap
Display all your express routes in the terminal!
Stars: ✭ 131 (-23.84%)
Mutual labels:  routing
Router5
Flexible and powerful universal routing solution
Stars: ✭ 1,704 (+890.7%)
Mutual labels:  routing
Redux Tower
Saga powered routing engine for Redux app.
Stars: ✭ 155 (-9.88%)
Mutual labels:  routing
Express Env Example
A sample express environment that is well architected for scale. Read about it here:
Stars: ✭ 130 (-24.42%)
Mutual labels:  routing
Libosmscout
Libosmscout is a C++ library for offline map rendering, routing and location lookup based on OpenStreetMap data
Stars: ✭ 159 (-7.56%)
Mutual labels:  routing
Bsdrp
BSD Router Project
Stars: ✭ 126 (-26.74%)
Mutual labels:  routing
Routing
The routing core of itinero.
Stars: ✭ 145 (-15.7%)
Mutual labels:  routing
Flow builder
Flutter Flows made easy! A Flutter package which simplifies flows with a flexible, declarative API.
Stars: ✭ 169 (-1.74%)
Mutual labels:  routing
Rayo.js
Micro framework for Node.js
Stars: ✭ 170 (-1.16%)
Mutual labels:  routing
Ccna60d
60天通过思科认证的网络工程师考试
Stars: ✭ 155 (-9.88%)
Mutual labels:  routing

voie 🛣

npm version

File system based routing for Vue 3 applications using Vite

Voie is a Vite plugin that brings file system based routing to your Vue 3 applications.

Getting Started

Install Voie:

Vite 2 is supported from ^0.7.x, Vite 1 support is discontinued

$ npm install -D vite-plugin-voie

Note: [email protected]^4 is a peer dependency

Add to your vite.config.js:

import vue from '@vitejs/plugin-vue';
import voie from 'vite-plugin-voie';

export default {
  plugins: [vue(), voie()],
};

Overview

By default a page is a Vue component exported from a .vue or .js file in the src/pages directory.

You can access the generated routes by importing the voie-pages module in your application.

import { createRouter } from 'vue-router';
import routes from 'voie-pages';

const router = createRouter({
  // ...
  routes,
});

Note: TypeScript users can install type definitions for the generated routes via the voie-pages package:

$ npm install -D voie-pages

Configuration

interface UserOptions {
  pagesDir?: string;
  extensions?: string[];
  importMode?: ImportMode | ImportModeResolveFn;
  extendRoute?: (route: Route, parent: Route | undefined) => Route | void;
}

pagesDir

Relative path to the pages directory. Supports globs.

Default: 'src/pages'

extensions

Array of valid file extensions for pages.

Default: ['vue', 'js']

importMode

Import mode can be set to either async, sync, or a function which returns one of those values.

Default: 'async'

To get more fine-grained control over which routes are loaded sync/async, you can use a function to resolve the value based on the route path. For example:

// vite.config.js
export default {
  // ...
  plugins: [
    voie({
      importMode(path) {
        // Load index synchronously, all other pages are async.
        return path.includes('index') ? 'sync' : 'async';
      },
    }),
  ],
};

extendRoute

A function that takes a route and optionally returns a modified route. This is useful for augmenting your routes with extra data (e.g. route metadata).

// vite.config.js
export default {
  // ...
  plugins: [
    voie({
      extendRoute(route, parent) {
        if (route.path === '/') {
          // Index is unauthenticated.
          return route;
        }

        // Augment the route with meta that indicates that the route requires authentication.
        return {
          ...route,
          meta: { auth: true },
        };
      },
    }),
  ],
};

Using configuration

To use custom configuration, pass your options to Voie when instantiating the plugin:

// vite.config.js
import voie from 'vite-plugin-voie';

export default {
  plugins: [
    voie({
      pagesDir: 'src/views',
      extensions: ['vue', 'ts'],
    }),
  ],
};

File System Routing

Voie is inspired by the routing from NuxtJS 💚

Voie automatically generates an array of routes for you to plug-in to your instance of Vue Router. These routes are determined by the structure of the files in your pages directory. Simply create .vue files in your pages directory and routes will automatically be created for you, no additional configuration required!

For more advanced use cases, you can tailor Voie to fit the needs of your app through configuration.

Basic Routing

Voie will automatically map files from your pages directory to a route with the same name:

  • src/pages/users.vue -> /users
  • src/pages/users/profile.vue -> /users/profile
  • src/pages/settings.vue -> /settings

Index Routes

Files with the name index are treated as the index page of a route:

  • src/pages/index.vue -> /
  • src/pages/users/index.vue -> /users

Dynamic Routes

Dynamic routes are denoted using square brackets. Both directories and pages can be dynamic:

  • src/pages/users/[id].vue -> /users/:id (/users/one)
  • src/[user]/settings.vue -> /:user/settings (/one/settings)

Any dynamic parameters will be passed to the page as props. For example, given the file src/pages/users/[id].vue, the route /users/abc will be passed the following props:

{ "id": "abc" }

Nested Routes

We can make use of Vue Routers child routes to create nested layouts. The parent component can be defined by giving it the same name as the directory that contains your child routes.

For example, this directory structure:

src/pages/
  ├── users/
  │  ├── [id].vue
  │  └── index.vue
  └── users.vue

will result in this routes configuration:

[
  {
    path: '/users',
    component: '/src/pages/users.vue',
    children: [
      {
        path: '',
        component: '/src/pages/users/index.vue',
        name: 'users',
      },
      {
        path: ':id',
        component: '/src/pages/users/[id].vue',
        name: 'users-id',
      },
    ],
  },
];

Catch-all Routes

Catch-all routes are denoted with square brackets containing an ellipsis:

  • src/pages/[...all].vue -> /* (/non-existent-page)

The text after the ellipsis will be used both to name the route, and as the name of the prop in which the route parameters are passed.

Thanks

Many thanks go to @antfu for their support of this project.

Trivia

voie is the french word for "way" and is pronounced /vwa/.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].