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 committed Apr 17, 2015
1 parent 88f48f2 commit a80300d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
36 changes: 32 additions & 4 deletions 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,14 +55,40 @@ var createCoveragePreprocessor = function(logger, basePath, reporters, coverageR
}
}

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);
}

if (file.sourceMap) {
var consumer = new SourceMapConsumer(file.sourceMap);
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;

Expand All @@ -69,9 +97,9 @@ var createCoveragePreprocessor = function(logger, basePath, reporters, coverageR
var coverageObjMatch = coverageObjRegex.exec(instrumentedCode);

if (coverageObjMatch !== null) {
var coverageObj = JSON.parse(coverageObjMatch[0]);
coverageMap.add(coverageObj);
var coverageObj = JSON.parse(coverageObjMatch[0]);

coverageMap.add(coverageObj);
}
}

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"dependencies": {
"dateformat": "~1.0.6",
"istanbul": "~0.3.0",
"minimatch": "^0.3.0"
"minimatch": "~0.3.0",
"source-map": "~0.4.2"
},
"peerDependencies": {
"karma": ">=0.9"
Expand Down

0 comments on commit a80300d

Please sign in to comment.