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

@moccona/vite-plugin-react-conventional-router

v0.1.0

Published

Vite plugin for react conventional router

Downloads

3

Readme

@moccona/vite-plugin-react-conventional-router

⚠️注意⚠️

这个库只基于 react-router 实现,与 react-router 的功能高度集成。其他的路由库暂时不支持,比如 @tanstack/react-router

安装与配置

  • 先安装 react-routerreact-router-dom
npm i react-router react-router-dom
  • 安装插件
npm i @moccona/vite-plugin-react-conventional-router -D
  • 配置插件
// vite.config.ts
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import conventionalRouter from "@moccona/vite-react-conventional-router "

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    react(),
    conventionalRouter({
      // 需要解析为路由的文件
      include: ["src/pages/**"],
      // 需要被排除解析的文件
      exclude: ["src/**/components/**","src/**/hooks/**"]
    }),
  ],
})

//
  • 添加虚拟模块类型定义文件到 tsconfig.json , 避免导入路有配置的时候报 MODULE NOT FOUND 的错误。
{
  //...
  "include": [
    "src",
    "./node_modules/@moccona/vite-react-conventional-router/client.d.ts"
  ]
}
  • 从虚拟路由模块中导入生成的路由数据,然后创建路由
import routes from "virtual:routes"
import { createBrowserRouter, RouterProvider } from "react-router-dom";

export default function App() {
  return <div id="main">
    <RouterProvider
      router={createBrowserRouter(routes)}
      fallbackElement={
        <div>
          Loading
        </div>
      }
    />
  </div>
}

文件名与路由的映射关系

`src/pages/index.tsx`           -> `/`
`src/pages/home.tsx`            -> `/home`
`src/pages/home.list.tsx`       -> `/home/list`
`src/pages/[email protected]`        -> `/home/:id`
`src/pages/home.list.$id.tsx`   -> `/home/list/:id?`

Layout

两种布局组件格式。

  • **/layout.tsx

    作为同级目录下的布局组件。

  • **/page.layout.tsx

    作为page路由的布局组件。

ErrorBoundary

两种错误边界组件格式。

  • **/errorBoundary.tsx

    作为同级目录下的错误边界组件。

  • **/page.errorBoundary.tsx

    作为page路由的错误边界组件。

如何组织路由文件内容

文件内容组织参考 react-routerlazy 部分。唯一不同的是,默认到处会作为 Component 被使用。