npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vite-plugin-router

v0.3.9

Published

File system router for React applications using Vite.

Downloads

129

Readme

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