From a8d8110b10fe883dc3177d5d00edbb044b5f1a4c Mon Sep 17 00:00:00 2001 From: ElevateBart Date: Sat, 10 Jul 2021 21:32:41 -0500 Subject: [PATCH] fix: remove the vue specific code and avoid transforming in prod --- src/index.ts | 39 +++------------------------------------ 1 file changed, 3 insertions(+), 36 deletions(-) diff --git a/src/index.ts b/src/index.ts index 2973381..f55ff75 100644 --- a/src/index.ts +++ b/src/index.ts @@ -87,46 +87,13 @@ function createTransform(opts: IstanbulPluginOptions = {}): TransformHook { }); return async function (this: TransformPluginContext, srcCode: string, id: string): Promise{ - if (process.env.NODE_ENV == 'production' || id.startsWith('/@modules/')) { + if (id.startsWith('/@modules/')) { // do not transform if this is a dep - // do not transform for production builds return; } if (exclude.shouldInstrument(id)) { - // if the vue component has already been transformed, - // it can be treated as a javascript file - if (!id.endsWith('.vue') || srcCode.trim().slice(0, 1) !== "<") { - return instrumentCode.call(this, srcCode, id, opts); - } - - // Vue files are special, it requires a hack to fix the source mappings - // We take the source code from within the '); - - if (!openScriptTagObject || endIndex == -1) { - // ignore this vue file, doesn't contain any javascript - return; - } - - const startIndex = openScriptTagObject.index - const openScriptTag = openScriptTagObject[0] - - const numberOfLinesBeforeScript = srcCode.slice(0, endIndex).match(/\n/g)?.length ?? 0; - const startOffset = openScriptTag.length; - - const scriptCode = '\n'.repeat(numberOfLinesBeforeScript) + srcCode.slice(startIndex + startOffset, endIndex); - - const res = await instrumentCode.call(this, scriptCode, id, opts); - - // if is anywhere in the script block, even in a string, - // the parser errors - const resCodeSanatized = res.code.replace(/<\/script>/g, "<\\/script>") - - res.code = `${srcCode.slice(0, startIndex + startOffset)}\n${resCodeSanatized}\n${srcCode.slice(endIndex)}`; - return res; + return instrumentCode.call(this, srcCode, id, opts); } }; } @@ -136,7 +103,7 @@ function istanbulPlugin(opts: IstanbulPluginOptions = {}): Plugin { const env = opts.cypress ? process.env.CYPRESS_COVERAGE : process.env.VITE_COVERAGE; const requireEnv = opts.requireEnv ?? false; - if (requireEnv && env?.toLowerCase() === 'false') { + if (process.env.NODE_ENV == 'production' && requireEnv && env?.toLowerCase() === 'false') { return { name: 'vite:istanbul' }; }