Skip to content

Commit

Permalink
Merge pull request #1253 from substack/syntax-error-cache
Browse files Browse the repository at this point in the history
Optimization: Add syntax check cache
  • Loading branch information
zertosh committed May 14, 2015
2 parents 9d1d1e3 + 9271862 commit e08391c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 2 deletions.
11 changes: 9 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var isarray = require('isarray');
var defined = require('defined');
var has = require('has');
var sanitize = require('htmlescape').sanitize;
var shasum = require('shasum');

var bresolve = require('browser-resolve');
var resolve = require('resolve');
Expand Down Expand Up @@ -68,6 +69,7 @@ function Browserify (files, opts) {
}
: bresolve
;
self._syntaxCache = {};

var ignoreTransform = [].concat(opts.ignoreTransform).filter(Boolean);
self._filterTransform = function (tr) {
Expand Down Expand Up @@ -627,9 +629,14 @@ Browserify.prototype._unshebang = function () {
};

Browserify.prototype._syntax = function () {
var self = this;
return through.obj(function (row, enc, next) {
var err = syntaxError(row.source, row.file || row.id);
if (err) return this.emit('error', err);
var h = shasum(row.source);
if (typeof self._syntaxCache[h] === 'undefined') {
var err = syntaxError(row.source, row.file || row.id);
if (err) return this.emit('error', err);
self._syntaxCache[h] = true;
}
this.push(row);
next();
});
Expand Down
47 changes: 47 additions & 0 deletions test/syntax_cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
var Seq = require('seq');
var browserify = require('../');
var test = require('tap').test;
var shasum = require('shasum');

test('syntax cache - valid', function (t) {
t.plan(2);

var expectedCache = {}
var cacheKey;

var b = browserify(__dirname + '/syntax_cache/valid.js');
b.once('dep', function(row) {
cacheKey = shasum(row.source);
expectedCache[cacheKey] = true;
});

Seq()
.seq(function() { b.bundle(this); })
.seq(function() {
t.deepEqual(b._syntaxCache, expectedCache);
b._syntaxCache[cacheKey] = expectedCache[cacheKey] = 'beep';
b.bundle(function(err, src) {
// if the cache worked, the "cacheKey"
// should not be reset to "true"
t.deepEqual(b._syntaxCache, expectedCache);
});
});
});

test('syntax cache - skip invalid', function (t) {
t.plan(5);

var b = browserify(__dirname + '/syntax_cache/invalid.js');

Seq()
.seq(function() { b.bundle(this); })
.catch(function(lastErr) {
t.deepEqual(b._syntaxCache, {});
t.similar(String(lastErr), /ParseError/);
b.bundle(function(err, src) {
t.deepEqual(b._syntaxCache, {});
t.similar(String(err), /ParseError/);
t.notEqual(lastErr, err, 'errors should be unique');
});
});
});
2 changes: 2 additions & 0 deletions test/syntax_cache/invalid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var x = {
var y = 6;
2 changes: 2 additions & 0 deletions test/syntax_cache/valid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var x = {};
var y = 6;

0 comments on commit e08391c

Please sign in to comment.