forked from webpack-contrib/istanbul-instrumenter-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
53 lines (47 loc) · 2.01 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
/* eslint-disable no-param-reassign */
import { createInstrumenter } from 'istanbul-lib-instrument';
import loaderUtils from 'loader-utils';
import validateOptions from 'schema-utils';
import convert from 'convert-source-map';
/* eslint-disable-line */
const schema = require('./options');
export default function instanbulInstrumentLoader(source, sourceMap) {
/**
* This is an unfortunate hack that lets us use fork-ts-checker-webpack-plugin
* for a faster build, whilst also checking for 100% branch coverage with Istanbul.
*
* Based on: https://github.com/kulshekhar/ts-jest/pull/488
*
* When TypeScript transpiles, it introduces some logic branches outside of our
* control, particularly where decorators are concerned.
*
* This is a thin wrapper around the istanbul-instrumenter-loader for Webpack,
* which modifies the transpiled code and inserts Istanbul ignore statements
* to ignore these branches.
*/
if (source) {
// Ignore decorators on methods and properties
source = source.replace(/(tslib.+\.__decorate)/g, '/* istanbul ignore next */$1');
// When constructor parameters have decorated properties (eg @inject), TS adds
// a typeof branch check, which we don't want to instrument
source = source.replace(/(typeof \(_\w\s*=)/g, '/* istanbul ignore next */$1');
// ------------------------- END OF HACK -------------------------
}
const options = Object.assign({ produceSourceMap: true }, loaderUtils.getOptions(this));
validateOptions(schema, options, {
name: 'Istanbul Instrumenter Loader',
baseDataPath: 'options',
});
let srcMap = sourceMap;
// use inline source map, if any
if (!srcMap) {
const inlineSourceMap = convert.fromSource(source);
if (inlineSourceMap) {
srcMap = inlineSourceMap.sourcemap;
}
}
const instrumenter = createInstrumenter(options);
instrumenter.instrument(source, this.resourcePath, (error, instrumentedSource) => {
this.callback(error, instrumentedSource, instrumenter.lastSourceMap());
}, srcMap);
}