Skip to content

Commit

Permalink
fix: update dependencies and fix sourcemap support
Browse files Browse the repository at this point in the history
  • Loading branch information
billowz committed Jul 8, 2019
1 parent 4431393 commit a2e4654
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 56 deletions.
20 changes: 15 additions & 5 deletions README.md
Expand Up @@ -50,6 +50,18 @@ Can be a minimatch pattern or an array of minimatch patterns. If is omitted or o

Can be a minimatch pattern or an array of minimatch patterns. Files to exclude, commonly the test files.

#### `extensions`

Array of strings that specifies the file extensions to process. default: ['js']

#### `compact`

generate compact code. default: `true`

#### `sourceMap`

produce a source map for the instrumented code. default: `true`

#### `instrumenterConfig`

An object of options that will be passed to the instrumenter.
Expand All @@ -59,14 +71,12 @@ Default value:
```js
{
esModules: true,
codeGenerationOptions: {
sourceMap: id,
sourceMapWithCode: true
}
compact: true,
produceSourceMap: true
}
```

[More info](http://gotwarlost.github.io/istanbul/public/apidocs/classes/Instrumenter.html#method_Instrumenter) about options.
[More info](https://github.com/istanbuljs/istanbuljs/blob/master/packages/istanbul-lib-instrument/api.md#parameters-1) about options.

#### `instrumenter`

Expand Down
6 changes: 3 additions & 3 deletions package.json
Expand Up @@ -33,15 +33,15 @@
"lint": "eslint src/index.js test/*.js"
},
"dependencies": {
"istanbul-lib-instrument": "^1.9.1",
"rollup-pluginutils": "^2.0.1"
"istanbul-lib-instrument": "^3.3.0",
"rollup-pluginutils": "^2.8.1"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-preset-env": "^1.6.1",
"eslint": "^4.12.1",
"mocha": "^4.0.1",
"rollup": "^0.52.1",
"rollup": "^1.0.0",
"rollup-plugin-babel": "^3.0.2"
},
"repository": {
Expand Down
47 changes: 27 additions & 20 deletions src/index.js
@@ -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() };
}
};
}
96 changes: 68 additions & 28 deletions test/test.js
@@ -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
);
});
});
});

0 comments on commit a2e4654

Please sign in to comment.