forked from berstend/gulp-sass-inheritance
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
91 lines (77 loc) · 2.35 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
'use strict';
var path = require('path');
var es = require('event-stream');
var _ = require('lodash');
var fs = require('fs');
var Vinyl = require('vinyl');
var sassGraph = require('sass-graph');
var PLUGIN_NAME = 'gulp-sass-parent';
var stream;
function gulpSassInheritance(options) {
options = options || {};
if (typeof options.dir !== 'string' && !Array.isArray(options.dir)) {
throw new Error('gulp-sass-inheritance: Missing dir in options');
}
if (typeof options.dir === 'string') {
options.dir = [options.dir];
}
var files = [];
function writeStream(currentFile) {
if (currentFile && currentFile.contents.length) {
files.push(currentFile);
}
}
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() {
var stream = this;
if (files.length) {
var allPaths = _.map(files, 'path');
var newFiles = files;
_.forEach(options.dir, function(dir) {
var graph = sassGraph.parseDir(dir, options);
_.forEach(files, function(file) {
if (graph.index && graph.index[file.path]) {
var fullpaths = recureOnImports([],graph, file.path);
fullpaths.forEach(function (fp) {
if (!_.includes(allPaths, fp)) {
allPaths.push(fp);
newFiles.push(new Vinyl({
cwd: file.cwd,
base: path.resolve(dir),
path: fp,
stat: fs.statSync(fp),
contents: fs.readFileSync(fp)
}));
}
});
if (options.debug) {
console.log('File', file.path);
console.log(' - importedBy', fullpaths);
}
}
});
});
es.readArray(files)
.pipe(es.through(
function (f) {
stream.emit('data', f);
},
function () {
stream.emit('end');
}
));
} else {
stream.emit('end');
}
}
stream = es.through(writeStream, endStream);
return stream;
}
module.exports = gulpSassInheritance;