Skip to content

Consistent format for CSS syntax errors #87

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

Merged
merged 1 commit into from
Aug 17, 2016
Merged
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
37 changes: 33 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
var loaderUtils = require('loader-utils');
var postcss = require('postcss');
var formatCodeFrame = require('babel-code-frame');
var loaderUtils = require('loader-utils');
var postcss = require('postcss');

function formatMessage(message, loc, source) {
var formatted = message;
if (loc) {
formatted = formatted +
' (' + loc.line + ':' + loc.column + ')';
}
if (loc && source) {
formatted = formatted +
'\n\n' + formatCodeFrame(source, loc.line, loc.column) + '\n';
}
return formatted;
}

function PostCSSLoaderError(name, message, loc, source, error) {
Error.call(this);
Error.captureStackTrace(this, PostCSSLoaderError);
this.name = name;
this.error = error;
this.message = formatMessage(message, loc, source);
this.hideStack = true;
}

PostCSSLoaderError.prototype = Object.create(Error.prototype);
PostCSSLoaderError.prototype.constructor = PostCSSLoaderError;

module.exports = function (source, map) {
if ( this.cacheable ) this.cacheable();
Expand Down Expand Up @@ -68,8 +94,11 @@ module.exports = function (source, map) {
})
.catch(function (error) {
if ( error.name === 'CssSyntaxError' ) {
loader.emitError(error.message + error.showSourceCode());
callback();
callback(new PostCSSLoaderError(
'Syntax Error',
error.reason,
{ line: error.line, column: error.column },
error.input.source));
} else {
callback(error);
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"license": "MIT",
"repository": "postcss/postcss-loader",
"dependencies": {
"babel-code-frame": "^6.11.0",
"loader-utils": "^0.2.15",
"postcss": "^5.1.2"
},
Expand Down