Skip to content

Commit

Permalink
feat(reporter): cache SourceMapConsumer
Browse files Browse the repository at this point in the history
Error formatting is very slow on big files. This makes it a lot faster.
  • Loading branch information
panrafal authored and dignifiedquire committed May 20, 2015
1 parent e046379 commit fe6ed7e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
9 changes: 7 additions & 2 deletions lib/reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var log = require('./logger').create('reporter');
var MultiReporter = require('./reporters/multi');
var baseReporterDecoratorFactory = require('./reporters/base').decoratorFactory;
var SourceMapConsumer = require('source-map').SourceMapConsumer;
var memoizeWeak = require('memoizee/weak');

var createErrorFormatter = function(basePath, emitter, SourceMapConsumer) {
var lastServedFiles = [];
Expand Down Expand Up @@ -30,6 +31,10 @@ var createErrorFormatter = function(basePath, emitter, SourceMapConsumer) {
'(\\:(\\d+))?' + // column
'', 'g');

var getSourceMapConsumer = memoizeWeak(function(sourceMap) {
return new SourceMapConsumer(sourceMap);
});

return function(msg, indentation) {
// remove domain and timestamp from source files
// and resolve base path / absolute path urls into absolute path
Expand All @@ -45,9 +50,9 @@ var createErrorFormatter = function(basePath, emitter, SourceMapConsumer) {
line = parseInt(line || '0', 10);
column = parseInt(column || '0', 10);

var smc = new SourceMapConsumer(file.sourceMap);
try {
var original = smc.originalPositionFor({line: line, column: column});
var original = getSourceMapConsumer(file.sourceMap)
.originalPositionFor({line: line, column: column});

return util.format('%s:%d:%d <- %s:%d:%d', path, line, column, original.source,
original.line, original.column);
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@
"useragent": "~2.0.4",
"graceful-fs": "~2.0.1",
"connect": "~2.26.0",
"source-map": "~0.1.31"
"source-map": "~0.1.31",
"memoizee": "~0.3.8"
},
"devDependencies": {
"LiveScript": "^1.3.0",
Expand Down
8 changes: 4 additions & 4 deletions test/unit/reporter.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ describe 'reporter', ->

class MockSourceMapConsumer
constructor: (sourceMap) ->
@source = sourceMap.replace 'SOURCE MAP ', '/original/'
@source = sourceMap.content.replace 'SOURCE MAP ', '/original/'
originalPositionFor: (position) ->
if position.line == 0
throw new TypeError('Line must be greater than or equal to 1, got 0')
Expand All @@ -88,8 +88,8 @@ describe 'reporter', ->
it 'should rewrite stack traces', (done) ->
formatError = m.createErrorFormatter '/some/base', emitter, MockSourceMapConsumer
servedFiles = [new File('/some/base/a.js'), new File('/some/base/b.js')]
servedFiles[0].sourceMap = 'SOURCE MAP a.js'
servedFiles[1].sourceMap = 'SOURCE MAP b.js'
servedFiles[0].sourceMap = {content: 'SOURCE MAP a.js'}
servedFiles[1].sourceMap = {content: 'SOURCE MAP b.js'}

emitter.emit 'file_list_modified', q(served: servedFiles)

Expand Down Expand Up @@ -118,7 +118,7 @@ describe 'reporter', ->
beforeEach ->
formatError = m.createErrorFormatter '/some/base', emitter, MockSourceMapConsumer
servedFiles = [new File('C:/a/b/c.js')]
servedFiles[0].sourceMap = 'SOURCE MAP b.js'
servedFiles[0].sourceMap = {content: 'SOURCE MAP b.js'}

it 'should correct rewrite stack traces without sha', (done) ->
emitter.emit 'file_list_modified', q(served: servedFiles)
Expand Down

0 comments on commit fe6ed7e

Please sign in to comment.