Skip to content

Instantly share code, notes, and snippets.

@Tommos0
Last active June 8, 2021 14:11
Show Gist options
  • Save Tommos0/217c8457f40be60434aecf1f3f23c7a3 to your computer and use it in GitHub Desktop.
Save Tommos0/217c8457f40be60434aecf1f3f23c7a3 to your computer and use it in GitHub Desktop.
Instrument vite (rollup) React Typescript project with istanbul
/**
* Coverage instrumentation with working source maps.
* Use as rollup or vite plugin.
*/
import { createInstrumenter } from "istanbul-lib-instrument";
import { Plugin } from "rollup";
import { createFilter } from "@rollup/pluginutils";
const filter = createFilter("src/**/*.(js|jsx|ts|tsx)");
const plugin: Plugin = {
name: "istanbul",
transform(code, id) {
if (!filter(id)) {
return;
}
const instrumenter = createInstrumenter({
esModules: true,
compact: true,
produceSourceMap: true,
autoWrap: true,
preserveComments: true,
});
const combinedSourceMap = this.getCombinedSourcemap();
const transformedCode = instrumenter.instrumentSync(code, id, {
...combinedSourceMap,
version: String(combinedSourceMap.version),
});
const sourceMap = instrumenter.lastSourceMap();
return {
code: transformedCode,
map: { ...sourceMap, version: Number(sourceMap.version) },
};
},
};
export default plugin;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment