Skip to content

Commit

Permalink
fix: remove the vue specific code and avoid transforming in prod
Browse files Browse the repository at this point in the history
  • Loading branch information
elevatebart committed Jul 11, 2021
1 parent 59e7132 commit a8d8110
Showing 1 changed file with 3 additions and 36 deletions.
39 changes: 3 additions & 36 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,46 +87,13 @@ function createTransform(opts: IstanbulPluginOptions = {}): TransformHook {
});

return async function (this: TransformPluginContext, srcCode: string, id: string): Promise<undefined | TransformResult>{
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 <script> tag and instrument this
// Then we pad the lines to get the correct line numbers for the mappings
const openScriptTagObject = scriptRE.exec(srcCode);
const endIndex = srcCode.indexOf('</script>');

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 </script> 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);
}
};
}
Expand All @@ -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' };
}

Expand Down

0 comments on commit a8d8110

Please sign in to comment.