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-31 21:55:26 +02:00
|
|
|
ip,
|
2015-03-27 01:19:33 +01:00
|
|
|
filePush,
|
2015-03-31 22:26:29 +02:00
|
|
|
sassMap,
|
|
|
|
sassMapFile,
|
|
|
|
sassFileSrc,
|
2015-03-30 14:16:04 +02:00
|
|
|
errorM,
|
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);
|
2015-03-31 21:55:26 +02:00
|
|
|
opts.data = file.contents.toString();
|
|
|
|
|
|
|
|
if (opts.includePaths) {
|
|
|
|
if (typeof opts.includePaths === 'string') {
|
|
|
|
ip = opts.includePaths;
|
|
|
|
opts.includePaths = [];
|
|
|
|
opts.includePaths.push(ip);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
opts.includePaths = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
opts.includePaths.push(path.dirname(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) {
|
2015-03-31 22:26:29 +02:00
|
|
|
// Transform map into JSON
|
|
|
|
sassMap = JSON.parse(sassObj.map.toString());
|
|
|
|
// Grab the stdout and transform it into stdin
|
|
|
|
sassMapFile = sassMap.file.replace('stdout', 'stdin');
|
|
|
|
// Grab the base file name that's being worked on
|
|
|
|
sassFileSrc = file.path.split('/').pop();
|
|
|
|
// Replace the stdin with the original file name
|
|
|
|
sassMap.sources[sassMap.sources.indexOf(sassMapFile)] = sassFileSrc;
|
|
|
|
// Replace the map file with the original file name
|
|
|
|
sassMap.file = sassFileSrc;
|
|
|
|
// Apply the map
|
|
|
|
applySourceMap(file, sassMap);
|
2015-03-27 01:19:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
file.contents = sassObj.css;
|
|
|
|
file.path = gutil.replaceExtension(file.path, '.css');
|
|
|
|
|
|
|
|
cb(null, file);
|
|
|
|
};
|
|
|
|
|
2015-03-30 14:16:04 +02:00
|
|
|
//////////////////////////////
|
|
|
|
// Handles error message
|
|
|
|
//////////////////////////////
|
|
|
|
errorM = function errorM(error) {
|
2015-03-31 21:55:26 +02:00
|
|
|
var relativePath = '',
|
|
|
|
filePath = error.file === 'stdin' ? file.path : error.file,
|
2015-03-30 14:16:04 +02:00
|
|
|
message = '';
|
|
|
|
|
2015-03-31 21:55:26 +02:00
|
|
|
filePath = filePath ? filePath : file.path;
|
|
|
|
relativePath = path.relative(process.cwd(), filePath);
|
|
|
|
|
2015-03-30 14:16:04 +02:00
|
|
|
message += gutil.colors.underline(relativePath) + '\n';
|
|
|
|
message += gutil.colors.gray(' ' + error.line + ':' + error.column) + ' ';
|
|
|
|
message += error.message;
|
|
|
|
|
|
|
|
return cb(new gutil.PluginError(
|
|
|
|
PLUGIN_NAME, message
|
|
|
|
));
|
|
|
|
};
|
|
|
|
|
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) {
|
2015-03-30 14:16:04 +02:00
|
|
|
return errorM(error);
|
2015-03-27 00:42:14 +01:00
|
|
|
}
|
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-30 14:16:04 +02:00
|
|
|
return errorM(error);
|
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;
|