Skip to content

Commit

Permalink
fix: Added variable for turning off NODE_ENV check
Browse files Browse the repository at this point in the history
  • Loading branch information
iFaxity committed Oct 20, 2021
1 parent 6d074a7 commit fdbfbc5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Creates the vite plugin from a set of optional plugin options.
* `opts.extension {string|string[]}` - Optional string or array of strings of extensions to include (dot prefixed like .js or .ts). By default this is set to `['.js', '.cjs', '.mjs', '.ts', '.tsx', '.jsx', '.vue']`
* `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`

Examples
--------------------------
Expand Down
13 changes: 8 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ interface IstanbulPluginOptions {
extension?: string|string[];
requireEnv?: boolean;
cypress?: boolean;
checkProd?: boolean;
}

// Required for typing to work in createConfigureServer()
Expand Down Expand Up @@ -85,13 +86,15 @@ function createTransform(opts: IstanbulPluginOptions = {}): TransformHook {
function istanbulPlugin(opts: IstanbulPluginOptions = {}): Plugin {
// Only instrument when we want to, as we only want instrumentation in test
// By default the plugin is always on
const env = opts.cypress ? process.env.CYPRESS_COVERAGE : process.env.VITE_COVERAGE;
const requireEnv = opts.requireEnv ?? false;
const env = (opts.cypress ? process.env.CYPRESS_COVERAGE : process.env.VITE_COVERAGE);
const envValue = env?.toLowerCase();
const requireEnv = opts?.requireEnv ?? false;
const prodCheck = opts?.checkProd ?? true;

if (
process.env.NODE_ENV == 'production' ||
env?.toLowerCase() === 'false' ||
(requireEnv && env?.toLowerCase() !== 'true')
(prodCheck && process.env.NODE_ENV?.toLowerCase() === 'production') ||
(!requireEnv && envValue === 'false') ||
(requireEnv && envValue !== 'true')
) {
return { name: PLUGIN_NAME };
}
Expand Down

0 comments on commit fdbfbc5

Please sign in to comment.