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

@m-mrz/route-gen

v2.1.2

Published

A file-based route generator for React Router

Downloads

132

Readme

Route-Gen

Route-Gen is a Node.js package designed to streamline the routes definition process in React. It automatically generates routes for React Router (v6), depending on the file structure of your project.

Installation

You can easily install Route-Gen with npm:

npm install --save-dev @m-mrz/route-gen

Usage

Once Route-Gen is installed, you can trigger the routes generation by running the following command:

route-gen

This command will analyze the project structure (see Project Structure) and generate routes accordingly. After that, wrap your project entrypoint in a RouteProvider tag, providing the generated routes as follows.

import * as React from "react";
import * as ReactDOM from "react-dom";
import {
    createBrowserRouter,
    RouterProvider,
} from "react-router-dom";

import { routes } from "../routes"

const router = createBrowserRouter(routes);

ReactDOM.createRoot(document.getElementById("root")).render(
    <RouterProvider router={router}/>
);

Project Structure

Route-Gen relies on a specific project structure to generate routes correctly. The structure is highly inspired by the App Router of Next.js.

Use folders to represents routes

All pages should be put into a parent folder called pages. Here, the folder structure will mirror the generated routes. For instance, if we plan to have two routes /books and /books/:bookID we should create the following structure.

pages/
  └─ books/
      └─ :bookID/

Static routes

Static routes are the classic routes, like the /books one in the previous example, where the route name is fixed in time. In this case, the folder name exactly mirror the name of the generated route.

Dynamic routes

On the other hand, dynamic routes are routes that can have a different value depending on the rendered element. Following the React Router convention, their folder name should have the : char as prefix, like the /books/:bookID route in the previous example.

Use files to represent elements

Each rendered route contains a bunch of elements with a specific meaning

| Element | Description | Required | |----------|------------------------------------------------------------------------------|----------| | page | The content of the route view. It should be present in every accepted route. | False | | layout | The layout wrapping the route page, if any, and its children routes. | False |

Each element should be put in a different file, and the name of the file should mirror the name of the component. To make the generator understand that a file represents a specific element we should end its name with the element name.

For instance, consider the /books route of the previous example, and imagine it has both page and layout components. The resulting structure should be the following.

pages/
  └─ books/
      ├─ :bookID/...
      ├─ BooksPage.tsx
      └─ BooksLayout.tsx

Utility folders

A common need of React developers is to split a big component in several sub-components, for better readability and maintainability, that are usually put inside a components folder.

Since in our convention each folder would represent a different route, we add the concept of utility folders, that is folders not considered in the generated routes tree. To differentiate them from traditional ones, their name should have the _ char as prefix.

By enriching the previous examples, the resulting structure is something like that.

pages/
  └─ books/
      ├─ _components/...
      ├─ :bookID/...
      ├─ BooksPage.tsx
      └─ BooksLayout.tsx

Structure Example

Here an example of the resulting structure.

pages/
  ├─ items/
  │   └─ :itemID/
  │       ├─ _components/...
  │       ├─ _graphql/...
  │       └─ RootPage.tsx
  ├─ menus/
  │   └─ :menuID/
  │       ├─ _components/...
  │       ├─ _graphql/...
  │       └─ RootPage.tsx
  └─ merchants/
      ├─ :merchantID/
      │   ├─ _components/...
      │   ├─ _graphql/...
      │   └─ RootPage.tsx
      ├─ menus/
      │   ├─ _components/...
      │   ├─ _graphql/...
      │   └─ RootPage.tsx
      └─ ItemDetailLayout.tsx

Configuration

To customize Route-Gen's behavior, create a route-gen.json file in the root of your project. Here, you can specify the root of the pages directory, which serves as the starting point for generated routes and where the generated routes will be placed.

Here's the default configuration:

{
  "root": "./src"
}

License

Route-Gen is MIT Licensed.