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

umi-plugin-runtime-routes

v1.1.3

Published

Modify routes generated by umi at runtime.

Downloads

2

Readme

umi-plugin-runtime-routes

Modify routes generated by umi at runtime.

Install

yarn add umi-plugin-runtime-routes -D

Usage

1. Define a routes Modifier function

Routes Modifier is a function used for modifying routes at runtime, its parameter is routes which is generated by umi, and return value should be the modifiedroutes.

You could define Modifier by yourself, also this plugin provides some built-in helper functions and utils to help you to create Modifier easier.

For example, pick the envHelper from the built-in helper functions to create our sample Modifier:

// routesModifer.js
import { helpers } from 'umi-plugin-runtime-routes';

const envHelper = helpers.envHelper;

export default function(routes) {
  // Maybe in your case, you are not using "window.currentEnv" at runtime, this is just a sample
  const modifiedRoutes = envHelper(routes, window.currentEnv);
  
  return modifiedRoutes;
}

2. Add extra information in routes option in .umirc.js

The added info will be passed at runtime to help you creating Modifer to modify routes, there is no limitation about how to add these info. In the example, the envHelper is picked, so let's add some environment info in routes:

// .umirc.js

export default {
  routes: [
    {
      path: '/',
      component: '../layouts/index',
      routes: [
        { path: '/user', redirect: '/user/login', env: ['foo', 'bar'] },
        { path: '/user/login', redirect: './user/login', env: ['bar'] },
      ],
    },
  ],
};

3. Configure plugin in .umirc.js

Configure this plugin in .umirc.js.

// .umirc.js

export default {
  routes: [/* ... */];
  plugins: ['umi-plugin-runtime-routes', { modifier: 'path/to/routesModifier' }],
};

Options

options.modifier

options.modifier is a relative path string to the Modifier module, the base path is current working directory.

Modifer

A function used for modifying routes at runtime, like this:

function routesModifier(routes) {
  const modifiedRoutes = // ... modify routes
  return modifiedRoutes;
}

To help you create Modifier function easier, this plugin provides some built-in helper functions and some utils, see it at below.

Helpers

All of the built-in helpers are exported at helpers variable.

import { helpers } from 'umi-plugin-runtime-routes';

envHelper(routes, currentEnv)

This helper help you to modify routes by the application running environment.

1. Create modifier function

// routesModifier.js
import { helpers } from 'umi-plugin-runtime-routes';

function routesModifier(routes) {
  const modifiedRoutes = helpers.envHelper(routes, 'foo'); // 'foo' is current env
  return modifiedRoutes;
}

2. Define routes:

// .umirc.js

export default {
  routes: [
    { /* ... */, env: ['foo', 'bar'] } // add 'env' array at each route
  ]
};

Utils

All of the utils are exported at utils variable.

import { utils } from 'umi-plugin-runtime-routes';

routesFilter(routes, callback)

When you are creating the Modifier function by yourself, you need to do some works to iterate routes generated by umi, then return a new routes, this util helps you to automatically iterate routes and return the new routes, you just need to pass a filter callback to indicate if single route need to be preserved.

The first parameter is routes, the second parameter is the filter callback, when the return value of callback is true, the handling route is preserved, or the route will be deleted.

You can create the modifier like this:

// routesModifier.js
import { utils } from 'umi-plugin-runtime-routes';

export default function(routes) {
  const modifiedRoutes = utils.routesFilter(routes, (route) => {
    // return true or false to do the filter
  });
  
  return modifiedRoutes;
}

License

MIT License