import plugin , { StartFilesProps } from '@start/plugin/src/' ; import { InstrumenterOptions } from 'istanbul-lib-instrument' ; export default ( options ? , extensions ? ) => plugin ( 'istanbulInstrument' , ( { logMessage } ) => async ( { files } ) => { const Module = await import ( 'module' ) ; const { fromSource : getSourceMapFromSource } = await import ( 'convert-source-map' ) ; const { createInstrumenter } = await import ( 'istanbul-lib-instrument' ) ; const { hookRequire } = await import ( 'istanbul-lib-hook' ) ; const hooks = await import ( './hooks' ) ; const { default : coverageVariable } = await import ( './variable' ) ; const instrumenter = createInstrumenter ( { ... options , coverageVariable } ) ; hooks . clearAll ( ) ; files . forEach ( ( file ) => { delete Module . _cache [ file . path ] ; } ) ; const hook = hookRequire ( ( file ) => files . findIndex ( ( { path } ) => file === path ) !== - 1 , ( source , options ) => { const sourceMapRaw = getSourceMapFromSource ( source ) ; let sourceMapObject = null ; if ( sourceMapRaw ) { sourceMapObject = sourceMapRaw . toObject ( ) ; } return instrumenter . instrumentSync ( source , options . file , sourceMapObject ) ; } , { extensions } ) ; hooks . add ( hook ) ; logMessage ( 'imports has been hooked' ) ; } ) ;