From d6234b80e00f484694b20e4a4ebde0f0075bb634 Mon Sep 17 00:00:00 2001 From: Nick Malaguti Date: Thu, 16 Apr 2015 12:13:23 -0400 Subject: [PATCH] feat(preprocessor): Add sourcemap support 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 #109 --- lib/preprocessor.js | 30 +++++++++++++++++++++++++++++- package.json | 5 +++-- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/lib/preprocessor.js b/lib/preprocessor.js index c43afbf..a13bd96 100644 --- a/lib/preprocessor.js +++ b/lib/preprocessor.js @@ -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') @@ -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 diff --git a/package.json b/package.json index 5ea2f6e..93e7cf9 100644 --- a/package.json +++ b/package.json @@ -19,9 +19,10 @@ ], "author": "SATO taichi ", "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": {