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

@linteng/dynamic-router

v0.2.2

Published

一个自动生成约定式路由的预处理器

Downloads

49

Readme

@linteng/dynamic-router

一个自动生成约定式路由的webpack预处理器和脚本

开始

首先安装 @linteng/dynamic-router:

npm install @linteng/@linteng/dynamic-router --save-dev

or

yarn add -D @linteng/@linteng/dynamic-router

or

pnpm add -D @linteng/@linteng/dynamic-router

然后在 webpack 配置中增加loader

webpack.config.js

module.exports = {
  module: {
    rules: [
      {
        test: /\.json$/,
        type: 'javascript/auto',
        resourceQuery: /dynamicRouter/,
        use: {
          loader: '@linteng/dynamic-router',
          options: {
            dir: path.resolve(process.cwd(), 'src', 'pages'),
          },
        },
      },
    ],
  },
};

给项目工程创建一份router json的配置

router.json

  • ignore: 忽略该路由的参数生成
  • replace: 该路由替换为自定义路由
{
  "ignore": [
    "/welcome"
  ],
  "replace": {
    "/page1": "/page1/:id?",
    "/page2": "/page2/edit/:id?"
  }
}

根据router.json 再引入routerHelper来生成最终的react router

router.tsx

import { pageRouter } from './router.json?dynamicRouter';
import { routerHelper } from '@linteng/dynamic-router;
import { Router, BrowserRouter, HashRouter } from 'react-router-dom';
import { createBrowserHistory } from 'history';

const history = createBrowserHistory();
const config = {
  
  /**
   * router过滤函数
   * @param {*} props   // 挂载在router上的props
   * @param {*} routerPath    // router path
   * @return {*} boolean  // 是否拦截掉路由
   */
  filter(props: any, routerPath: any) {
    if (routerPath === '/filter') {
      return false; // 渲染为error页面
    }
    return true;
  },
  error: {
    404: '/error/404'   // 配置404页面的具体指向路径
  }
};
const DynamicRouter = routerHelper(pageRouter, config);
export default function router() {
  return (
    <Router history={history}>
      <BrowserRouter>
        {DynamicRouter}
      </BrowserRouter>
    </Router>
  );
}

路由自动重定向 _config.ts

// 说明: 如果遇到路由redirects,则重定向到/error/redirect-page
// 应用场景:可用来作路由鉴权,例如在home页判断是否有登录态来决定是否跳转login
export default {
  redirects: [{ from: "/redirects", to: "/error/redirect-page" }]
};

页面props参数传递 _config.ts

// 说明: 可以同级目录的index.tsx与layout.tsx的props.pageConfig中获取到传递的参数
// 应用场景:例如需要往页面主体或布局注入一些静态配置时可用
export default {
  someConfig: [{ detail: "blabla" }]
};

目录概述

src
├── index.tsx
├── pages
│   ├── components    
│   ├── error
│   │   ├── 404
│   │   │   ├── components
│   │   │   ├── index.tsx
│   │   │   └── layout.tsx
│   ├── index.less
│   ├── index.tsx         // 路由主体页面
│   ├── layout.tsx        // 路由公共布局页面
│   ├── page1
│   │   ├── _config.ts    // 路由props参数设置与redirect设置
│   │   ├── components    // components目录为组件目录,里面的内容不会被生成路由对象
│   │   │   └── page1-comp.tsx
│   │   ├── index.tsx     
│   │   └── layout.tsx    
│   ├── page2
│   │   ├── components
│   │   ├── index.tsx
│   │   ├── layout.tsx
│   │   └── page2-1       // 子级路由
│   │       ├── components
│   │       ├── index.tsx
│   │       └── layout.tsx
├── router
│   ├── config.json       // 路由配置
│   └── index.tsx         // 路由生成入口

运行example工程

cd example
pnpm i
pnpm dev