From df32000dd2ee0ec74ec00c1d4385cd874746e6b6 Mon Sep 17 00:00:00 2001 From: David Manning Date: Sat, 25 Jan 2014 09:46:20 -0800 Subject: [PATCH] Added: onError and onSuccess callback options --- README.md | 10 +++++++++- index.js | 8 ++++++++ test/test.js | 14 ++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f475da2..3f13d26 100644 --- a/README.md +++ b/README.md @@ -30,10 +30,18 @@ Options passed as a hash into `sass()` will be passed along to [`node-sass`](htt ## gulp-sass specific options -#### `errLogToConsole` +#### `errLogToConsole: true` If you pass `errLogToConsole: true` into the options hash, sass errors will be logged to the console instead of generating a `gutil.PluginError` object. Use this option with `gulp.watch` to keep gulp from stopping every time you mess up your sass. +#### `onSuccess: callback` + +Pass in your own callback to be called upon successful compilaton by node-sass. The callback has the form `callback(css)`, and is passed the compiled css as a string. Note: This *does not* prevent gulp-sass's default behavior of writing the output css file. + +#### `onError: callback` + +Pass in your own callback to be called upon a sass error from node-sass. The callback has the form `callback(err)`, where err is the error string generated by libsass. Note: this *does* prevent an actual `gulpPluginError` object from being created. + #Imports and Partials gulp-sass now automatically passes along the directory of every scss file it parses as an include path for node-sass. This means that as long as you specify your includes relative to path of your scss file, everything will just work. diff --git a/index.js b/index.js index e087116..4704fbd 100644 --- a/index.js +++ b/index.js @@ -31,6 +31,8 @@ module.exports = function (options) { } opts.success = function (css) { + if (typeof opts.onSuccess === 'function') opts.onSuccess(css); + file.path = ext(file.path, '.css'); file.contents = new Buffer(css); cb(null, file); @@ -41,6 +43,12 @@ module.exports = function (options) { gutil.log('[gulp-sass] ' + err); return cb(); } + + if (typeof opts.onError === 'function') { + opts.onError(err); + return cb(); + } + return cb(new gutil.PluginError('gulp-sass', err)); }; diff --git a/test/test.js b/test/test.js index 3a440dc..978c0a5 100644 --- a/test/test.js +++ b/test/test.js @@ -92,4 +92,18 @@ test('emit error on sass errors', function (t) { t.end(); }); stream.write(errorFile); +}); + +test('call custom error callback when opts.onError is given', function (t) { + var stream = gsass({ onError: function (err) { + t.equal(err, + 'source string:1: error: property "font" must be followed by a \':\'\n' + ); + t.end(); + }}); + + var errorFile = createVinyl('somefile.sass', + new Buffer('body { font \'Comic Sans\'; }')); + + stream.write(errorFile); }); \ No newline at end of file