From 99ce9c0aca2f4cd62e23041b92291ac04e2c6407 Mon Sep 17 00:00:00 2001 From: joshwooding <12938082+joshwooding@users.noreply.github.com> Date: Thu, 5 May 2022 01:13:44 +0100 Subject: [PATCH] fix: sourcesContent not being escaped --- src/index.ts | 6 ++++-- src/types.d.ts | 6 +++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index 1c3d38b..3f90cfc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ -import type { SourceMap } from 'rollup'; +import type { ExistingRawSourceMap } from 'rollup'; import { Plugin, TransformResult, createLogger } from 'vite'; import { createInstrumenter } from 'istanbul-lib-instrument'; import TestExclude from 'test-exclude'; @@ -27,7 +27,9 @@ const PLUGIN_NAME = 'vite:istanbul'; const MODULE_PREFIX = '/@modules/'; const NULL_STRING = '\0'; -function sanitizeSourceMap(sourceMap: SourceMap): SourceMap { +function sanitizeSourceMap(sourceMap: ExistingRawSourceMap): ExistingRawSourceMap { + // delete sourcesContent since it is optional and if it contains process.env.NODE_ENV vite will try and replace it and break + delete sourceMap.sourcesContent; // JSON parse/stringify trick required for istanbul to accept the SourceMap return JSON.parse(JSON.stringify(sourceMap)); } diff --git a/src/types.d.ts b/src/types.d.ts index 3fe6451..76566d8 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -1,8 +1,8 @@ declare module 'istanbul-lib-instrument' { - import { SourceMap } from 'rollup'; + import { ExistingRawSourceMap } from 'rollup'; interface Instrumenter { - instrumentSync(code: string, filename: string, inputSourceMap?: SourceMap | undefined): string; - lastSourceMap(): SourceMap; + instrumentSync(code: string, filename: string, inputSourceMap?: ExistingRawSourceMap | undefined): string; + lastSourceMap(): ExistingRawSourceMap; } export function createInstrumenter(opts: {