Skip to content

Commit

Permalink
Merge previous source maps when generating new
Browse files Browse the repository at this point in the history
  • Loading branch information
kpdecker committed Sep 16, 2016
1 parent 8af1d57 commit 5f531b7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
19 changes: 15 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var rimraf = require('rimraf')
var onExit = require('signal-exit')
var resolveFrom = require('resolve-from')
var convertSourceMap = require('convert-source-map')
var mergeSourceMap = require('merge-source-map')
var md5hex = require('md5-hex')
var findCacheDir = require('find-cache-dir')
var js = require('default-require-extensions/js')
Expand Down Expand Up @@ -253,19 +254,30 @@ NYC.prototype._transformFactory = function (cacheDir) {

return function (code, metadata, hash) {
var filename = metadata.filename
var sourceMap

if (_this._sourceMap) _this._handleSourceMap(cacheDir, code, hash, filename)
if (_this._sourceMap) {
sourceMap = convertSourceMap.fromSource(code) || convertSourceMap.fromMapFileSource(code, path.dirname(filename))

_this._handleSourceMap(cacheDir, code, hash, filename, sourceMap)
}

try {
instrumented = instrumenter.instrumentSync(code, filename)

var lastSourceMap = instrumenter.lastSourceMap()
if (lastSourceMap) {
if (sourceMap) {
lastSourceMap = mergeSourceMap(
sourceMap.toObject(),
lastSourceMap)
}

instrumented += '\n' + convertSourceMap.fromObject(lastSourceMap).toComment()
}
} catch (e) {
// don't fail external tests due to instrumentation bugs.
console.warn('failed to instrument', filename, 'with error:', e.message)
console.warn('failed to instrument', filename, 'with error:', e.stack)
instrumented = code
}

Expand All @@ -277,8 +289,7 @@ NYC.prototype._transformFactory = function (cacheDir) {
}
}

NYC.prototype._handleSourceMap = function (cacheDir, code, hash, filename) {
var sourceMap = convertSourceMap.fromSource(code) || convertSourceMap.fromMapFileSource(code, path.dirname(filename))
NYC.prototype._handleSourceMap = function (cacheDir, code, hash, filename, sourceMap) {
if (sourceMap) {
if (hash) {
var mapPath = path.join(cacheDir, hash + '.map')
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
"istanbul-lib-source-maps": "^1.0.1",
"istanbul-reports": "^1.0.0-alpha.8",
"md5-hex": "^1.2.0",
"merge-source-map": "^1.0.2",
"micromatch": "^2.3.11",
"mkdirp": "^0.5.0",
"pkg-up": "^1.0.0",
Expand Down

0 comments on commit 5f531b7

Please sign in to comment.