Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions sass-graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@

var fs = require('fs');
var path = require('path');
var _ = require('lodash');
var includes = require('lodash/includes');
var intersection = require('lodash/intersection');
var uniq = require('lodash/uniq');
var glob = require('glob');
var parseImports = require('./parse-imports');

function identity (value) {
return value;
}

// resolve a sass module to a path
function resolveSassPath(sassPath, loadPaths, extensions) {
// trim sass file extensions
Expand Down Expand Up @@ -45,13 +51,13 @@ function Graph(options, dir) {
this.exclude = options.exclude instanceof RegExp ? options.exclude : null;
this.index = {};
this.follow = options.follow || false;
this.loadPaths = _(options.loadPaths).map(function(p) {
this.loadPaths = (options.loadPaths || []).map(function(p) {
return path.resolve(p);
}).value();
});

if (dir) {
var graph = this;
_.each(glob.sync(dir+'/**/*.@('+this.extensions.join('|')+')', { dot: true, nodir: true, follow: this.follow }), function(file) {
glob.sync(dir+'/**/*.@('+this.extensions.join('|')+')', { dot: true, nodir: true, follow: this.follow }).forEach(function(file) {
try {
graph.addFile(path.resolve(file));
} catch (e) {}
Expand All @@ -76,23 +82,23 @@ Graph.prototype.addFile = function(filepath, parent) {

var i, length = imports.length, loadPaths, resolved;
for (i = 0; i < length; i++) {
loadPaths = _([cwd, this.dir]).concat(this.loadPaths).filter().uniq().value();
loadPaths = uniq([cwd, this.dir].concat(this.loadPaths).filter(identity));
resolved = resolveSassPath(imports[i], loadPaths, this.extensions);
if (!resolved) continue;

// check exclcude regex
if (this.exclude !== null && this.exclude.test(resolved)) continue;

// recurse into dependencies if not already enumerated
if (!_.includes(entry.imports, resolved)) {
if (!includes(entry.imports, resolved)) {
entry.imports.push(resolved);
this.addFile(fs.realpathSync(resolved), filepath);
}
}

// add link back to parent
if (parent) {
resolvedParent = _(parent).intersection(this.loadPaths).value();
resolvedParent = intersection(parent, this.loadPaths);

if (resolvedParent) {
resolvedParent = parent.substr(parent.indexOf(resolvedParent));
Expand Down Expand Up @@ -134,7 +140,7 @@ Graph.prototype.visit = function(filepath, callback, edgeCallback, visited) {

var i, length = edges.length;
for (i = 0; i < length; i++) {
if (!_.includes(visited, edges[i])) {
if (!includes(visited, edges[i])) {
visited.push(edges[i]);
callback(edges[i], this.index[edges[i]]);
this.visit(edges[i], callback, edgeCallback, visited);
Expand Down