Skip to content

Commit

Permalink
don't write bundles when a syntax error occured and resume writing wh…
Browse files Browse the repository at this point in the history
…en syntax errors are fixed
  • Loading branch information
James Halliday committed Aug 2, 2012
1 parent 62b7662 commit 1f3c544
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 55 deletions.
9 changes: 3 additions & 6 deletions bin/cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ if (argv.ignore) bundle.ignore(argv.ignore);
if (argv.outfile) {
function write () {
var src = bundle.bundle();
if (!bundle.ok) return;

fs.writeFile(argv.outfile, src, function () {
if (argv.verbose) {
console.log(Buffer(src).length + ' bytes written');
Expand All @@ -155,10 +157,5 @@ if (argv.outfile) {
}
else {
var src = bundle.bundle();
try { Function(src) }
catch (err) {
console.error(err.stack);
process.exit(1);
}
console.log(src);
if (bundle.ok) console.log(src);
}
20 changes: 3 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ var exports = module.exports = function (entryFile, opts) {
else next()
};

if (opts.watch) watch(self, w, opts.watch);
if (opts.watch) watch(w, opts.watch);

if (opts.filter) {
w.register('post', function (body) {
Expand Down Expand Up @@ -165,32 +165,18 @@ var exports = module.exports = function (entryFile, opts) {
var firstBundle = true;
self.modified = new Date;

var ok = true;
self.on('bundle', function () {
ok = true;
});

self.on('syntaxError', function (err) {
ok = false;
if (self.listeners('syntaxError').length <= 1) {
console.error(err && err.stack || err);
}
});

var lastOk = null;
self.bundle = function () {
if (!ok && w._cache) return w._cache;
if (!ok && lastOk) return lastOk;
if (w._cache) return w._cache;

var src = w.bundle.apply(w, arguments);
self.ok = Object.keys(w.errors).length === 0;

if (!firstBundle) {
self.modified = new Date;
}
firstBundle = false;

w._cache = src;
if (ok) lastOk = src;
return src;
};

Expand Down
8 changes: 4 additions & 4 deletions lib/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ var fs = require('fs');
var path = require('path');
var exists = fs.exists || path.exists;

module.exports = function (self, w, opts) {
module.exports = function (w, opts) {
if (!w.watches) w.watches = [];
w.register(reg.bind(null, self, w, opts));
w.register(reg.bind(null, w, opts));
};

function reg (self, w, opts, body, file) {
function reg (w, opts, body, file) {
// if already being watched
if (w.watches[file]) return body;

Expand Down Expand Up @@ -54,7 +54,7 @@ function reg (self, w, opts, body, file) {
}

w._cache = null;
self.emit('bundle');
w.emit('bundle');
}, 100);
}
else if (event === 'rename') {
Expand Down
56 changes: 28 additions & 28 deletions lib/wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ function idFromPath (path) {
}

function Wrap (opts) {
var self = this;
process.nextTick(function () {
self._ticked = true;
});

var home = process.env.HOME || process.env.USERPROFILE;
if (opts.cache === undefined && home !== undefined) {
opts.cache = true;
Expand All @@ -35,36 +30,37 @@ function Wrap (opts) {
if (opts.cache) {
if (typeof opts.cache === 'boolean') {
var file = home + '/.config/browserify/cache.json';
self.detective = deputy(file);
this.detective = deputy(file);
}
else {
self.detective = deputy(opts.cache);
this.detective = deputy(opts.cache);
}
}
else {
self.detective = detective;
this.detective = detective;
}

self.exports = opts.exports;
this.exports = opts.exports;

self.files = {};
self.filters = [];
self.pathFilters = [];
self.postFilters = [];
self.preFilters = [];
self.aliases = {};
self._checkedPackages = {};
this.files = {};
this.filters = [];
this.pathFilters = [];
this.postFilters = [];
this.preFilters = [];
this.aliases = {};
this._checkedPackages = {};
this.errors = {};

self.ignoring = {};
self.extensions = [ '.js' ];
this.ignoring = {};
this.extensions = [ '.js' ];

self.prepends = [ wrappers.prelude, wrappers.process ];
self.appends = []
self.entries = {};
self.debug = opts.debug;
this.prepends = [ wrappers.prelude, wrappers.process ];
this.appends = []
this.entries = {};
this.debug = opts.debug;

self.require('path');
self.require('__browserify_process');
this.require('path');
this.require('__browserify_process');
}

util.inherits(Wrap, EventEmitter);
Expand Down Expand Up @@ -128,6 +124,8 @@ Wrap.prototype.register = function (ext, fn) {

Wrap.prototype.reload = function (file) {
var self = this;
delete self.errors[file];

if (self.files[file]) {
var f = self.files[file];
f.body = undefined;
Expand Down Expand Up @@ -161,14 +159,16 @@ Wrap.prototype.readFile = function (file) {

var err = checkSyntax(body, file);
if (err) {
if (self._ticked) {
self.emit('syntaxError', err);
}
else process.nextTick(function () {
self.errors[file] = err;

process.nextTick(function () {
self.emit('syntaxError', err);
});
return undefined;
}
else {
delete self.errors[file];
}

return body;
};
Expand Down

0 comments on commit 1f3c544

Please sign in to comment.