babel-plugin-istanbul

中文文档
8.0.0发布于 2026/04/09BSD-3-ClauseCommonJS近 30 天下载 2.2亿

A babel plugin that adds istanbul instrumentation to ES6 code

原始使用文档
文档目录8 节
  1. Usage
  2. Integrations
  3. karma
  4. mocha on node.js (through nyc)
  5. Ignoring files
  6. Source Maps
  7. Credit where credit is due
  8. babel-plugin-istanbul for enterprise

babel-plugin-istanbul

Coverage Status Conventional Commits community slack

Having problems? want to contribute? join our community slack.

A Babel plugin that instruments your code with Istanbul coverage. It can instantly be used with karma-coverage and mocha on Node.js (through nyc).

Note: This plugin does not generate any report or save any data to any file; it only adds instrumenting code to your JavaScript source code. To integrate with testing tools, please see the Integrations section.

Usage

Install it:

npm install --save-dev babel-plugin-istanbul

Add it to .babelrc in test mode:

{
  "env": {
    "test": {
      "plugins": [ "istanbul" ]
    }
  }
}

Optionally, use cross-env to set NODE_ENV=test:

{
  "scripts": {
    "test": "cross-env NODE_ENV=test nyc --reporter=lcov --reporter=text mocha test/*.js"
  }
}

Integrations

karma

It just works with Karma. First, make sure that the code is already transpiled by Babel (either using karma-babel-preprocessor, karma-webpack, or karma-browserify). Then, simply set up karma-coverage according to the docs, but don’t add the coverage preprocessor. This plugin has already instrumented your code, and Karma should pick it up automatically.

It has been tested with bemusic/bemuse project, which contains ~2400 statements.

mocha on node.js (through nyc)

Configure Mocha to transpile JavaScript code using Babel, then you can run your tests with nyc, which will collect all the coverage report.

babel-plugin-istanbul respects the include/exclude configuration options from nyc, but you also need to configure NYC not to instrument your code by adding these settings in your package.json:

  "nyc": {
    "sourceMap": false,
    "instrument": false
  },

Ignoring files

You don't want to cover your test files as this will skew your coverage results. You can configure this by providing plugin options matching nyc's exclude/include rules:

{
  "env": {
    "test": {
      "plugins": [
        ["istanbul", {
          "exclude": [
            "**/*.spec.js"
          ]
        }]
      ]
    }
  }
}

If you don't provide options in your Babel config, the plugin will look for exclude/include config under an "nyc" key in package.json.

You can also use istanbul's ignore hints to specify specific lines of code to skip instrumenting.

Source Maps

By default, this plugin will pick up inline source maps and attach them to the instrumented code such that code coverage can be remapped back to the original source, even for multi-step build processes. This can be memory intensive. Set useInlineSourceMaps to prevent this behavior.

{
  "env": {
    "test": {
      "plugins": [
        ["istanbul", {
          "useInlineSourceMaps": false
        }]
      ]
    }
  }
}

If you're instrumenting code programatically, you can pass a source map explicitly.

import babelPluginIstanbul from 'babel-plugin-istanbul';

function instrument(sourceCode, sourceMap, filename) {
  return babel.transform(sourceCode, {
    filename,
    plugins: [
      [babelPluginIstanbul, {
        inputSourceMap: sourceMap
      }]
    ]
  })
}

Credit where credit is due

The approach used in babel-plugin-istanbul was inspired by Thai Pangsakulyanont's original library babel-plugin-__coverage__.

babel-plugin-istanbul for enterprise

Available as part of the Tidelift Subscription.

The maintainers of babel-plugin-istanbul and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. Learn more.

版本与发布记录

babel-plugin-istanbul 共发布 42 个版本,首次发布于 2016/06/23;过去 12 个月发布了 3 个版本。

版本发布日期距今
8.0.0latest2026/04/096 个月前
8.0.22026/04/096 个月前
8.0.12026/04/096 个月前
7.0.12025/09/061.2 年前
7.0.02024/07/052 年前
6.1.12021/10/175 年前

关于 babel-plugin-istanbul 的常见问题

babel-plugin-istanbul 是什么?

A babel plugin that adds istanbul instrumentation to ES6 code

如何安装 babel-plugin-istanbul?

在项目目录运行 npm install babel-plugin-istanbul,或使用 pnpm add / yarn add / bun add。

babel-plugin-istanbul 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/babel-plugin-istanbul 是否存在。

babel-plugin-istanbul 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

babel-plugin-istanbul 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

babel-plugin-istanbul 可以商用吗?

babel-plugin-istanbul 使用 BSD-3-Clause 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。另外不得用原作者名义为衍生产品背书。以上为概括,不构成法律意见,请以许可原文为准。

babel-plugin-istanbul 的下载量有多少?

根据 npm 官方下载统计,babel-plugin-istanbul 在 2026/10/05 – 2026/11/03 这 30 天内被下载 224,548,317 次,日均约 7,484,944 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

babel-plugin-istanbul 还在维护吗?

最新版本 8.0.0 发布于 2026/04/09(6 个月前);至今共发布 42 个版本,其中过去 12 个月发布了 3 个;首次发布于 2016/06/23。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。