gulp-sass-multi-inheritance
Version:
Rebuild only changed sass/scss files.
85 lines (68 loc) • 2.65 kB
JavaScript
;
var es = require('event-stream');
var _ = require("lodash");
var vfs = require('vinyl-fs');
var sassGraph = require('sass-graph');
var PLUGIN_NAME = 'gulp-sass-multi-inheritance';
var stream;
function gulpSassInheritance(options) {
options = options || {};
var files = [];
function writeStream(currentFile) {
if (currentFile && currentFile.contents.length) {
files.push(currentFile);
}
}
//sonradan eklendi
function recureOnImports(acc, graph, filePath) {
var fullpaths = graph.index[filePath].importedBy
return fullpaths.reduce(
function (acc, thePath) {
return acc.concat(thePath, graph.index[thePath].importedBy.reduce(
function (acc, aPath) {
return acc.concat(aPath, recureOnImports([], graph, aPath))
}, []))
}, acc)
}
function endStream() {
if (files.length) {
var filesPaths = [];
var graph = sassGraph.parseDir(options.dir, options)
_.forEach(files, function (file) {
filesPaths.push(file.path); //for not imported file, I mean like outputted file Ex:file.scss
if (graph.index && graph.index[file.path]) {
var fullpaths = recureOnImports([], graph, file.path);
if (options.debug) {
console.log('File : ', file.path);
console.log('importedBy > ', fullpaths);
}
filesPaths = _.union(filesPaths, fullpaths);
}
});
if (options.allowPathContain) {
for (var i = filesPaths.length - 1; i >= 0; i--) {
if (filesPaths[i].indexOf(options.allowPathContain) === -1) filesPaths.splice(i, 1);
}
//console.log(filesPaths); //for test
}
if (filesPaths.length) {
vfs.src(filesPaths, { 'base': options.dir })
.pipe(es.through(
function (f) {
stream.emit('data', f);
},
function () {
stream.emit('end');
}
));
} else {
stream.emit('end');
}
} else {
stream.emit('end');
}
}
stream = es.through(writeStream, endStream);
return stream;
};
module.exports = gulpSassInheritance;