forked from goto-bus-stop/browserify-istanbul
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (49 loc) · 1.52 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
var through = require('through2');
var minimatch = require('minimatch');
var defaultIgnore = ['**/node_modules/**', '**/bower_components/**', '**/test/**', '**/tests/**', '**/*.json'];
function shouldIgnoreFile(file, options) {
var ignore = options.defaultIgnore === false ? [] : defaultIgnore;
ignore = ignore.concat(options.ignore || []);
return ignore.some(function(pattern) {
return minimatch(file, pattern, options.minimatchOptions);
});
}
module.exports = function(options, extraOptions) {
var file;
options = options || {};
if (typeof options === 'string') {
file = options;
options = extraOptions || {};
return transform(options, file);
}
return transform.bind(null, options);
};
function transform(options, file) {
if (shouldIgnoreFile(file, options))
return through();
var instrumenterConfig = Object.assign({}, {
autoWrap: true,
coverageVariable: '__coverage__',
embedSource: true,
noCompact: false,
preserveComments: true,
produceSourceMap: true
}, options.instrumenterConfig);
var instrumenter = (options.instrumenter || require('istanbul-lib-instrument')).createInstrumenter(instrumenterConfig);
var data = '';
return through(function(buf, enc, callback) {
data += buf;
callback();
}, function(callback) {
var self = this;
instrumenter.instrument(data, file, function(err, code) {
if (!err) {
self.push(code);
} else {
self.emit('error', err);
}
self.push(null);
callback();
});
});
}