Giter Site home page Giter Site logo

arthurfiorette / vite-router Goto Github PK

View Code? Open in Web Editor NEW

This project forked from felipe-bergamaschi/vite-router

0.0 0.0 0.0 172 KB

File system routing for React + Typescript applications using Vite

License: MIT License

JavaScript 3.80% TypeScript 77.74% CSS 16.44% HTML 2.01%

vite-router's Introduction

vite-plugin-router

npm version monthly downloads types license

File system routing for React + Typescript applications using Vite

Getting Started

React

Does not support versions below react-router v6

Install:

npm install -D vite-plugin-router
npm install react-router react-router-dom

Vite config

Add to your vite.config.js:

import Routes from 'vite-plugin-router';

export default {
  plugins: [
    // ...
    Routes()
  ]
};

Overview

By default, vite-plugin-router creates a route file in the src/ directory containing all the route settings for your application, while observing the files within src/app.

Routes are configured using the Suspense API of react-router by default.

React

Create app folder within src/ and add index.tsx file. Export a default component as an example:

export default function Page() {
  return (
    <div>
      <h1>Vite Router</h1>
    </div>
  );
}

Run your application npm run dev, and you will be able to observe the (VITE ROUTER) logs and find the 'routes' file created.

Add AppRoutes to your main.tsx:

import React from 'react'
import ReactDOM from 'react-dom/client'
// ...
import { AppRoutes } from './routes'

ReactDOM.createRoot(document.getElementById('root')!).render(
  <React.StrictMode>
    <AppRoutes />
  </React.StrictMode>,
)

Configuration

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

// vite.config.js
import { defineConfig } from 'vite';
import Routes from 'vite-plugin-router';

export default defineConfig({
  plugins: [
    Routes({
      dir: 'src/app',
      outDir: 'src'
    })
  ]
});

dir

  • Type: string
  • Default: 'src/app'

Path to the pages directory.

outDir

  • Type: string
  • Default: 'src'

Output path for the routes file.

extensions

  • Default:
    • ['tsx', 'jsx', 'ts', 'js']

ignore files

  • Default:
    • [style.*, *.css] An array of glob patterns to ignore matches.
# folder structure
src/app/
    ├── admin/
    │  └── index.tsx
    │  └── index.css
    └── index.tsx
    └── style.ts

File System Routing

Inspired by the routing from NextJS

'Vite router' simplifies the process of creating routes for your vite application by automatically generating a 'routes' file based on the structure of the .tsx files in your pages directory. With this approach, connecting to your vite application becomes effortless, as no additional configuration is needed on your part.

Basic Routing

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

  • src/app/users.tsx -> /users
  • src/app/users/profile.tsx -> /users/profile
  • src/app/settings.tsx -> /settings

Index Routes

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

  • src/app/index.tsx -> /
  • src/app/users/index.tsx -> /users

Dynamic Routes

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

  • src/app/users/[id].tsx -> /users/:id (/users/123)
  • src/app/users/[user]/settings.tsx -> /users/:user/settings (/users/123/settings)

Layouts

We can utilize 'layout' files to create nested layouts from the parent. By naming a specific file 'layout' and defining its child routes within it, we can establish a hierarchical structure for our application. This approach enhances the organization and management of routes, making it easier to maintain and extend the application.

For example, this directory structure:

src/app/
    ├── users/
    │  ├── index.tsx
    │  └── layout.tsx
    └── index.tsx

🚀 New features

Our application is in a constant state of evolution, and our team is dedicated to bringing you numerous improvements and exciting new features that will enhance its power and user-friendliness. Below, we present a glimpse of some of the features we are actively developing:

🚧 Catch-all Routes

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

  • src/app/[...all].tsx -> /* (/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.

🚧 Custom error 404

Create custom 404 routes tailored to each directory.

src/app/
    ├── users/
    │  ├── index.tsx
    │  ├── layout.tsx
    │  └── 404.tsx
    └── index.tsx

License

MIT License © 2023-PRESENT Felipe Bergamaschi

vite-router's People

Contributors

felipe-bergamaschi avatar arthurfiorette avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.