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-file-based-react-router

v0.0.2

Published

Vite plugin to generate file-based routes for react-router

Downloads

79

Readme

vite-plugin-file-based-react-router

Opinionated vite plugin to generate file-based routes for using with react-router.

Inspired from tanstack.com/router, but without TypeScript.

Features

To-do

  • [ ] unit tests
  • [ ] duplicate routes detection

Installation

npm --save-dev vite-plugin-file-based-react-router

Config

Plugin config in vite.config.js.

import fileBasedReactRouter from 'vite-plugin-file-based-react-router';

//...

export default defineConfig({
  plugins: [
    fileBasedReactRouter({
      root, // default as 'src'
      routesDir, // default as 'pages'
      subRouters: { // optional sub-routers
        '/app/editor': { 
          // load sub-router from ./modules/editor and mount it to /app/editor/*
          importPath: './modules/editor',
          isLazy: true,
        },
      },
    }),
    //...
  ]
});

File-based convention

  • Sample files under src/pages:
├── _error.jsx or _error.lazy_.jsx : component as errorElement under /
├── _layout.jsx : layout as element under /
├── app 
│   ├── [module]
│   │   └── _any.lazy_.jsx : component as element under /app/:module/* with lazy import
│   └── _layout.jsx : layout as element under /app
├── route1 
│   └── index.jsx : component as element under /route1
├── index.loader_.js : (async) function as loader under /
├── login.lazy_.jsx : component as element under /login
└── logout.loader_.js : (async) function as loader under /logout
  • Sample files under src/modules/editor/pages as sub-router:
├── _layout.jsx : layout as element under /app/editor
├── project.[id].edit.lazy_.jsx : component as element under /app/editor/project/:id/edit with lazy import
├── project.lazy_.jsx : component as element under /app/editor/project with lazy import
└── workspace.jsx : component as element under /app/editor/workspace 

Route component (as element)

export const handle = {
  //...
}; // exposed as handle

export const Component = () => {...}; // as layout or page 
Component.displayName = 'ComponentName'; // optional, useful for inspection

Data loader

export default ({ params }) => { 
    const result = do_some_thing_before_rendering_page();
    return result; // can be accessed by useLoaderData() in the page component
}

Error component (as errorElement)

function ErrorComponent() {
    const error = useRouteError();
    // ...
};

export default ErrorComponent;

Lazy module

// loader is optional
export const loader = ({ params }) => { 
    const result = do_some_thing_before_rendering_page();
    return result; // can be accessed by useLoaderData() in the page component
}

export const handle = {
  //...
}; 

export const Component = () => {...}; // as page 
Component.displayName = 'ComponentName'; // optional, useful for inspection

Runtime generated files

After running vite dev or vite build, it will generate

  • ./src/routes.runtime.jsx
  • ./src/modules/editor/sub-routes.runtime.jsx

Usage

import { RouterProvider, createBrowserRouter } from "react-router-dom";
import routes, { lazyRouting } from './routes.runtime';

const router = createBrowserRouter(routes, { patchRoutesOnNavigation: lazyRouting });

<RouterProvider router={router} />

License

MIT