diff --git a/src/index.ts b/src/index.ts index 7fcf88b..05a792f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,10 +1,10 @@ -import type { ExistingRawSourceMap } from 'rollup'; -import { Plugin, TransformResult, createLogger } from 'vite'; -import { createInstrumenter } from 'istanbul-lib-instrument'; -import TestExclude from 'test-exclude'; import { loadNycConfig } from '@istanbuljs/load-nyc-config'; +import { createInstrumenter } from 'istanbul-lib-instrument'; import picocolors from 'picocolors'; -import {createIdentitySourceMap} from "./source-map"; +import type { ExistingRawSourceMap } from 'rollup'; +import TestExclude from 'test-exclude'; +import { Plugin, TransformResult, createLogger } from 'vite'; +import { createIdentitySourceMap } from './source-map'; const { yellow } = picocolors; @@ -14,9 +14,9 @@ declare global { } export interface IstanbulPluginOptions { - include?: string|string[]; - exclude?: string|string[]; - extension?: string|string[]; + include?: string | string[]; + exclude?: string | string[]; + extension?: string | string[]; requireEnv?: boolean; cypress?: boolean; checkProd?: boolean; @@ -40,9 +40,9 @@ function sanitizeSourceMap(rawSourceMap: ExistingRawSourceMap): ExistingRawSourc return JSON.parse(JSON.stringify(sourceMap)); } -function getEnvVariable(key: string, prefix: string|string[], env: Record): string { +function getEnvVariable(key: string, prefix: string | string[], env: Record): string { if (Array.isArray(prefix)) { - const envPrefix = prefix.find(pre => { + const envPrefix = prefix.find((pre) => { const prefixedName = `${pre}${key}`; return env[prefixedName] != null; @@ -77,11 +77,51 @@ async function createTestExclude(opts: IstanbulPluginOptions): Promise ${yellow(`Sourcemaps was automatically enabled for code coverage to be accurate. - To hide this message set build.sourcemap to true, 'inline' or 'hidden'.`)}`); + logger.warn( + `${PLUGIN_NAME}> ${yellow( + 'Sourcemaps was automatically enabled for code coverage to be accurate.\n To hide this message set build.sourcemap to true, "inline" or "hidden".' + )}` + ); // Enforce sourcemapping, - config.build = config.build || {}; + config.build ??= {}; config.build.sourcemap = true; } - testExclude = await createTestExclude(opts) + testExclude = await createTestExclude(opts); }, configResolved(config) { // We need to check if the plugin should enable after all configuration is resolved @@ -139,9 +180,11 @@ export default function istanbulPlugin(opts: IstanbulPluginOptions = {}): Plugin : getEnvVariable('COVERAGE', envPrefix, env); const envVar = envCoverage?.toLowerCase() ?? ''; - if ((checkProd && isProduction && !forceBuildInstrument) || + if ( + (checkProd && isProduction && !forceBuildInstrument) || (!requireEnv && envVar === 'false') || - (requireEnv && envVar !== 'true')) { + (requireEnv && envVar !== 'true') + ) { enabled = false; } }, @@ -157,7 +200,7 @@ export default function istanbulPlugin(opts: IstanbulPluginOptions = {}): Plugin return next(); } - const coverage = (global.__coverage__) ?? null; + const coverage = global.__coverage__ ?? null; let data: string; try { @@ -178,7 +221,9 @@ export default function istanbulPlugin(opts: IstanbulPluginOptions = {}): Plugin // do not transform if ssr return; } - + if (!canInstrumentChunk(id, srcCode)) { + return; + } const filename = resolveFilename(id); if (testExclude.shouldInstrument(filename)) { @@ -187,10 +232,12 @@ export default function istanbulPlugin(opts: IstanbulPluginOptions = {}): Plugin const code = instrumenter.instrumentSync(srcCode, filename, combinedSourceMap); // Create an identity source map with the same number of fields as the combined source map - const identitySourceMap = sanitizeSourceMap(createIdentitySourceMap(filename, srcCode, { - file: combinedSourceMap.file, - sourceRoot: combinedSourceMap.sourceRoot - })); + const identitySourceMap = sanitizeSourceMap( + createIdentitySourceMap(filename, srcCode, { + file: combinedSourceMap.file, + sourceRoot: combinedSourceMap.sourceRoot, + }) + ); // Create a result source map to combine with the source maps of previous plugins instrumenter.instrumentSync(srcCode, filename, identitySourceMap);