Skip to Content
DocumentationEcosystem@canyonjs/babel-plugin

@canyonjs/babel-plugin

Source

一个用于检测 CI 环境变量的 Babel 插件。与 istanbuljs 配合使用,完成代码插桩。

使用方法

安装:

npm install --save-dev @canyonjs/babel-plugin

babel.config.js 中添加这些配置:

module.exports = { plugins: process.env.CI_COMMIT_REF_NAME === "test-coverage" ? ["istanbul", "canyon"] : [], // 注意插件顺序:canyon 插件应在 istanbul 插件之后 };

它做了两件事:

  1. 检测 CI 流水线变量
  2. 收集参与编译的文件覆盖率初始数据

配置

babel.config.js

module.exports = { plugins: [ "istanbul", [ "canyon", { // #region == Step 2: CI Provider auto-detection, generally no manual configuration needed, see Support Provider documentation for details repoID: "230614", // Repository ID sha: "xxxxxxxxx", // Git Commit SHA // #endregion // #region == Step 4: Separate hit and map data (optional) keepMap: false, // Keep coverage map, optional, default is false. // #endregion // #region == Step 5: Other configuration (optional) instrumentCwd: "/path/to", // Instrumentation working directory, may need manual configuration in multi-repo mode provider: "gitlab", // Source code provider (optional), default is gitlab include: ["src/**/*.ts", "src/**/*.tsx"], // Include glob patterns (optional) exclude: ["**/*.test.ts", "**/*.spec.ts"], // Exclude glob patterns (optional) extensions: [".ts", ".tsx", ".js"], // Extensions participating in include/exclude matching (optional) // #endregion }, ], ], };
配置项描述是否必填默认值
repoID仓库 ID一般无需手动配置(自动检测 CI Provider)None
shaGit 提交 SHA一般无需手动配置(自动检测 CI Provider)None
branchGit 仓库分支一般无需手动配置(自动检测 CI Provider)None
keepMap保留覆盖率地图,可选,默认为 true,当为 false 时,将生成 .canyon_output 文件可选true
instrumentCwd插桩工作目录,在多仓库模式下可能需要手动配置可选process.cwd()
provider源代码提供商(可选),默认为 gitlab可选gitlab
include需要插桩的文件匹配规则(glob),语义与 babel-plugin-istanbul 一致可选[]
exclude不需要插桩的文件匹配规则(glob),语义与 babel-plugin-istanbul 一致可选[]
extensionsinclude/exclude 匹配时使用的扩展名列表可选.js/.ts/…