@@ -8,6 +8,7 @@ const { isAbsolute, resolve } = require('path')
88// TODO: switch back to @c 88/v8-coverage once patch is landed.
99const { mergeProcessCovs } = require ( '@bcoe/v8-coverage' )
1010const v8toIstanbul = require ( 'v8-to-istanbul' )
11+ const isCjsEsmBridgeCov = require ( './is-cjs-esm-bridge' )
1112
1213class Report {
1314 constructor ( {
@@ -55,21 +56,43 @@ class Report {
5556 if ( this . _allCoverageFiles ) return this . _allCoverageFiles
5657
5758 const v8ProcessCov = this . _getMergedProcessCov ( )
58-
5959 const map = libCoverage . createCoverageMap ( { } )
60+ const resultCountPerPath = new Map ( )
61+ const possibleCjsEsmBridges = new Map ( )
6062
6163 for ( const v8ScriptCov of v8ProcessCov . result ) {
6264 try {
6365 const path = resolve ( this . resolve , v8ScriptCov . url )
6466 const converter = v8toIstanbul ( path , this . wrapperLength )
6567 await converter . load ( )
66- converter . applyCoverage ( v8ScriptCov . functions )
67- map . merge ( converter . toIstanbul ( ) )
68+
69+ if ( resultCountPerPath . has ( path ) ) {
70+ resultCountPerPath . set ( path , resultCountPerPath . get ( path ) + 1 )
71+ } else {
72+ resultCountPerPath . set ( path , 0 )
73+ }
74+
75+ if ( isCjsEsmBridgeCov ( v8ScriptCov ) ) {
76+ possibleCjsEsmBridges . set ( converter , {
77+ path,
78+ functions : v8ScriptCov . functions
79+ } )
80+ } else {
81+ converter . applyCoverage ( v8ScriptCov . functions )
82+ map . merge ( converter . toIstanbul ( ) )
83+ }
6884 } catch ( err ) {
6985 console . warn ( `file: ${ v8ScriptCov . url } error: ${ err . stack } ` )
7086 }
7187 }
7288
89+ for ( const [ converter , { path, functions } ] of possibleCjsEsmBridges ) {
90+ if ( resultCountPerPath . get ( path ) <= 1 ) {
91+ converter . applyCoverage ( functions )
92+ map . merge ( converter . toIstanbul ( ) )
93+ }
94+ }
95+
7396 this . _allCoverageFiles = map
7497 return this . _allCoverageFiles
7598 }
0 commit comments