forked from artberri/rollup-plugin-istanbul
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
41 lines (33 loc) · 1.18 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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 = 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;
// TODO require the inputSourceMap that generated by the previous plugins
code = instrumenter.instrumentSync(code, id);
return { code, map: instrumenter.lastSourceMap() };
}
};
}