Skip to content

Commit

Permalink
catch CSS syntax errors before rework-css parses
Browse files Browse the repository at this point in the history
  • Loading branch information
benholloway committed Aug 24, 2015
1 parent a23a66f commit 408c8a1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
26 changes: 21 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,27 @@ module.exports = function loader(content, sourceMap) {
}

// process
return rework(content)
.use(reworkPlugin)
.toString({
sourcemap: this.sourceMap || options.sourceMap
});
// rework will throw on css syntax errors
var FILENAME_PLACEHOLDER = '<filename>';
try {
return rework(content, { source: FILENAME_PLACEHOLDER })
.use(reworkPlugin)
.toString({
sourcemap: this.sourceMap || options.sourceMap
});
}
// fail gracefully
catch(exception) {
var message = ('CSS syntax error (resolve-url-loader did not operate)' + exception.message)
.replace(FILENAME_PLACEHOLDER, '');
if (options.fail) {
this.emitError(message);
}
else if (!options.silent) {
this.emitWarning(message);
}
return content; // original content unchanged
}

/**
* Convert each relative file in the given array to absolute path.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "resolve-url-loader",
"version": "1.0.3",
"version": "1.1.0",
"description": "Webpack loader that resolves relative paths in url() statements based on the original source file",
"main": "index.js",
"scripts": {
Expand Down
7 changes: 7 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ require('!style!css!resolve-url!sass?sourceMap!./file.scss');

Note that **source maps** must be enabled on any preceding loader. In the above example we use `sass?sourceMap`.

In some use cases (no preceding transpiler) there will be no incoming source map. Therefore we do not warn if the
source-map is missing.

### Apply via webpack config

It is preferable to adjust your `webpack.config` so to avoid having to prefix every `require()` statement:
Expand Down Expand Up @@ -68,6 +71,10 @@ if you know what you are doing.

* `sourceMap` Generate a source-map.

* `silent` Do not display warnings on CSS syntax error.

* `fail` Syntax errors will result in an error.

## How it works

The incoming source-map is used to resolve the original file. This is necessary where there was some preceding transpile
Expand Down

0 comments on commit 408c8a1

Please sign in to comment.