Skip to content

Commit 55aeead

Browse files
pr1smjohnjbarton
authored andcommittedDec 9, 2019
Update Source Map Handling (#394)
* refactor(preprocessor): Update Source Map Handling This commit updates the handling of source map inclusion in the preprocessor. Instead of attempting to combine both the incoming source map and instrumented source map, a single one is chosen and included as an inline comment of the instrumented code. This commit also adds a null/undefined check before registering an incoming source map in the sourceMapStore. * chore(deps): Remove unused source-map package The previous commit removes the need for the source-map package. * test(preprocessor): Update test mocks This commit updates the instrumenter mock. Since the new logic calls instrumenter.lastSourceMap() in more cases, this has to be mocked properly. * fix(preprocessor): merging source maps This commit fixes an issue where karma would be unable to report correctly mapped stacktraces back to original sources. Instead of manually merging source maps, the default instrumenter, istanbul, supports passing in an input source map and having that merged automatically with the internal source map of instrumented code. The merged source map is now available for access using instrumenter.lastSourceMap(), so that is what file.sourceMap gets updated to (if the merge completed successfully). Istanbul reporting still requires the original source map only, so the check to register the source map with the source store is made before this merged source map is used. This allows istanbul to use the original incoming source map, while still allowing karma to use the merged source map.
1 parent b23664e commit 55aeead

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed
 

‎lib/preprocessor.js

+11-10
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
const { createInstrumenter } = require('istanbul-lib-instrument')
1010
const minimatch = require('minimatch')
1111
const path = require('path')
12-
const { SourceMapConsumer, SourceMapGenerator } = require('source-map')
1312
const globalSourceMapStore = require('./source-map-store')
1413
const globalCoverageMap = require('./coverage-map')
1514

@@ -148,18 +147,20 @@ function createCoveragePreprocessor (logger, basePath, reporters = [], coverageR
148147
log.error('%s\n at %s', err.message, file.originalPath)
149148
done(err.message)
150149
} else {
151-
if (file.sourceMap && instrumenter.lastSourceMap()) {
150+
// Register the incoming sourceMap for transformation during reporting (if it exists)
151+
if (file.sourceMap) {
152+
sourceMapStore.registerMap(jsPath, file.sourceMap)
153+
}
154+
155+
// Add merged source map (if it merged correctly)
156+
const lastSourceMap = instrumenter.lastSourceMap()
157+
if (lastSourceMap) {
152158
log.debug('Adding source map to instrumented file for "%s".', file.originalPath)
153-
const generator = SourceMapGenerator.fromSourceMap(new SourceMapConsumer(instrumenter.lastSourceMap().toString()))
154-
generator.applySourceMap(new SourceMapConsumer(file.sourceMap))
155-
file.sourceMap = JSON.parse(generator.toString())
159+
file.sourceMap = lastSourceMap
156160
instrumentedCode += '\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,'
157-
instrumentedCode += Buffer.from(JSON.stringify(file.sourceMap)).toString('base64') + '\n'
161+
instrumentedCode += Buffer.from(JSON.stringify(lastSourceMap)).toString('base64') + '\n'
158162
}
159163

160-
// Register the sourceMap for transformation during reporting
161-
sourceMapStore.registerMap(jsPath, file.sourceMap)
162-
163164
if (includeAllSources) {
164165
let coverageObj
165166
// Check if the file coverage object is exposed from the instrumenter directly
@@ -186,7 +187,7 @@ function createCoveragePreprocessor (logger, basePath, reporters = [], coverageR
186187

187188
done(instrumentedCode)
188189
}
189-
})
190+
}, file.sourceMap)
190191
}
191192
}
192193

‎package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
"istanbul-lib-report": "^2.0.8",
2525
"istanbul-lib-source-maps": "^3.0.6",
2626
"istanbul-reports": "^2.2.4",
27-
"minimatch": "^3.0.0",
28-
"source-map": "^0.5.1"
27+
"minimatch": "^3.0.0"
2928
},
3029
"license": "MIT",
3130
"devDependencies": {

‎test/preprocessor.spec.coffee

+6-1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ describe 'preprocessor', ->
6565
fakeInstanbulLikeInstrumenter::instrument = (_a, _b, callback) ->
6666
callback()
6767
return
68+
fakeInstanbulLikeInstrumenter::lastSourceMap = ->
69+
return
6870
process = createPreprocessor mockLogger, '/base/path', ['coverage', 'progress'],
6971
instrumenters:
7072
fakeInstanbulLike :
@@ -89,7 +91,8 @@ describe 'preprocessor', ->
8991
fakeInstanbulLikeInstrumenter::instrument = (_a, _b, callback) ->
9092
callback()
9193
return
92-
94+
fakeInstanbulLikeInstrumenter::lastSourceMap = ->
95+
return
9396
process = createPreprocessor mockLogger, '/base/path', ['coverage', 'progress'],
9497
instrumenters:
9598
fakeInstanbulLike:
@@ -164,6 +167,8 @@ describe 'preprocessor', ->
164167
ibrikInstrumenter::instrument = (_a, _b, callback) ->
165168
callback()
166169
return
170+
ibrikInstrumenter::lastSourceMap = ->
171+
return
167172

168173
process = createPreprocessor mockLogger, '/base/path', ['coverage', 'progress'],
169174
instrumenters:

0 commit comments

Comments
 (0)
Please sign in to comment.