From 713936c4bb42dc9ddeefba35295e886a1e96feed Mon Sep 17 00:00:00 2001 From: Matt Winchester Date: Mon, 9 May 2016 15:27:17 -0600 Subject: [PATCH 1/2] feat: adding sourcemap support --- index.js | 20 ++++++++++++++++++-- package.json | 3 ++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 2c06abe..f90d20d 100644 --- a/index.js +++ b/index.js @@ -1,19 +1,35 @@ 'use strict'; +var SourceMapConsumer = require('source-map').SourceMapConsumer; var isparta = require('isparta'); -module.exports = function(source) { +module.exports = function(source, map) { var config = this.options.isparta || { embedSource: true, noAutoWrap: true, babel: this.options.babel }; + if(this.sourceMap){ + config.codeGenerationOptions = config.codeGenerationOptions || {}; + config.codeGenerationOptions.sourceMap = this.resourcePath; + config.codeGenerationOptions.sourceMapWithCode = true; + } + var instrumenter = new isparta.Instrumenter(config); if (this.cacheable) { this.cacheable(); } - return instrumenter.instrumentSync(source, this.resourcePath); + var instrumented = instrumenter.instrumentSync(source, this.resourcePath); + + if(this.sourceMap){ + var outMap = instrumenter.lastSourceMap(); + outMap.applySourceMap(new SourceMapConsumer(map), this.resourcePath); + + map = outMap.toJSON(); + } + + this.callback(null, instrumented, map); }; diff --git a/package.json b/package.json index 2e1f6ae..5c609e9 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,8 @@ "main": "index.js", "files": [ "index.js" ], "dependencies": { - "isparta": "4.x.x" + "isparta": "4.x.x", + "source-map": "^0.5.6" }, "engines": { "node": ">=0.10.0" From f13cf116d06dae61802ae27900c5a7c841431f2b Mon Sep 17 00:00:00 2001 From: Matt Winchester Date: Tue, 10 May 2016 11:04:21 -0600 Subject: [PATCH 2/2] handle the null case --- index.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index f90d20d..13cb7b2 100644 --- a/index.js +++ b/index.js @@ -26,9 +26,12 @@ module.exports = function(source, map) { if(this.sourceMap){ var outMap = instrumenter.lastSourceMap(); - outMap.applySourceMap(new SourceMapConsumer(map), this.resourcePath); - map = outMap.toJSON(); + if(outMap){ + outMap.applySourceMap(new SourceMapConsumer(map), this.resourcePath); + + map = outMap.toJSON(); + } } this.callback(null, instrumented, map);