2014-02-11 06:01:45 +01:00
|
|
|
var fs = require('fs')
|
|
|
|
, map = require('map-stream')
|
2013-09-01 01:53:11 +02:00
|
|
|
, sass = require('node-sass')
|
2014-01-10 17:27:05 +01:00
|
|
|
, path = require('path')
|
|
|
|
, gutil = require('gulp-util')
|
|
|
|
, ext = gutil.replaceExtension
|
2014-09-11 01:24:04 +02:00
|
|
|
, applySourceMap = require('vinyl-sourcemaps-apply')
|
2013-09-01 01:53:11 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
module.exports = function (options) {
|
2014-08-12 21:11:39 +02:00
|
|
|
var opts = options || {};
|
2013-09-01 01:53:11 +02:00
|
|
|
|
|
|
|
function nodeSass (file, cb) {
|
2014-01-23 00:48:40 +01:00
|
|
|
var fileDir = path.dirname(file.path);
|
|
|
|
var addedLocalDirPath = false;
|
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
|
|
|
}
|
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
|
|
|
|
2014-05-10 21:04:51 +02:00
|
|
|
if (file.sourceMap) {
|
|
|
|
opts.sourceComments = 'map';
|
2014-09-11 07:35:08 +02:00
|
|
|
opts.sourceMap = file.path;
|
2014-05-10 21:04:51 +02:00
|
|
|
}
|
|
|
|
|
2014-02-18 21:46:40 +01:00
|
|
|
if (opts.sourceComments === 'map' || opts.sourceComments === 'normal') {
|
2014-08-12 21:11:39 +02:00
|
|
|
opts.sourceMap = opts.sourceMap || '';
|
2014-02-11 06:01:45 +01:00
|
|
|
opts.file = file.path;
|
|
|
|
} else {
|
|
|
|
opts.data = file.contents.toString();
|
|
|
|
}
|
2013-09-01 06:54:25 +02:00
|
|
|
|
2014-01-22 21:52:04 +01:00
|
|
|
if (opts.includePaths && Array.isArray(opts.includePaths)) {
|
|
|
|
if (opts.includePaths.indexOf(fileDir) === -1) {
|
2014-01-23 00:48:40 +01:00
|
|
|
opts.includePaths.push(fileDir);
|
|
|
|
addedLocalDirPath = true;
|
2014-01-22 21:52:04 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
opts.includePaths = [fileDir];
|
|
|
|
}
|
|
|
|
|
2014-02-11 06:01:45 +01:00
|
|
|
opts.success = function (css, map) {
|
|
|
|
var sourceMap;
|
|
|
|
if (typeof opts.onSuccess === 'function') opts.onSuccess(css, map);
|
|
|
|
|
|
|
|
if (map) {
|
2014-05-10 21:04:51 +02:00
|
|
|
// hack to remove the already added sourceMappingURL from libsass
|
|
|
|
css = css.replace(/\n\/\*#\s*sourceMappingURL\=.*\*\//, '');
|
|
|
|
|
2014-09-11 01:24:04 +02:00
|
|
|
applySourceMap(file, map);
|
2014-02-11 06:01:45 +01:00
|
|
|
}
|
2014-01-25 18:46:20 +01:00
|
|
|
|
2013-12-14 17:47:44 +01:00
|
|
|
file.path = ext(file.path, '.css');
|
|
|
|
file.contents = new Buffer(css);
|
|
|
|
cb(null, file);
|
2014-01-19 09:39:41 +01:00
|
|
|
};
|
2013-09-01 06:54:25 +02:00
|
|
|
|
|
|
|
opts.error = function (err) {
|
2014-01-13 07:27:45 +01:00
|
|
|
if (opts.errLogToConsole) {
|
2014-01-19 09:39:41 +01:00
|
|
|
gutil.log('[gulp-sass] ' + err);
|
2014-01-13 07:27:45 +01:00
|
|
|
return cb();
|
|
|
|
}
|
2014-01-25 18:46:20 +01:00
|
|
|
|
2014-08-12 21:11:39 +02:00
|
|
|
if (typeof opts.onError === 'function') {
|
2014-01-25 18:46:20 +01:00
|
|
|
opts.onError(err);
|
|
|
|
return cb();
|
|
|
|
}
|
|
|
|
|
2014-01-10 22:34:30 +01:00
|
|
|
return cb(new gutil.PluginError('gulp-sass', err));
|
2014-01-19 09:39:41 +01:00
|
|
|
};
|
2013-09-01 06:54:25 +02:00
|
|
|
|
2013-09-01 04:00:14 +02:00
|
|
|
sass.render(opts);
|
2014-01-23 00:48:40 +01:00
|
|
|
|
|
|
|
if (addedLocalDirPath) opts.includePaths.pop();
|
|
|
|
|
2013-09-01 01:53:11 +02:00
|
|
|
}
|
|
|
|
|
2014-01-20 19:56:56 +01:00
|
|
|
return map(nodeSass);
|
2014-01-19 09:39:41 +01:00
|
|
|
};
|
2014-02-11 06:01:45 +01:00
|
|
|
|
|
|
|
function getSourcesContent (sources) {
|
|
|
|
sourcesContent = [];
|
|
|
|
|
|
|
|
for (var i = 0; i < sources.length; i++) {
|
|
|
|
sourcesContent[i] = fs.readFileSync(sources[i], { encoding: 'utf8' });
|
|
|
|
}
|
|
|
|
|
|
|
|
return sourcesContent;
|
2014-02-18 21:46:40 +01:00
|
|
|
}
|