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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@lightfish/router

v0.0.1

Published

基于文件系统的 React Router 路由生成工具。将 Vite `import.meta.glob` 导入的页面模块映射,自动转换为 `react-router-dom` 的 `RouteObject[]`。

Readme

@lightfish/router

基于文件系统的 React Router 路由生成工具。将 Vite import.meta.glob 导入的页面模块映射,自动转换为 react-router-domRouteObject[]

安装

pnpm add @lightfish/router

peerDependencies 要求:

  • react >= 18
  • react-router-dom 6.21.1

快速开始

1. 按文件系统约定组织页面

src/pages/
├── index.tsx            # 首页 /
├── layout.tsx           # 全局布局(可选)
├── settings.tsx         # 首页元信息(可选)
├── users/
│   ├── index.tsx        # /users
│   ├── layout.tsx       # users 目录专属布局(可选)
│   ├── 404.tsx          # users 下的 404(可选)
│   └── [id]/
│       ├── index.tsx    # /users/:id
│       └── settings.tsx # /users/:id 的页面元信息
└── components/          # 被默认忽略,不生成路由
    └── UserCard.tsx

2. 在 Vite 项目中配合 import.meta.glob 使用

// src/router.tsx
import { createFileRoutes } from "@lightfish/router";
import { createBrowserRouter, RouterProvider } from "react-router-dom";

// 导入所有页面模块(eager 模式确保路由表立即可用)
const pages = import.meta.glob("/src/pages/**/*.tsx", { eager: true }) as Record<string, any>;
const layouts = import.meta.glob("/src/pages/**/layout.tsx", { eager: true }) as Record<string, any>;
const notFounds = import.meta.glob("/src/pages/**/404.tsx", { eager: true }) as Record<string, any>;
const settings = import.meta.glob("/src/pages/**/settings.tsx", { eager: true }) as Record<string, any>;

const routeObjects = createFileRoutes({
  pages,
  layouts,
  notFounds,
  settings,
  // 以下为默认值,可按需覆盖:
  // rootDir: "/src/pages",
  // ignoredSegmentNames: ["components", "models"],
});

const router = createBrowserRouter(routeObjects);

export function AppRouter() {
  return <RouterProvider router={router} />;
}

3. 挂载到应用入口

// src/main.tsx
import { createRoot } from "react-dom/client";
import { AppRouter } from "./router";

createRoot(document.getElementById("root")!).render(<AppRouter />);

文件路由约定

| 文件名 | 作用 | 说明 | |---|---|---| | index.tsx | 页面组件 | 必选。每个目录的默认页面。 | | layout.tsx | 布局组件 | 可选。包裹当前目录及其子路由。组件内部通过 <Outlet /> 渲染子路由。 | | 404.tsx | 404 页面 | 可选。当前目录下的未匹配路径兜底。 | | settings.tsx | 页面元信息 | 可选。default 导出 { title?: string },组件挂载时自动设置 document.title。 |

动态路由

文件或目录名中的 [param] 语法会被转换为 react-router-dom 的 :param 路径参数:

| 文件路径 | 生成的路由 path | |---|---| | src/pages/users/[id]/index.tsx | /users/:id | | src/pages/posts/[postId]-[slug]/index.tsx | /posts/:postId-:slug |

忽略目录

路径中包含 ignoredSegmentNames(默认 ["components", "models"])中任意目录名的文件不会生成路由。允许你将辅助组件就近放在页面目录下:

src/pages/users/
├── index.tsx          # ✅ 生成路由 /users
└── components/
    └── UserTable.tsx  # ❌ 忽略,不生成路由

API

createFileRoutes(options): RouteObject[]

核心函数,接收配置对象,返回 react-router-domRouteObject[]

CreateFileRoutesOptions

| 字段 | 类型 | 默认值 | 说明 | |---|---|---|---| | pages | Record<string, FileRouteModule> | 必填 | import.meta.glob 导入的页面模块映射。key 为文件路径,value 为模块对象。 | | layouts | Record<string, FileRouteModule> | {} | layout 模块映射。 | | notFounds | Record<string, FileRouteModule> | {} | 404 模块映射。 | | settings | Record<string, FileRouteSettingsModule> | {} | 页面元信息模块映射。 | | rootDir | string | "/src/pages" | 页面文件的根目录,会从文件路径中去掉此前缀。 | | ignoredSegmentNames | string[] | ["components", "models"] | 需要忽略的目录名列表。 | | commonLayout | FileRouteComponent | 内置 DefaultLayout | 全局公共布局组件。当某目录无 layout.tsx 时使用。 | | commonNotFound | FileRouteComponent | 内置 DefaultNotFound | 全局公共 404 组件。当某目录无 404.tsx 时使用。 |

routeSegmentToPath(segment: string): string

将文件路由的动态段语法 [param] 转换为 :param。也可单独使用。

License

MIT