Seamless integration between Rollup and Istanbul code coverage instrumentation.
npx @tessl/cli install tessl/npm-rollup-plugin-istanbul@5.0.0A Rollup plugin that seamlessly integrates Istanbul code coverage instrumentation into the Rollup bundling process. The plugin instruments JavaScript source code for coverage analysis while excluding test files from instrumentation, making it ideal for projects that need to bundle both application code and tests separately.
npm install --save-dev rollup-plugin-istanbulimport istanbul from 'rollup-plugin-istanbul';For CommonJS:
const istanbul = require('rollup-plugin-istanbul');import { rollup } from 'rollup';
import istanbul from 'rollup-plugin-istanbul';
// Basic configuration
rollup({
entry: 'main.js',
plugins: [
istanbul({
exclude: ['test/**/*.js']
})
]
}).then(...)
// With custom instrumenter configuration
rollup({
entry: 'main.js',
plugins: [
istanbul({
include: ['src/**/*.js'],
exclude: ['test/**/*.js', 'spec/**/*.js'],
instrumenterConfig: {
esModules: true,
compact: false,
produceSourceMap: true
}
})
]
})Creates a Rollup plugin instance for Istanbul code coverage instrumentation.
/**
* Creates a Rollup plugin instance for Istanbul code coverage instrumentation.
*
* @param {Object} [options={}] - Configuration options for the plugin
* @param {string|string[]} [options.include] - Minimatch pattern(s) for files to include
* @param {string|string[]} [options.exclude] - Minimatch pattern(s) for files to exclude
* @param {Object} [options.instrumenterConfig] - Configuration passed to Istanbul instrumenter
* @param {boolean} [options.instrumenterConfig.esModules=true] - Enable ES module instrumentation
* @param {boolean} [options.instrumenterConfig.compact=true] - Generate compact instrumented code
* @param {boolean} [options.instrumenterConfig.produceSourceMap=true] - Generate source maps
* @param {boolean} [options.instrumenterConfig.autoWrap=true] - Automatically wrap code
* @param {boolean} [options.instrumenterConfig.preserveComments=true] - Preserve comments
* @param {Object} [options.instrumenter] - Alternative instrumenter implementation
* @returns {Object} Rollup plugin object with name and transform properties
*/
function istanbul(options);
// Plugin object returned by istanbul function
// {
// name: string, // Plugin name ("istanbul")
// transform: function // Transform function for code instrumentation
// }
// Transform function signature:
// transform(code: string, id: string) -> { code: string, map: object } | undefinedUsage Examples:
// Exclude test files from coverage
istanbul({
exclude: ['test/**/*.js', 'spec/**/*.js']
})
// Include only specific source files
istanbul({
include: ['src/**/*.js', 'lib/**/*.js'],
exclude: ['src/**/*.test.js']
})
// Custom instrumenter configuration
istanbul({
exclude: ['test/**/*.js'],
instrumenterConfig: {
esModules: true,
compact: true,
produceSourceMap: true,
autoWrap: true,
preserveComments: true
}
})
// Using with custom instrumenter (e.g., isparta)
istanbul({
exclude: ['test/**/*.js'],
instrumenter: require('isparta')
})Minimatch pattern(s) for files to include in instrumentation. If omitted, all files are included by default.
string | string[]'src/**/*.js', ['src/**/*.js', 'lib/**/*.js']Minimatch pattern(s) for files to exclude from instrumentation. Commonly used to exclude test files.
string | string[]'test/**/*.js', ['test/**/*.js', 'spec/**/*.js']Configuration options passed to the Istanbul instrumenter.
Object{
esModules: true,
compact: true,
produceSourceMap: true,
autoWrap: true,
preserveComments: true
}Available instrumenter configuration options:
esModules: Enable ES module instrumentationcompact: Generate compact instrumented codeproduceSourceMap: Generate source maps for instrumented codeautoWrap: Automatically wrap code in anonymous functionpreserveComments: Preserve comments in instrumented codeAlternative instrumenter implementation that implements the Istanbul API.
Objectistanbul-lib-instrumentrequire('isparta') for ES6+ transpilation// karma.conf.js
const istanbul = require('rollup-plugin-istanbul');
module.exports = function (config) {
config.set({
files: ['test/*.js'],
preprocessors: {
'test/*.js': ['rollup']
},
rollupPreprocessor: {
rollup: {
plugins: [
istanbul({
exclude: ['test/*.js']
})
]
}
},
reporters: ['coverage']
});
};// karma.conf.js
const istanbul = require('rollup-plugin-istanbul');
const babel = require('@rollup/plugin-babel').babel;
module.exports = function (config) {
config.set({
files: ['test/*.js'],
preprocessors: {
'test/*.js': ['rollup']
},
rollupPreprocessor: {
plugins: [
istanbul({
exclude: ['test/*.js']
}),
babel({ babelHelpers: 'bundled' })
],
output: {
format: 'iife',
sourceMap: 'inline'
}
},
reporters: ['coverage'],
coverageReporter: {
dir: 'coverage',
includeAllSources: true,
reporters: [
{ type: 'text' },
{ type: 'html', subdir: 'html' },
{ type: 'lcov', subdir: './' }
]
}
});
};The plugin integrates into Rollup's transform pipeline with the following behavior:
@rollup/pluginutils.createFilter to determine which files to instrument based on include/exclude patternsThe plugin returns undefined for files that don't match the filter criteria, allowing them to pass through unchanged.