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

role-xml-generate

v1.0.13

Published

ROLE XML GENERATE

Downloads

5

Readme

用法

安装 role-xml-generate

npm i role-xml-generate -D

命令

// 该命令会为指定的项目路由入口文件编译依赖最后一个node执行文件(server.js),该执行文件执行后会生成给rxg start使用的路由入口文件(role.config.js中的routeEntry文件,生成的默认文件名为route.entry.js)
// 注入变量名称为ROUTE_ENTRY
例如
cross-env ROUTE_ENTRY='../../tmos/src/config/package_name/index.ts' rxg route
// 根据role.config.js配置文件生成所需xml文件
npx rxg start

如何在项目中使用

项目中使用方法为配置 script 执行脚本,如下 其意思为编译ROUTE_ENTRY以及其依赖生成 node 执行文件(server.js),然后执行server.js生成role.config.js配置文件中routeEntry配置项所需的文件(route.entry.js),然后使用route start命令启动 xml 工具生成 xml 文件 执行过程中生成的(server.js)和(route.entry.js)会保存在执行命令的目录下的临时目录.role中,执行结束后会自动删除,可以在role.config.js中配置keepTemporarilyFile保存临时文件

  "scripts": {
    "generate:xml": "cross-env ROUTE_ENTRY='../../tmos/src/config/package_name/index.ts' rxg route && node ./.role/server.js && rxg start"
  },

使用npm run generate:xml执行

配置文件 role.config.js

以下为默认配置

module.exports = {
  // webpack配置文件入口名称,主要是为了获取alias
  webpackEntry: 'ice.config.js',
  // 默认从webpackEntry中拿,但是引入webpackEntry文件报错时使用这个配置
  alias: {},
  // 路由配置文件入口名称
  routeEntry: 'route.js',
  // 将该xml文件内容拼接到生成的xml文件当中
  mergeXmlFile: '',
  // api可能会在的页面名称
  apiFileNames: ['model', 'store'],
  // 收集页面依赖文件时需要匹配的文件类型后缀
  extensions: ['js', 'ts', 'tsx', 'jsx'],
  // node_modules中需要进行匹配的模块  Array
  node_modules: [],
  // xml标签名称配置
  xml: {
    // 生成的xml文件精确等级 menu 0 | api 1 | ...
    level: 1,
    // 根节点名称
    rootName: 'tsie-config',
    // api匹配到的次数大于该数值时提取为公共api
    minCount: 1,
    // 主要用于生成的xml文件的标签名称和属性名称
    nodes: [
      {
        name: 'menu',
        attrs: {
          name: 'name',
          id: 'id',
          menupath: 'menuPath',
        },
      },
      {
        name: 'api',
        attrs: {
          name: 'name',
          id: 'id',
          url: 'url',
          method: 'method',
        },
      },
    ],
    // xml属性拼接规则,key为需要拼接的属性,value为拼接规则
    spliceRule: {
      id: '_',
      path: '',
    },
  },
  // 输出xml文件名称
  outputFileName: 'role-config.xml',
  // 自定义变量  暂时只有PACKAGE_NAME
  define: {},
  // 保留临时文件
  keepTemporarilyFile: false,
  /**
   *   获取依赖文件路径时的ast抽象树额外匹配规则,默认只匹配使用import导入的文件 import {} from '' | import()
   *    像在单个页面当中使用React.Lazy...等特殊的懒加载方法需要自己添加匹配规则
   *  
   * 例如匹配 import()
   *  (context, tools, deps, next) => {
        const { getExistAbsPath, isHandlerPath, isModelPath, parentPath } = tools;
        return {
          CallExpression(path) {
            if (path.node.callee.type !== 'Import') return;
            const importPath = path.node.arguments[0].value;
            const key = getExistAbsPath(importPath, require('path').resolve(parentPath, '..'), context);
            if (key && !deps[key] && isHandlerPath(key, context)) {
              deps[key] = importPath;
              next(key);
            }
          },
        };
      },
   */
  depsFileMatch: () => ({}),
};

匹配错误收集

工具在执行过程中会匹配 API 时会匹配所有与model相关文件中的createAjaxModel,从中匹配出url,method,name字段用于生成 xml,在匹配过程中 url 字段可能有不同的写法,工具中兼容了三种匹配规则:

1.字符串

const model = createAjaxModel({
  name: 'name...',
  url: 'url',
  method: 'post',
  feedback: staticFeedback,
  state: {
    data: [],
  },
});

2.对象

export const model = createAjaxModel({
  name: 'name...',
  method: 'get',
  url:
    {
      package_name: 'url',
    }[PACKAGE_NAME] || 'url',
  feedback: staticFeedback,
});

3.函数

export const model = createAjaxModel({
  name: 'name...',
  method: 'get',
  url: (() => {
    const apiMap = {
      package_name1: 'url1',
      package_name2: 'url2',
      package_name3: 'url3',
      default: 'url',
    };
    return apiMap[PACKAGE_NAME] || apiMap.default;
  })(),
  feedback: staticFeedback,
});

以上三种 url 使用方式之外都会报错,错误信息会被收集,然后将此处的 url 字段转换为url: 'can_not_match_url',在 xml 文件生成后会将错误信息打印出来,此时需要根据错误信息到对应的文件夹查找对应modelurl自行判断然后在 xml 文件中进行替换

输出 xml 文件后若有警告 ⚠️ 信息,则需要查看信息,根据提示对 xml 文件中某些字段进行手动替换 错误信息如下

解析错误文件路径:
page_path/store.ts

WARN 解析以上文件中url字段进行字段替换时出错,url字段的值将会替换为can_not_match_url,请在/path/tmos-front-end/role-config.xml文件中全局搜索自行进行替换