cheers-mp-plugin-router
读取 cheers-mp-router 路由库的路由配置,自动生成对应的小程序页面path数组并设置到app.json中
Last updated 22 days ago by xsasadasdasdasd .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install cheers-mp-plugin-router 
SYNC missed versions from official npm registry.

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" }

Current Tags

  • 1.6.0                                ...           latest (22 days ago)

4 Versions

  • 1.6.0                                ...           22 days ago
  • 1.4.0                                ...           5 months ago
  • 1.3.1                                ...           5 months ago
  • 1.3.0                                ...           5 months ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 4
Last Day 0
Last Week 4
Last Month 14
Dependencies (4)
Dev Dependencies (0)
None
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |