forked from artberri/rollup-plugin-istanbul
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: update dependencies and fix sourcemap support
Closes artberri#21
- Loading branch information
Showing
4 changed files
with
113 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,41 @@ | ||
import { createFilter } from 'rollup-pluginutils'; | ||
import istanbul from 'istanbul-lib-instrument'; | ||
import { extname } from 'path'; | ||
|
||
function makeFilter (opts, extensions) { | ||
const filter = createFilter(opts.include, opts.exclude); | ||
|
||
extensions = opts.extensions || extensions; | ||
if (!extensions || extensions === '*') { | ||
return filter; | ||
} | ||
|
||
if (!Array.isArray(extensions)) { | ||
extensions = [extensions]; | ||
} | ||
extensions = extensions.map(e => (e[0] !== '.' ? `.${e}` : e)); | ||
|
||
return id => filter(id) && extensions.indexOf(extname(id)) > -1; | ||
} | ||
|
||
export default function (options = {}) { | ||
const filter = createFilter(options.include, options.exclude); | ||
const filter = makeFilter(options, ['.js']), | ||
opts = Object.assign( | ||
{ esModules: true, compact: options.compact !== false }, | ||
options.instrumenterConfig, | ||
{ produceSourceMap: options.sourceMap !== false } | ||
), | ||
instrumenter = new (options.instrumenter || istanbul).createInstrumenter(opts); | ||
|
||
return { | ||
name: 'istanbul', | ||
transform (code, id) { | ||
if (!filter(id)) return; | ||
|
||
var instrumenter; | ||
var sourceMap = !!options.sourceMap; | ||
var opts = Object.assign({}, options.instrumenterConfig); | ||
|
||
if (sourceMap) { | ||
opts.codeGenerationOptions = Object.assign({}, | ||
opts.codeGenerationOptions || {format: {compact: !opts.noCompact}}, | ||
{sourceMap: id, sourceMapWithCode: true} | ||
); | ||
} | ||
|
||
opts.esModules = true; | ||
instrumenter = new (options.instrumenter || istanbul).createInstrumenter(opts); | ||
|
||
// TODO require the inputSourceMap that generated by the previous plugins | ||
code = instrumenter.instrumentSync(code, id); | ||
|
||
var map = sourceMap ? | ||
instrumenter.lastSourceMap().toJSON() : | ||
{mappings: ''}; | ||
|
||
return { code, map }; | ||
return { code, map: instrumenter.lastSourceMap() }; | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,79 @@ | ||
var assert = require('assert'); | ||
var rollup = require('rollup'); | ||
var istanbulPlugin = require( '..' ); | ||
const assert = require('assert'); | ||
const rollup = require('rollup'); | ||
const istanbulPlugin = require('..'); | ||
|
||
process.chdir( __dirname ); | ||
process.chdir(__dirname); | ||
|
||
describe('rollup-plugin-istanbul', function () { | ||
this.timeout(15000); | ||
|
||
it('transforms code through istanbul instrumenter', function () { | ||
return rollup.rollup({ | ||
input: 'fixtures/main.js', | ||
plugins: [ istanbulPlugin() ], | ||
globals: { | ||
whatever: 'whatever' | ||
} | ||
}).then( function ( bundle ) { | ||
return bundle.generate({format: 'iife'}); | ||
}).then(generated => { | ||
var code = generated.code; | ||
assert.ok(code.indexOf('coverage[path]') !== -1, code); | ||
}); | ||
return rollup | ||
.rollup({ | ||
input: 'fixtures/main.js', | ||
plugins: [istanbulPlugin()] | ||
}) | ||
.then(bundle => | ||
bundle.generate({ | ||
format: 'iife', | ||
globals: { | ||
whatever: 'whatever' | ||
} | ||
}) | ||
) | ||
.then(generated => { | ||
const code = generated.output[0].code; | ||
assert.ok(code.indexOf('coverage[path]') !== -1, code); | ||
}); | ||
}); | ||
|
||
it('adds the file name properly', function () { | ||
return rollup.rollup({ | ||
input: 'fixtures/main.js', | ||
plugins: [ istanbulPlugin() ], | ||
globals: { | ||
whatever: 'whatever' | ||
} | ||
}).then( function ( bundle ) { | ||
return bundle.generate({format: 'iife'}); | ||
}).then(generated => { | ||
var code = generated.code; | ||
assert.ok(code.indexOf('fixtures/main.js') !== -1, code); | ||
}); | ||
return rollup | ||
.rollup({ | ||
input: 'fixtures/main.js', | ||
plugins: [istanbulPlugin()] | ||
}) | ||
.then(bundle => | ||
bundle.generate({ | ||
format: 'iife', | ||
globals: { | ||
whatever: 'whatever' | ||
} | ||
}) | ||
) | ||
.then(generated => { | ||
const code = generated.output[0].code; | ||
assert.ok(/(\/|\\\\)fixtures(\/|\\\\)main\.js"/.test(code), code); | ||
}); | ||
}); | ||
|
||
it('transforms code through istanbul instrumenter with source map', function () { | ||
return rollup | ||
.rollup({ | ||
input: 'fixtures/main.js', | ||
plugins: [istanbulPlugin()] | ||
}) | ||
.then(bundle => | ||
bundle.generate({ | ||
sourcemap: true, | ||
format: 'iife', | ||
globals: { | ||
whatever: 'whatever' | ||
} | ||
}) | ||
) | ||
.then(generated => { | ||
const { code, map } = generated.output[0]; | ||
|
||
assert.ok(code.indexOf('coverage[path]') !== -1, code); | ||
assert.ok(/(\/|\\\\)fixtures(\/|\\\\)main\.js"/.test(code), code); | ||
|
||
assert.ok(map.sources[0] === 'fixtures/main.js', code); | ||
assert.ok( | ||
map.sourcesContent[0] === | ||
'function foo(bar) {\n if (bar) {\n whatever.do();\n } else {\n whatever.stop();\n }\n}\n', | ||
map | ||
); | ||
}); | ||
}); | ||
}); |