Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Docs for automatic sourcemapping #20

Merged
merged 2 commits into from
Mar 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ vite-plugin-istanbul
[![npm bundle size (scoped)](https://img.shields.io/bundlephobia/min/vite-plugin-istanbul?label=Bundle%20size&style=for-the-badge)](https://npmjs.org/package/vite-plugin-istanbul)
[![npm bundle size (scoped)](https://img.shields.io/bundlephobia/minzip/vite-plugin-istanbul?label=Bundle%20size%20%28gzip%29&style=for-the-badge)](https://npmjs.org/package/vite-plugin-istanbul)

A Vite plugin to instrument your code for nyc/istanbul code coverage. In similar way as the Webpack Loader istanbul-instrumenter-loader. Only intended for use in development while running tests.
A Vite plugin to instrument your code for nyc/istanbul code coverage. In similar way as the Webpack Loader `istanbul-instrumenter-loader`. Only intended for use in development while running tests.

Version v2.x for Vite v2.0, for Vite v1.0 install v1.x of this plugin.

As of v2.1.0 you can toggle the coverage off by setting the environment variable `VITE_COVERAGE='false'`, by default it will always instrument the code. To require the explicit definition of the variable, set the option `requireEnv` to **true**.
However only the v2 version is actively tested and developed.

Installation
--------------------------
Expand Down Expand Up @@ -44,6 +43,15 @@ Creates the vite plugin from a set of optional plugin options.
* `opts.checkProd {boolean}` - Optional boolean to enforce the plugin to skip instrumentation for production environments. Looks at Vite's **isProduction** key from the `ResolvedConfig`.
* `opts.forceBuildInstrument {boolean}` - Optional boolean to enforce the plugin to add instrumentation in build mode. Defaults to false.

Notes
--------------------------

As of v2.1.0 you can toggle the coverage off by setting the env variable `VITE_COVERAGE='false'`, by default it will always instrument the code. To require the explicit definition of the variable, set the option `requireEnv` to **true**.

This plugin also requires the Vite configuration [build.sourcemap](https://vitejs.dev/config/#build-sourcemap) to be set to either **true**, **'inline'**, **'hidden'**.
But the plugin will automatically default to **true** if it is missing in order to give accurate cove coverage.
The plugin will notify when this happens in order for a developer to fix it. This notification will show even when the plugin is disabled by e.g `opts.requireEnv`, `VITE_COVERAGE=false`. This is due to a limitation of the API for this kind of feature.

Examples
--------------------------

Expand Down
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@ export = function istanbulPlugin(opts: IstanbulPluginOptions = {}): Plugin {
// instrument and sourcemap
enforce: 'post',
config(config) {
if (config.build?.sourcemap != true) {
logger.warn(`${PLUGIN_NAME}> ${yellow('Sourcemaps not enabled and will be automatically enabled for code coverage to be accurate.')}`);
// If sourcemap is not set (either undefined or false)
if (!config.build?.sourcemap) {
logger.warn(`${PLUGIN_NAME}> ${yellow(`Sourcemaps was automatically enabled for code coverage to be accurate.
To hide this message set build.sourcemap to true, 'inline' or 'hidden'.`)}`);

// Enforce sourcemapping,
config.build = config.build || {};
Expand All @@ -92,6 +94,7 @@ export = function istanbulPlugin(opts: IstanbulPluginOptions = {}): Plugin {
}

// Returns the current code coverage in the global scope
// Used if an external endpoint is required to fetch code coverage
middlewares.use((req, res, next) => {
if (req.url !== COVERAGE_PUBLIC_PATH) {
return next();
Expand Down