Added Sync option
Addes `sass.sync` to allow users to compile synchronously. Wrapped the async callback in an `if` statement controlled by a `sync` option for the main function Added `renderSync` render option
This commit is contained in:
parent
c1d2696a3b
commit
f416379458
62
index.js
62
index.js
|
@ -12,10 +12,11 @@ var PLUGIN_NAME = 'gulp-sass';
|
|||
//////////////////////////////
|
||||
// Main Gulp Sass function
|
||||
//////////////////////////////
|
||||
var gulpSass = function gulpSass(options) {
|
||||
var gulpSass = function gulpSass(options, sync) {
|
||||
return through.obj(function(file, enc, cb) {
|
||||
var opts,
|
||||
callback;
|
||||
callback,
|
||||
result;
|
||||
|
||||
if (file.isNull()) {
|
||||
return cb(null, file);
|
||||
|
@ -36,27 +37,56 @@ var gulpSass = function gulpSass(options) {
|
|||
opts.omitSourceMapUrl = true;
|
||||
}
|
||||
|
||||
callback = function(error, obj) {
|
||||
if (error) {
|
||||
if (sync !== true) {
|
||||
callback = function(error, obj) {
|
||||
if (error) {
|
||||
return cb(new gutil.PluginError(
|
||||
PLUGIN_NAME, error.message + ' ' + gutil.colors.cyan('line ' + error.line) + ' in ' + gutil.colors.magenta(error.file)
|
||||
));
|
||||
}
|
||||
// Build Source Maps!
|
||||
if (obj.map) {
|
||||
applySourceMap(file, JSON.parse(obj.map.toString()));
|
||||
}
|
||||
|
||||
file.contents = obj.css;
|
||||
file.path = gutil.replaceExtension(file.path, '.css');
|
||||
|
||||
cb(null, file);
|
||||
};
|
||||
|
||||
sass.render(opts, callback);
|
||||
}
|
||||
else {
|
||||
try {
|
||||
result = sass.renderSync(opts);
|
||||
|
||||
// Build Source Maps!
|
||||
if (result.map) {
|
||||
applySourceMap(file, JSON.parse(result.map.toString()));
|
||||
}
|
||||
|
||||
file.contents = result.css;
|
||||
file.path = gutil.replaceExtension(file.path, '.css');
|
||||
|
||||
cb(null, file);
|
||||
}
|
||||
catch(error) {
|
||||
return cb(new gutil.PluginError(
|
||||
PLUGIN_NAME, error.message + ' ' + gutil.colors.cyan('line ' + error.line) + ' in ' + gutil.colors.magenta(error.file)
|
||||
));
|
||||
}
|
||||
// Build Source Maps!
|
||||
if (obj.map) {
|
||||
applySourceMap(file, JSON.parse(obj.map.toString()));
|
||||
}
|
||||
|
||||
file.contents = obj.css;
|
||||
file.path = gutil.replaceExtension(file.path, '.css');
|
||||
|
||||
cb(null, file);
|
||||
};
|
||||
|
||||
sass.render(opts, callback);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
//////////////////////////////
|
||||
// Sync Sass render
|
||||
//////////////////////////////
|
||||
gulpSass.sync = function sync(options) {
|
||||
return gulpSass(options, true);
|
||||
};
|
||||
|
||||
//////////////////////////////
|
||||
// Log errors nicely
|
||||
//////////////////////////////
|
||||
|
|
Loading…
Reference in a new issue