@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 插件之后
};它做了两件事:
- 检测 CI 流水线变量
- 收集参与编译的文件覆盖率初始数据
配置
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 |
| sha | Git 提交 SHA | 一般无需手动配置(自动检测 CI Provider) | None |
| branch | Git 仓库分支 | 一般无需手动配置(自动检测 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 一致 | 可选 | [] |
| extensions | include/exclude 匹配时使用的扩展名列表 | 可选 | .js/.ts/… |