Skip to content

Commit

Permalink
feat: Support instrument in build mode
Browse files Browse the repository at this point in the history
  • Loading branch information
jordan-boyer committed Feb 8, 2022
1 parent 7a4836e commit 90b7e8b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Creates the vite plugin from a set of optional plugin options.
* `opts.requireEnv {boolean}` - Optional boolean to require env to be true to instrument to code, otherwise it will instrument even if env variable is not set.
* `opts.cypress {boolean}` - Optional boolean to change the env to CYPRESS_COVERAGE instead of VITE_COVERAGE. For ease of use with @cypress/code-coverage.
* `opts.checkProd {boolean}` - Optional boolean to enforce the plugin to skip instrumentation for production environments, checks *NODE_ENV* for "production" (case insensitive). Defaults to true.
* `opts.forceBuildInstrument {boolean}` - Optional boolean to enforce the plugin to add instrumentation in build mode. Defaults to false.

Examples
--------------------------
Expand Down
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ interface IstanbulPluginOptions {
cypress?: boolean;
checkProd?: boolean;
cwd?: string;
forceBuildInstrument?: boolean;
}

// Custom extensions to include .vue files
Expand All @@ -36,6 +37,7 @@ export = function istanbulPlugin(opts: IstanbulPluginOptions = {}): Plugin {
// By default the plugin is always on
const requireEnv = opts?.requireEnv ?? false;
const checkProd = opts?.checkProd ?? true;
const forceBuildInstrument = opts?.forceBuildInstrument ?? false
const logger = createLogger('warn', { prefix: 'vite-plugin-istanbul' });
const exclude = new TestExclude({
cwd: opts.cwd ?? process.cwd(),
Expand All @@ -57,7 +59,7 @@ export = function istanbulPlugin(opts: IstanbulPluginOptions = {}): Plugin {

return {
name: PLUGIN_NAME,
apply: 'serve', // only apply for live service
apply: forceBuildInstrument ? 'build' : 'serve',
// istanbul only knows how to instrument JavaScript,
// this allows us to wait until the whole code is JavaScript to
// instrument and sourcemap
Expand All @@ -78,7 +80,7 @@ export = function istanbulPlugin(opts: IstanbulPluginOptions = {}): Plugin {
const { CYPRESS_COVERAGE, VITE_COVERAGE } = config.env;
const env = (opts.cypress ? CYPRESS_COVERAGE : VITE_COVERAGE)?.toLowerCase();

if ((checkProd && isProduction) ||
if ((checkProd && isProduction && !forceBuildInstrument) ||
(!requireEnv && env === 'false') ||
(requireEnv && env !== 'true')) {
enabled = false;
Expand Down

0 comments on commit 90b7e8b

Please sign in to comment.