cheers-mp-plugin-router

1.9.0 • Public • Published

cheers-mp-plugin-router

babal AST 解析路由配置,提取 path 到 app.json,搭配cheers-mp-router使用

使用

cheers-mp 脚手架中安装即启用

npm i cheers-mp-plugin-router -D

示例

假设路由配置文件所在文件路径是: src/service/router.ts router.ts 文件中有如下代码定义:

import Router, { RouteConfig } from "cheers-mp-router";

const routeConfigList: RouteConfig[] = [
  // 主包路由
  { name: "entry", path: "pages/entry/index" },
  { name: "store-home", path: "pages/tabbar/store-home/index", isTab: true },
  // 子包 packageA
  { name: "test", path: "packageA/pages/test/index" },
  { name: "test2", path: "packageA/pages/test2/index" },
  // 独立子包 ipackageB
  { name: "test3", path: "ipackageB/pages/test3/index" },
];

const router = new Router({ routes: routeConfigList });
// router.beforeEach((to, from, next) => {
//     // console.log("当前路由", from);
//     // console.log("即将前往的路由", to);
//     // console.log("beforeEach1");
//     // setTimeout(() => {
//     //   if (to.name === "product-details") {
//     //     console.log("拦截前往商品详情的请求,转到搜索页面");
//     //     next({
//     //       name: "test"
//     //     });
//     //   } else {
//     //     console.log("放行");
//     //     next();
//     //   }
//     // }, 1500);
//     next();
// });

export default router;

那么我们可以在cheers-config.js配置:

modeule.export = {
  pluginOptions: {
    //  路由配置文件所在的路径
    router: "src/service/router.ts",
  },
};

最终本插件会解析出来router.ts文件中的 path 配置为:

{
  "pages": ["pages/entry/index", "pages/tabbar/store-home/index"],
  "subpackages": [
    {
      "root": "packageA",
      "pages": ["pages/test/index", "pages/test2/index"],
      "independent": false
    },
    {
      "root": "ipackageB",
      "pages": ["pages/test3/index"],
      "independent": true
    }
  ]
}

插件会将此配置更新到src/app.json文件中.

按照约定大于配置的原则:

  • 如果path路径名字以package 开头,则此目录视为子包,例如:{ name: "test", path: "packageA/pages/test/index" }
  • 如果path路径名字以ipackage 开头,则此目录视为独立子包,例如: { name: "test3", path: "ipackageB/pages/test3/index" }
  • 其他路由视为主包,例如:{ name: "entry", path: "pages/entry/index" }

Readme

Keywords

Package Sidebar

Install

npm i cheers-mp-plugin-router

Weekly Downloads

1

Version

1.9.0

License

MIT

Unpacked Size

6.58 kB

Total Files

3

Last publish

Collaborators

  • xsasadasdasdasd