{"id":26891870,"url":"https://github.com/mickeyjsx/umi-plugin-runtime-routes","last_synced_at":"2025-03-31T22:41:56.309Z","repository":{"id":35066248,"uuid":"202531953","full_name":"mickeyjsx/umi-plugin-runtime-routes","owner":"mickeyjsx","description":"☀️ Modify routes generated by umi at runtime.","archived":false,"fork":false,"pushed_at":"2022-12-09T22:38:08.000Z","size":915,"stargazers_count":1,"open_issues_count":11,"forks_count":0,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-17T14:39:45.036Z","etag":null,"topics":["plugin","routes","runtime","umi"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mickeyjsx.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-08-15T11:50:02.000Z","updated_at":"2021-11-16T10:55:02.000Z","dependencies_parsed_at":"2023-01-15T13:15:24.680Z","dependency_job_id":null,"html_url":"https://github.com/mickeyjsx/umi-plugin-runtime-routes","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mickeyjsx%2Fumi-plugin-runtime-routes","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mickeyjsx%2Fumi-plugin-runtime-routes/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mickeyjsx%2Fumi-plugin-runtime-routes/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mickeyjsx%2Fumi-plugin-runtime-routes/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mickeyjsx","download_url":"https://codeload.github.com/mickeyjsx/umi-plugin-runtime-routes/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246552907,"owners_count":20795836,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["plugin","routes","runtime","umi"],"created_at":"2025-03-31T22:41:55.190Z","updated_at":"2025-03-31T22:41:56.297Z","avatar_url":"https://github.com/mickeyjsx.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# umi-plugin-runtime-routes\nModify routes generated by umi at runtime.\n\n## Install\n\n```bash\nyarn add umi-plugin-runtime-routes -D\n```\n\n## Usage\n\n### 1. Define a routes `Modifier` function\n\nRoutes 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 modified`routes`. \n\nYou could define `Modifier` by yourself, also this plugin provides some built-in `helper` functions and `utils` to help you to create Modifier easier.\n\nFor example, pick the `envHelper` from the built-in helper functions to create our sample Modifier:\n\n```javascript\n// routesModifer.js\nimport { helpers } from 'umi-plugin-runtime-routes';\n\nconst envHelper = helpers.envHelper;\n\nexport default function(routes) {\n  // Maybe in your case, you are not using \"window.currentEnv\" at runtime, this is just a sample\n  const modifiedRoutes = envHelper(routes, window.currentEnv);\n  \n  return modifiedRoutes;\n}\n```\n\n### 2. Add extra information in `routes` option in `.umirc.js`\n\nThe 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`:\n\n```javascript\n// .umirc.js\n\nexport default {\n  routes: [\n    {\n      path: '/',\n      component: '../layouts/index',\n      routes: [\n        { path: '/user', redirect: '/user/login', env: ['foo', 'bar'] },\n        { path: '/user/login', redirect: './user/login', env: ['bar'] },\n      ],\n    },\n  ],\n};\n```\n\n### 3. Configure plugin in `.umirc.js`\n\nConfigure this plugin in `.umirc.js`.\n\n```javascript\n// .umirc.js\n\nexport default {\n  routes: [/* ... */];\n  plugins: ['umi-plugin-runtime-routes', { modifier: 'path/to/routesModifier' }],\n};\n```\n\n## Options\n\n### options.modifier\n\n`options.modifier ` is a relative path string to the `Modifier` module, the base path is current working directory.\n\n#### Modifer\n\nA function used for modifying routes at runtime, like this:\n\n```javascript\nfunction routesModifier(routes) {\n  const modifiedRoutes = // ... modify routes\n  return modifiedRoutes;\n}\n```\n\nTo help you create Modifier function easier, this plugin provides some built-in helper functions and some utils, see it at below.\n\n## Helpers\n\nAll of the built-in helpers are exported at `helpers` variable.\n\n```javascript\nimport { helpers } from 'umi-plugin-runtime-routes';\n```\n\n### envHelper(routes, currentEnv)\n\nThis helper help you to modify routes by the application running environment.\n\n1\\. Create modifier function\n\n```javascript\n// routesModifier.js\nimport { helpers } from 'umi-plugin-runtime-routes';\n\nfunction routesModifier(routes) {\n  const modifiedRoutes = helpers.envHelper(routes, 'foo'); // 'foo' is current env\n  return modifiedRoutes;\n}\n```\n\n2\\. Define routes:\n\n```javascript\n// .umirc.js\n\nexport default {\n  routes: [\n    { /* ... */, env: ['foo', 'bar'] } // add 'env' array at each route\n  ]\n};\n```\n\n## Utils\n\nAll of the utils are exported at `utils` variable.\n\n```javascript\nimport { utils } from 'umi-plugin-runtime-routes';\n```\n\n### routesFilter(routes, callback)\n\nWhen 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.\n\nThe 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.\n\nYou can create the modifier like this:\n\n```javascript\n// routesModifier.js\nimport { utils } from 'umi-plugin-runtime-routes';\n\nexport default function(routes) {\n  const modifiedRoutes = utils.routesFilter(routes, (route) =\u003e {\n    // return true or false to do the filter\n  });\n  \n  return modifiedRoutes;\n}\n```\n\n## License\n\n[MIT License](https://github.com/mickeyjsx/umi-plugin-runtime-routes/blob/master/LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmickeyjsx%2Fumi-plugin-runtime-routes","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmickeyjsx%2Fumi-plugin-runtime-routes","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmickeyjsx%2Fumi-plugin-runtime-routes/lists"}