preprocessor.js
5.09 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Coverage Preprocessor
// =====================
//
// Depends on the the reporter to generate an actual report
// Dependencies
// ------------
var istanbul = require('istanbul')
var minimatch = require('minimatch')
var path = require('path')
var _ = require('lodash')
var SourceMapConsumer = require('source-map').SourceMapConsumer
var SourceMapGenerator = require('source-map').SourceMapGenerator
var globalSourceCache = require('./source-cache')
var extend = require('util')._extend
var coverageMap = require('./coverage-map')
// Regexes
// -------
var coverageObjRegex = /\{.*"path".*"fnMap".*"statementMap".*"branchMap".*\}/g
// Preprocessor creator function
function createCoveragePreprocessor (logger, helper, basePath, reporters, coverageReporter) {
var log = logger.create('preprocessor.coverage')
// Options
// -------
var instrumenterOverrides = {}
var instrumenters = {istanbul: istanbul}
var includeAllSources = false
var useJSExtensionForCoffeeScript = false
if (coverageReporter) {
instrumenterOverrides = coverageReporter.instrumenter
instrumenters = extend({istanbul: istanbul}, coverageReporter.instrumenters)
includeAllSources = coverageReporter.includeAllSources === true
useJSExtensionForCoffeeScript = coverageReporter.useJSExtensionForCoffeeScript === true
}
var sourceCache = globalSourceCache.get(basePath)
var instrumentersOptions = _.reduce(instrumenters, function getInstumenterOptions (memo, instrument, name) {
memo[name] = {}
if (coverageReporter && coverageReporter.instrumenterOptions) {
memo[name] = coverageReporter.instrumenterOptions[name]
}
return memo
}, {})
// if coverage reporter is not used, do not preprocess the files
if (!_.includes(reporters, 'coverage')) {
return function (content, _, done) {
done(content)
}
}
// check instrumenter override requests
function checkInstrumenters () {
return _.reduce(instrumenterOverrides, function (acc, literal, pattern) {
if (!_.includes(_.keys(instrumenters), String(literal))) {
log.error('Unknown instrumenter: %s', literal)
return false
}
return acc
}, true)
}
if (!checkInstrumenters()) {
return function (content, _, done) {
return done(1)
}
}
return function (content, file, done) {
log.debug('Processing "%s".', file.originalPath)
var jsPath = path.resolve(file.originalPath)
// default instrumenters
var instrumenterLiteral = 'istanbul'
_.forEach(instrumenterOverrides, function (literal, pattern) {
if (minimatch(file.originalPath, pattern, {dot: true})) {
instrumenterLiteral = String(literal)
}
})
var InstrumenterConstructor = instrumenters[instrumenterLiteral].Instrumenter
var constructOptions = instrumentersOptions[instrumenterLiteral] || {}
var codeGenerationOptions = null
if (file.sourceMap) {
log.debug('Enabling source map generation for "%s".', file.originalPath)
codeGenerationOptions = extend({
format: {
compact: !constructOptions.noCompact
},
sourceMap: file.sourceMap.file,
sourceMapWithCode: true,
file: file.path
}, constructOptions.codeGenerationOptions || {})
}
var options = extend({}, constructOptions)
options = extend(options, {codeGenerationOptions: codeGenerationOptions})
var instrumenter = new InstrumenterConstructor(options)
instrumenter.instrument(content, jsPath, function (err, instrumentedCode) {
if (err) {
log.error('%s\n at %s', err.message, file.originalPath)
done(err.message)
} else {
if (file.sourceMap && instrumenter.lastSourceMap()) {
log.debug('Adding source map to instrumented file for "%s".', file.originalPath)
var generator = SourceMapGenerator.fromSourceMap(new SourceMapConsumer(instrumenter.lastSourceMap().toString()))
generator.applySourceMap(new SourceMapConsumer(file.sourceMap))
file.sourceMap = JSON.parse(generator.toString())
instrumentedCode += '\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,'
instrumentedCode += new Buffer(JSON.stringify(file.sourceMap)).toString('base64') + '\n'
}
// remember the actual immediate instrumented JS for given original path
sourceCache[jsPath] = content
if (includeAllSources) {
// reset stateful regex
coverageObjRegex.lastIndex = 0
var coverageObjMatch = coverageObjRegex.exec(instrumentedCode)
if (coverageObjMatch !== null) {
var coverageObj = JSON.parse(coverageObjMatch[0])
coverageMap.add(coverageObj)
}
}
// RequireJS expects JavaScript files to end with `.js`
if (useJSExtensionForCoffeeScript && instrumenterLiteral === 'ibrik') {
file.path = file.path.replace(/\.coffee$/, '.js')
}
done(instrumentedCode)
}
})
}
}
createCoveragePreprocessor.$inject = [
'logger',
'helper',
'config.basePath',
'config.reporters',
'config.coverageReporter'
]
module.exports = createCoveragePreprocessor