Last active
June 8, 2021 14:11
-
-
Save Tommos0/217c8457f40be60434aecf1f3f23c7a3 to your computer and use it in GitHub Desktop.
Instrument vite (rollup) React Typescript project with istanbul
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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