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

react-route-generate

v1.1.2

Published

[中文文档](./README_Zh.MD)

Downloads

17

Readme

react-route-generate

中文文档

Usage

Execute the following command anywhere to globally install this tool:

npm install react-route-generate -g

Install the tool package again in the project where you want to use it:

npm install react-route-generate --dev

Now you can use this tool in your project.

Purpose

This tool allows you to declare routes using annotations and generate the corresponding configuration file with the following options:

export interface IAutoRoute {
  routeName: string;
  lazy?: boolean;
  keepAlive?: boolean;
  cacheWithParams?: Array<string>;
}

This configuration is used to generate the specified configuration, but it does not have a practical effect during runtime. The implementation is as follows:

export function autoGenerateRoute(options: IAutoRoute) {
  return (target: any) => target;
}

Usage

In your project, simply add the autoGenerateRoute annotation wherever you want to generate the route configuration, for example:

...
import { autoGenerateRoute } from 'react-route-generate';

@autoGenerateRoute({
  routeName: '/createArchive',
})
@inject(`app`)
@observer
export default class CreateArchivePage extends BasePage<CreateArchiveStore> {
    ...
}

By doing this, you have declared a route. Then, execute the following command in the project's root directory:

react-route-generate generate

You will obtain a route configuration file like this:

import React, { lazy } from 'react';
import CreateArchivePage from '@/pages/Archives/CreateArchive';

/**
 * This file is auto-generated by react-route-generate, do not modify.
 * 这个文件由插件自动生成,请不要修改。
 */

export const router = [
  {
    path: '/createArchive',
    component: CreateArchivePage,
  },
];

The annotation also supports React's lazy function. Simply set lazy to true:

...
import { autoGenerateRoute } from 'react-route-generate';

@autoGenerateRoute({
  keepAlive: true,
  routeName: '/archiveDetails',
  lazy: true,
})
@inject(`app`)
@observer
@withActivation
export default class ArchiveDetailsPage
  extends BasePage<ArchiveDetailsStore>
  implements IKeepAlive
{
    ...
}

The generated configuration will be as follows:

...
  {
    path: '/archiveDetails',
    component: lazy(() => import('@/pages/Archives/ArchiveDetails')),
    keepAlive: true,
  },
...

The plugin is only responsible for generating the configuration file. The usage of keepAlive and the specific implementation of the route is left to you.

Configuration

The tool has a configuration file named router.config.ts, which affects the behavior of the plugin:

const routeConfig = {
  /**
   * Scan file base path
   */
  baseDir: "src/pages",
  /**
   * Setup generate file output path
   */
  outputDir: "src/routes",
  /**
   * Config file output name. This file can be used in the project.
   */
  outFileName: "routeConfig",
  /**
   * Output file extension. The default is `.ts`.
   */
  outFileExt: "ts",
  /**
   * Load file match regex
   */
  match: "**/*.ts",
  /**
   * Replace alias
   */
  paths: {
      '@/': ['*']
  },

  /**
   * Base project URL, with tsconfig
   */
  baseUrl: 'src',

 

 /**
   * Project root directory, with tsconfig
   */
  rootDir: './' 
};
exports.default = routeConfig;
    
    

The configuration file is generated automatically when the plugin is first run, so you don't need to create it manually.

  • baseDir

    Set the directory where the plugin will search for files with annotations.

  • outDir

    Set the output directory for the generated files.

  • outFileName

    Set the name of the output file.

  • outFileExt

    Set the file type of the output file.

  • match

    Set the regular expression for matching annotated files.

  • paths

    Used to generate alias routes.

  • baseUrl

    Set the base directory for path generation. It can be set according to tsconfig.json.

  • rootDir

    Set the root directory for path generation. It can be set according to tsconfig.json.