forked from xzyfer/gulp-sass-graph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
149 lines (125 loc) · 4.16 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
'use strict';
var fs = require('fs');
var path = require('path');
var gutil = require('gulp-util');
var through = require('through2');
var _ = require('lodash');
var path = require('path');
var glob = require('glob');
var File = require('vinyl');
module.exports = function (loadPaths) {
var graph = {}
// adds a sass file to a graph of dependencies
var addToGraph = function(filepath, contents, parent) {
var entry = graph[filepath] = graph[filepath] || {
path: filepath,
imports: [],
importedBy: [],
modified: fs.statSync(filepath).mtime
};
var imports = sassImports(contents());
var cwd = path.dirname(filepath)
for (var i in imports) {
var resolved = sassResolve(imports[i], loadPaths.concat([cwd]));
if (!resolved) return false;
// recurse into dependencies if not already enumerated
if(!_.contains(entry.imports, resolved)) {
entry.imports.push(resolved);
addToGraph(resolved, function() {
return fs.readFileSync((path.extname(resolved) != "" ?
resolved : resolved+".scss"),'utf8')
}, filepath);
}
}
// add link back to parent
if(parent != null) {
entry.importedBy.push(parent);
}
return true;
}
// visits all files that are ancestors of the provided file
var visitAncestors = function(filepath, callback, visited) {
visited = visited || [];
var edges = graph[filepath].importedBy;
for(var i in edges) {
if(!_.contains(visited, edges[i])) {
visited.push(edges[i]);
callback(graph[edges[i]]);
visitAncestors(edges[i], callback, visited);
}
}
}
// parses the imports from sass
var sassImports = function(content) {
var re = /\@import (["'])(.+?)\1;/g, match = {}, results = [];
// strip comments
content = new String(content).replace(/\/\*.+?\*\/|\/\/.*(?=[\n\r])/g, '');
// extract imports
while (match = re.exec(content)) {
results.push(match[2]);
}
return results;
};
// resolve a relative path to an absolute path
var sassResolve = function(path, loadPaths) {
for(var p in loadPaths) {
var scssPath = loadPaths[p] + "/" + path + ".scss"
if (fs.existsSync(scssPath)) {
return scssPath;
}
var partialPath = scssPath.replace(/\/([^\/]*)$/, '/_$1');
if (fs.existsSync(partialPath)) {
return partialPath
}
}
console.warn("failed to resolve %s from ", path, loadPaths)
return false;
}
// builds the graph
_(loadPaths).forEach(function(path) {
_(glob.sync(path+"/**/*.scss", {})).forEach(function(file){
if(!addToGraph(file, function() { return fs.readFileSync(file) })) {
console.warn("failed to add %s to graph", file)
}
});
});
return through.obj(function (file, enc, cb) {
if (file.isNull()) {
this.push(file);
return cb();
}
if (file.isStream()) {
this.emit('error',
new gutil.PluginError('gulp-sass-graph', 'Streaming not supported'));
return cb();
}
fs.stat(file.path, function (err, stats) {
if (err) {
// pass through if it doesn't exist
if (err.code === 'ENOENT') {
this.push(file);
return cb();
}
this.emit('error', new gutil.PluginError('gulp-sass-graph', err));
this.push(file);
return cb();
}
var relativePath = file.path.substr(file.cwd.length+1);
console.log("processing %s", relativePath);
if(!graph[relativePath]) {
addToGraph(relativePath, function() { return file.contents.toString('utf8') });
}
this.push(file);
// push ancestors into the pipeline
visitAncestors(relativePath, function(node){
console.log("processing %s, which depends on %s", node.path, relativePath)
this.push(new File({
cwd: file.cwd,
base: file.base,
path: node.path
}));
}.bind(this));
cb();
}.bind(this));
});
};