Navigation Menu

Skip to content

Commit

Permalink
Use babel-code-frame to generate pretty SyntaxError messages.
Browse files Browse the repository at this point in the history
A developer on my team spent more time than needed debugging a SyntaxError
that was the result of attempting to minify es6 code that snuck into
node_modules/. I think that by providing additional context on the failed file,
we can help people be more productive.

This change uses babel-code-frame to generate an error message that includes a
few lines above/below the encountered SyntaxError.
  • Loading branch information
gdborton committed Mar 15, 2018
1 parent c9f1545 commit 096b179
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/uglifier.js
Expand Up @@ -101,7 +101,8 @@ function processAssets(compilation, options) {
}
})
.catch((e) => {
compilation.errors.push(new Error(`minifying ${assetName}\n${e}`));
const builtError = new Error(`Encountered an error while minifying ${assetName}:\n${e}`);
compilation.errors.push(builtError);
});
});

Expand Down
12 changes: 11 additions & 1 deletion lib/worker.js
@@ -1,3 +1,4 @@
const codeFrame = require('babel-code-frame');
const cache = require('./cache');
const tmpFile = require('./tmp-file');
const BOGUS_SOURCEMAP_STRING = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
Expand All @@ -20,7 +21,16 @@ function minify(source, map, uglifyOptions, uglifier) {
}

const result = uglifier.minify(source, opts);
if (result.error) throw result.error;
if (result.error) {
if (result.error.name === 'SyntaxError') {
const frame = codeFrame(source, result.error.line, result.error.col);
const errorMessage = `${result.error.name}: ${result.error.message}\n${frame}`;
throw new SyntaxError(errorMessage);
}

throw result.error;
}

result.code = result.code.replace(new RegExp(BOGUS_SOURCEMAP_URL), '');

return result;
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -33,6 +33,7 @@
"webpack": "^3.0.0"
},
"dependencies": {
"babel-code-frame": "^6.26.0",
"glob": "^7.0.5",
"mkdirp": "^0.5.1",
"pify": "^3.0.0",
Expand Down

0 comments on commit 096b179

Please sign in to comment.