Skip to content

Commit 408c8a1

Browse files
author
benholloway
committed
catch CSS syntax errors before rework-css parses
1 parent a23a66f commit 408c8a1

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

index.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,27 @@ module.exports = function loader(content, sourceMap) {
4242
}
4343

4444
// process
45-
return rework(content)
46-
.use(reworkPlugin)
47-
.toString({
48-
sourcemap: this.sourceMap || options.sourceMap
49-
});
45+
// rework will throw on css syntax errors
46+
var FILENAME_PLACEHOLDER = '<filename>';
47+
try {
48+
return rework(content, { source: FILENAME_PLACEHOLDER })
49+
.use(reworkPlugin)
50+
.toString({
51+
sourcemap: this.sourceMap || options.sourceMap
52+
});
53+
}
54+
// fail gracefully
55+
catch(exception) {
56+
var message = ('CSS syntax error (resolve-url-loader did not operate)' + exception.message)
57+
.replace(FILENAME_PLACEHOLDER, '');
58+
if (options.fail) {
59+
this.emitError(message);
60+
}
61+
else if (!options.silent) {
62+
this.emitWarning(message);
63+
}
64+
return content; // original content unchanged
65+
}
5066

5167
/**
5268
* Convert each relative file in the given array to absolute path.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "resolve-url-loader",
3-
"version": "1.0.3",
3+
"version": "1.1.0",
44
"description": "Webpack loader that resolves relative paths in url() statements based on the original source file",
55
"main": "index.js",
66
"scripts": {

readme.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ require('!style!css!resolve-url!sass?sourceMap!./file.scss');
4040

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

43+
In some use cases (no preceding transpiler) there will be no incoming source map. Therefore we do not warn if the
44+
source-map is missing.
45+
4346
### Apply via webpack config
4447

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

6972
* `sourceMap` Generate a source-map.
7073

74+
* `silent` Do not display warnings on CSS syntax error.
75+
76+
* `fail` Syntax errors will result in an error.
77+
7178
## How it works
7279

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

0 commit comments

Comments
 (0)