Skip to content
Browse files

don't write bundles when a syntax error occured and resume writing wh…

…en syntax errors are fixed
  • Loading branch information...
1 parent 62b7662 commit 1f3c544dc63db2477ef1e0a95979a67666a18b5e @substack committed
Showing with 38 additions and 55 deletions.
  1. +3 −6 bin/cmd.js
  2. +3 −17 index.js
  3. +4 −4 lib/watch.js
  4. +28 −28 lib/wrap.js
View
9 bin/cmd.js
@@ -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');
@@ -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);
}
View
20 index.js
@@ -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) {
@@ -165,24 +165,11 @@ 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;
@@ -190,7 +177,6 @@ var exports = module.exports = function (entryFile, opts) {
firstBundle = false;
w._cache = src;
- if (ok) lastOk = src;
return src;
};
View
8 lib/watch.js
@@ -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;
@@ -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') {
View
56 lib/wrap.js
@@ -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;
@@ -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);
@@ -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;
@@ -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;
};

0 comments on commit 1f3c544

Please sign in to comment.
Something went wrong with that request. Please try again.