Skip to content

Commit

Permalink
Merge pull request #42 from iFaxity/feature/support-envprefix
Browse files Browse the repository at this point in the history
feat: Added support for envPrefix
  • Loading branch information
iFaxity authored Jun 7, 2022
2 parents 9a266d4 + 806c91d commit 5265ed2
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ function sanitizeSourceMap(rawSourceMap: ExistingRawSourceMap): ExistingRawSourc
return JSON.parse(JSON.stringify(sourceMap));
}

function getEnvVariable(key: string, prefix: string|string[], env: Record<string, any>): string {
if (Array.isArray(prefix)) {
const envPrefix = prefix.find(pre => {
const prefixedName = `${pre}${key}`;

return env[prefixedName] != null;
});

prefix = envPrefix ?? '';
}

return env[`${prefix}${key}`];
}

export = 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
Expand Down Expand Up @@ -83,14 +97,18 @@ export = function istanbulPlugin(opts: IstanbulPluginOptions = {}): Plugin {
configResolved(config) {
// We need to check if the plugin should enable after all configuration is resolved
// As config can be modified by other plugins and from .env variables
const { isProduction } = config;
const { isProduction, env } = config;
const { CYPRESS_COVERAGE } = process.env;
const { VITE_COVERAGE } = config.env;
const env = (opts.cypress ? CYPRESS_COVERAGE : VITE_COVERAGE)?.toLowerCase();
const envPrefix = config.envPrefix ?? 'VITE_';

const envCoverage = opts.cypress
? CYPRESS_COVERAGE
: getEnvVariable('COVERAGE', envPrefix, env);
const envVar = envCoverage?.toLowerCase() ?? '';

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

0 comments on commit 5265ed2

Please sign in to comment.