2015-03-27 00:15:59 +01:00
|
|
|
'use strict';
|
|
|
|
|
2015-03-24 11:38:03 +01:00
|
|
|
var gutil = require('gulp-util');
|
|
|
|
var through = require('through2');
|
|
|
|
var assign = require('object-assign');
|
|
|
|
var path = require('path');
|
|
|
|
var sass = require('node-sass');
|
2015-03-24 10:40:48 +01:00
|
|
|
var applySourceMap = require('vinyl-sourcemaps-apply');
|
2013-09-01 01:53:11 +02:00
|
|
|
|
2015-03-23 23:01:12 +01:00
|
|
|
var PLUGIN_NAME = 'gulp-sass';
|
2014-01-05 16:15:20 +01:00
|
|
|
|
2015-03-24 13:32:46 +01:00
|
|
|
//////////////////////////////
|
|
|
|
// Main Gulp Sass function
|
|
|
|
//////////////////////////////
|
2015-03-27 00:42:14 +01:00
|
|
|
var gulpSass = function gulpSass(options, sync) {
|
2015-03-23 23:01:12 +01:00
|
|
|
return through.obj(function(file, enc, cb) {
|
2015-03-24 11:38:03 +01:00
|
|
|
var opts,
|
2015-03-27 01:19:33 +01:00
|
|
|
filePush,
|
2015-03-27 00:42:14 +01:00
|
|
|
callback,
|
|
|
|
result;
|
2015-03-24 11:38:03 +01:00
|
|
|
|
2014-01-05 16:15:20 +01:00
|
|
|
if (file.isNull()) {
|
2014-01-19 09:39:41 +01:00
|
|
|
return cb(null, file);
|
2014-01-05 16:15:20 +01:00
|
|
|
}
|
2015-03-23 23:01:12 +01:00
|
|
|
if (file.isStream()) {
|
|
|
|
return cb(new gutil.PluginError(PLUGIN_NAME, 'Streaming not supported'));
|
|
|
|
}
|
2014-01-20 22:41:36 +01:00
|
|
|
if (path.basename(file.path).indexOf('_') === 0) {
|
|
|
|
return cb();
|
|
|
|
}
|
2014-01-05 16:15:20 +01:00
|
|
|
|
2015-03-24 11:38:03 +01:00
|
|
|
opts = assign({}, options);
|
2014-12-25 06:14:44 +01:00
|
|
|
opts.file = file.path;
|
2013-09-01 06:54:25 +02:00
|
|
|
|
2015-03-24 10:40:48 +01:00
|
|
|
// Generate Source Maps if plugin source-map present
|
|
|
|
if (file.sourceMap) {
|
|
|
|
opts.sourceMap = file.path;
|
2015-03-24 10:51:57 +01:00
|
|
|
opts.omitSourceMapUrl = true;
|
2015-03-24 10:40:48 +01:00
|
|
|
}
|
|
|
|
|
2015-03-27 01:19:33 +01:00
|
|
|
//////////////////////////////
|
|
|
|
// Handles returning the file to the stream
|
|
|
|
//////////////////////////////
|
|
|
|
filePush = function filePush(sassObj) {
|
|
|
|
// Build Source Maps!
|
|
|
|
if (sassObj.map) {
|
|
|
|
applySourceMap(file, JSON.parse(sassObj.map.toString()));
|
|
|
|
}
|
|
|
|
|
|
|
|
file.contents = sassObj.css;
|
|
|
|
file.path = gutil.replaceExtension(file.path, '.css');
|
|
|
|
|
|
|
|
cb(null, file);
|
|
|
|
};
|
|
|
|
|
2015-03-27 00:42:14 +01:00
|
|
|
if (sync !== true) {
|
2015-03-27 01:19:33 +01:00
|
|
|
//////////////////////////////
|
|
|
|
// Async Sass render
|
|
|
|
//////////////////////////////
|
2015-03-27 00:42:14 +01:00
|
|
|
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)
|
|
|
|
));
|
|
|
|
}
|
2015-03-27 01:19:33 +01:00
|
|
|
filePush(obj);
|
2015-03-27 00:42:14 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
sass.render(opts, callback);
|
|
|
|
}
|
|
|
|
else {
|
2015-03-27 01:19:33 +01:00
|
|
|
//////////////////////////////
|
|
|
|
// Sync Sass render
|
|
|
|
//////////////////////////////
|
2015-03-27 00:42:14 +01:00
|
|
|
try {
|
|
|
|
result = sass.renderSync(opts);
|
|
|
|
|
2015-03-27 01:19:33 +01:00
|
|
|
filePush(result);
|
2015-03-27 00:42:14 +01:00
|
|
|
}
|
|
|
|
catch(error) {
|
2015-03-23 23:01:12 +01:00
|
|
|
return cb(new gutil.PluginError(
|
2015-03-24 20:15:54 +01:00
|
|
|
PLUGIN_NAME, error.message + ' ' + gutil.colors.cyan('line ' + error.line) + ' in ' + gutil.colors.magenta(error.file)
|
2015-03-23 23:01:12 +01:00
|
|
|
));
|
2014-02-11 06:01:45 +01:00
|
|
|
}
|
2015-03-27 00:42:14 +01:00
|
|
|
}
|
2015-03-23 23:01:12 +01:00
|
|
|
});
|
2014-01-19 09:39:41 +01:00
|
|
|
};
|
2015-03-24 13:32:46 +01:00
|
|
|
|
2015-03-27 00:42:14 +01:00
|
|
|
//////////////////////////////
|
|
|
|
// Sync Sass render
|
|
|
|
//////////////////////////////
|
|
|
|
gulpSass.sync = function sync(options) {
|
|
|
|
return gulpSass(options, true);
|
|
|
|
};
|
|
|
|
|
2015-03-24 13:32:46 +01:00
|
|
|
//////////////////////////////
|
|
|
|
// Log errors nicely
|
|
|
|
//////////////////////////////
|
|
|
|
gulpSass.logError = function logError(error) {
|
2015-03-25 20:29:30 +01:00
|
|
|
gutil.log(gutil.colors.red('[' + PLUGIN_NAME + '] ') + error.message);
|
2015-03-24 13:32:46 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = gulpSass;
|