Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simpler error handling #6

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 10 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,11 @@ function getHash(data) {
.digest('hex');
}

function handleError(compiled, stderr) {
stderr.write(compiled.error);
return 'console.log("Compile Error: ' + compiled.error + '");';
}

function compileFile(file, src, stderr) {
function compileFile(file, src) {
var compiled;
try {
compiled = compile(file, src);
} catch (err) {
return handleError({ error: err.message }, stderr);
}
compiled = compile(file, src);
if (compiled.error) throw new Error(compiled.error);

if (compiled.error) return handleError(compiled, stderr);
var comment = convert
.fromJSON(compiled.sourcemap)
// override sources that traceur adds i.e. in cases that require the runtime like generators it adds genratorWrap@runtime
Expand All @@ -38,9 +29,8 @@ function compileFile(file, src, stderr) {
return compiled.source + '\n' + comment;
}

function es6ify(filePattern, stderr) {
function es6ify(filePattern) {
filePattern = filePattern || /\.js/;
stderr = stderr || process.stderr;

return function (file) {
if (!filePattern.test(file)) return through();
Expand All @@ -54,7 +44,12 @@ function es6ify(filePattern, stderr) {
, cached = cache[file];

if (!cached || cached.hash !== hash) {
cache[file] = { compiled: compileFile(file, data, stderr), hash: hash };
try {
cache[file] = { compiled: compileFile(file, data), hash: hash };
} catch (ex) {
this.emit('error', ex);
return this.queue(null);
}
}

this.queue(cache[file].compiled);
Expand Down
1 change: 1 addition & 0 deletions test/bundle/syntax-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
function () {
19 changes: 19 additions & 0 deletions test/errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';
/*jshint asi: true */

var test = require('tap').test;
var browserify = require('browserify');
var es6ify = require('..');
var path = require('path');

test('\nsyntax error', function (t) {
t.plan(1);

browserify()
.transform(es6ify)
.require(__dirname + '/bundle/syntax-error.js', { entry: true })
.bundle(function (err, src) {
t.equal(err.message, path.resolve(__dirname + '/bundle/syntax-error.js') + ':1:13 \'}\' expected');
t.end();
});
});
5 changes: 3 additions & 2 deletions test/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var test = require('tap').test

test('transform adds sourcemap comment and uses cache on second time', function (t) {

t.plan(2);
t.plan(3);
var data = '';
var compiles = 0;

Expand Down Expand Up @@ -45,10 +45,11 @@ test('transform adds sourcemap comment and uses cache on second time', function
file: file + '.es6',
sources: [ file ],
names: [],
mappings: 'AAAA,MAAA,CAAA,OAAA,EAAiB,SAAA,CAAU;;ACEf,0CAAiD,CDDvC,CAAC,CAAA,CAAG,EAAA,CAAG,EAAA,CAAA,CAAA;ACErB;AACE,WAAA,EAAO,IAAA;;;;;ADHgB;AAC7B,mBAAA,CAAA,GAAW,CAAC,UAAA,CAAY,QAAA,CAAA;AAAA;AAAA;AAAA;AAAA,KCMlB,MAAA,EAAM,CAAA,CAAG;AACT,QAAA,EAAI,gCAAkC,CAAC,CAAA,CAAA,CACrC,MAAM,EAAA;AAAA;AAAA;AAAA,CAAA',
mappings: sourceMap.mappings,
sourcesContent: [ 'module.exports = function () {\n for (let element of [1, 2, 3]) {\n console.log(\'element:\', element);\n }\n};\n' ] }
, 'adds sourcemap comment including original source'
);
t.ok(sourceMap.mappings.length);
t.equal(compiles, 1, 'compiles only the first time');
}
});