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

@feoe/fs-router

v0.0.11

Published

file system based routing

Downloads

23

Readme

@feoe/fs-router

npm version npm downloads license

基于文件的约定式路由

为 React 应用提供类型安全的路由解决方案

文档 | 快速开始 | 示例

✨ 特性

  • 🚀 约定式路由 - 基于文件系统的路由约定,零配置即可使用
  • 📝 最佳实践 - 自带 React Router v6+ BrowserRouter/DataRouter 组件模块化
  • 🔒 类型安全 - 完整的 TypeScript 支持,提供类型安全的导航
  • 高性能 - 默认开启代码分割和懒加载,优化应用性能
  • 🔄 热更新 - 开发时文件变更自动重新生成路由文件
  • 🔧 多构建工具支持 - 支持 Vite、Webpack、Rspack 等主流构建工具

📦 安装

npm install @feoe/fs-router -D
# 或
yarn add @feoe/fs-router -D
# 或
pnpm add @feoe/fs-router -D

系统要求

  • Node.js 16.0 或更高版本
  • React 18.0 或更高版本
  • TypeScript 4.5 或更高版本(可选,但推荐)
  • React Router 6.0 或更高版本

🚀 快速开始

1. 配置构建工具

根据你使用的构建工具,选择对应的配置方式:

Vite

// vite.config.ts
import { defineConfig } from 'vite'
import { FileBasedRouterVite as fileBasedRouter } from '@feoe/fs-router/vite'

export default defineConfig({
  plugins: [
    fileBasedRouter({
      routesDirectory: 'src/routes',
      generatedRoutesPath: 'src/routes.tsx'
    })
  ]
})

Rspack

// rspack.config.js
const { FileBasedRouterRspack as fileBasedRouter } = require('@feoe/fs-router/rspack')

module.exports = {
  plugins: [
    fileBasedRouter({
      routesDirectory: 'src/routes',
      generatedRoutesPath: 'src/routes.tsx'
    })
  ]
}

Webpack

// webpack.config.js
const { FileBasedRouterWebpack as fileBasedRouter } = require('@feoe/fs-router/webpack')

module.exports = {
  plugins: [
    fileBasedRouter({
      routesDirectory: 'src/routes',
      generatedRoutesPath: 'src/routes.tsx'
    })
  ]
}

2. 创建路由文件

src/routes 目录下创建页面文件:

src/routes/
├── layout.tsx          # 根布局 (必须配置)
├── page.tsx            # 首页 (/)
├── about/
│   └── page.tsx        # 关于页面 (/about)
└── users/
    ├── layout.tsx      # 用户模块布局
    ├── page.tsx        # 用户列表 (/users)
    └── [id]/
        └── page.tsx    # 用户详情 (/users/:id)

3. 创建根布局

// src/routes/layout.tsx
import { Outlet } from 'react-router-dom'

export default function Layout() {
  return (
    <div>
      <nav>
        <a href="/">首页</a>
        <a href="/about">关于</a>
        <a href="/users">用户</a>
      </nav>
      <main>
        <Outlet />
      </main>
    </div>
  )
}

4. 创建页面组件

// src/routes/page.tsx
export default function HomePage() {
  return (
    <div>
      <h1>欢迎使用 @feoe/fs-router</h1>
      <p>这是基于文件的约定式路由示例</p>
    </div>
  )
}

5. 在应用中使用

// src/main.tsx
import React from 'react'
import ReactDOM from 'react-dom/client'
import { createBrowserRouter, RouterProvider } from 'react-router-dom'
import { routes } from './routes'

const router = createBrowserRouter(routes)

ReactDOM.createRoot(document.getElementById('root')!).render(
  <React.StrictMode>
    <RouterProvider router={router} />
  </React.StrictMode>
)

📖 核心概念

约定式路由

基于文件系统的路由约定,通过文件和目录结构自动生成路由配置:

  • page.tsx - 页面组件
  • page.data.ts - 页面数据加载 loader
  • layout.tsx - 布局组件
  • layout.data.ts - 布局数据加载 loader
  • loading.tsx - 加载状态组件
  • error.tsx - 错误边界组件
  • loader.ts - 数据加载器

动态路由

使用方括号创建动态路由:

src/routes/
├── users/
│   ├── [id]/
│   │   └── page.tsx    # /users/:id
│   └── [id]/
│       └── edit/
│           └── page.tsx # /users/:id/edit

类型安全

自动生成类型定义,提供完整的 TypeScript 支持:

import { useNavigate } from 'react-router-dom'

const navigate = useNavigate()

// 类型安全的导航
navigate('/users/123')  // ✅ 正确
navigate('/invalid')    // ❌ TypeScript 错误

🎯 示例

查看完整的示例项目:

📚 文档

完整的文档和 API 参考请访问:https://fs-router.feoe.dev

🤝 贡献

欢迎贡献代码!请查看 贡献指南 了解如何参与项目开发。

🙏 致谢

本项目的灵感来源于以下优秀的开源项目:

📄 许可证

MIT © feoe