import { createFilter } from 'rollup-pluginutils';
import istanbul from 'istanbul';

export default function (options = {}) {
  const filter = createFilter(options.include, options.exclude);

  return {
    transform (code, id) {
      if (!filter(id)) return;

      var InstrumenterImpl = (options.instrumenter || istanbul).Instrumenter;
      var instrumenterOptions = options.instrumenterConfig || { };
      if (!('esModules' in instrumenterOptions)) {
        instrumenterOptions.esModules = true;
      }

      var instrumenter = new InstrumenterImpl(instrumenterOptions);

      code = instrumenter.instrumentSync(code, id);

      return {
        code,
        map: { mappings: '' }
      };
    }
  };
}

@@ -506,16 +506,171 @@
      %7D%0A%0A
+      instrumenterOptions.codeGenerationOptions = instrumenter.codeGenerationOptions %7C%7C %7B%0A        sourceMap: id,%0A        sourceMapWithCode: true%0A      %7D;%0A%0A
       va
@@ -793,42 +793,79 @@
     
-return %7B%0A        code,%0A        map
+var map = instrumenter.lastSourceMap();%0A      map = map ? map.toJSON() 
 : %7B 
@@ -878,23 +878,44 @@
 gs: '' %7D
+;%0A
 %0A      
+return %7B code, map 
 %7D;%0A    %7D

add source map support