Skip to content

Commit

Permalink
feat(preprocessor): Add sourcemap support
Browse files Browse the repository at this point in the history
Adds sourcemap support to the preprocessor. If the file being
processed has a sourcemap property, it is merged with the output
from the coverage instrumentation. This enables better debugging
on instrumented code.

Closes karma-runner#109
  • Loading branch information
nmalaguti authored and Nick Malaguti committed Jun 9, 2015
1 parent a438122 commit d6234b8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
30 changes: 29 additions & 1 deletion lib/preprocessor.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var istanbul = require('istanbul'),
minimatch = require('minimatch'),
SourceMapConsumer = require('source-map').SourceMapConsumer,
SourceMapGenerator = require('source-map').SourceMapGenerator,
globalSourceCache = require('./sourceCache'),
extend = require('util')._extend,
coverageMap = require('./coverageMap')
Expand Down Expand Up @@ -53,11 +55,37 @@ var createCoveragePreprocessor = function (logger, basePath, reporters, coverage
}
}

var instrumenter = new instrumenters[instrumenterLiteral].Instrumenter(instrumentersOptions[instrumenterLiteral] || {})
var codeGenerationOptions = null

if (file.sourceMap) {
log.debug('Enabling source map generation for "%s".', file.originalPath)
codeGenerationOptions = extend({
format: {
compact: !instrumentersOptions[instrumenterLiteral].noCompact
},
sourceMap: file.sourceMap.file,
sourceMapWithCode: true,
file: file.path
}, instrumentersOptions[instrumenterLiteral].codeGenerationOptions || {})
}

var options = extend({}, instrumentersOptions[instrumenterLiteral] || {})
options = extend(options, {codeGenerationOptions: codeGenerationOptions})

var instrumenter = new instrumenters[instrumenterLiteral].Instrumenter(options)
instrumenter.instrument(content, jsPath, function (err, instrumentedCode) {
if (err) {
log.error('%s\n at %s', err.message, file.originalPath)
return
}

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
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
],
"author": "SATO taichi <[email protected]>",
"dependencies": {
"istanbul": "^0.3.0",
"istanbul": "^0.3.15",
"dateformat": "^1.0.6",
"minimatch": "^2.0.8"
"minimatch": "^2.0.8",
"source-map": "^0.4.2"
},
"license": "MIT",
"devDependencies": {
Expand Down

0 comments on commit d6234b8

Please sign in to comment.