Skip to content
This repository was archived by the owner on Jul 24, 2024. It is now read-only.
Closed
Show file tree
Hide file tree
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
64 changes: 64 additions & 0 deletions lib/sass-graph/parse-imports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
var tokenizer = require('scss-tokenizer');

function parseImports(content, isIndentedSyntax) {
var tokens = tokenizer.tokenize(content);
var results = [];
var tmp = '';
var inImport = false;
var inParen = false;
var prevToken = tokens[0];

var i, token;
for (i = 1; i < tokens.length; i++) {
token = tokens[i];

if (inImport && !inParen && token[0] === 'string') {
results.push(token[1]);
}
else if (token[1] === 'import' && prevToken[1] === '@') {
if (inImport && !isIndentedSyntax) {
throw new Error('Encountered invalid @import syntax.');
}

inImport = true;
}
else if (inImport && !inParen && (token[0] === 'ident' || token[0] === '/')) {
tmp += token[1];
}
else if (inImport && !inParen && (token[0] === 'space' || token[0] === 'newline')) {
if (tmp !== '') {
results.push(tmp);
tmp = '';

if (isIndentedSyntax) {
inImport = false;
}
}
}
else if (inImport && token[0] === ';') {
inImport = false;

if (tmp !== '') {
results.push(tmp);
tmp = '';
}
}
else if (inImport && token[0] === '(') {
inParen = true;
tmp = '';
}
else if (inParen && token[0] === ')') {
inParen = false;
}

prevToken = token;
}

if (tmp !== '') {
results.push(tmp);
}

return results;
}

module.exports = parseImports;
160 changes: 160 additions & 0 deletions lib/sass-graph/sass-graph.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
'use strict';

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

// resolve a sass module to a path
function resolveSassPath(sassPath, loadPaths, extensions) {
// trim sass file extensions
var re = new RegExp('(.('+extensions.join('|')+'))$', 'i');
var sassPathName = sassPath.replace(re, '');
// check all load paths
var i, j, length = loadPaths.length, scssPath, partialPath;
for (i = 0; i < length; i++) {
for (j = 0; j < extensions.length; j++) {
scssPath = path.normalize(loadPaths[i] + '/' + sassPathName + '.' + extensions[j]);
try {
if (fs.lstatSync(scssPath).isFile()) {
return scssPath;
}
} catch (e) {}
}

// special case for _partials
for (j = 0; j < extensions.length; j++) {
scssPath = path.normalize(loadPaths[i] + '/' + sassPathName + '.' + extensions[j]);
partialPath = path.join(path.dirname(scssPath), '_' + path.basename(scssPath));
try {
if (fs.lstatSync(partialPath).isFile()) {
return partialPath;
}
} catch (e) {}
}
}

// File to import not found or unreadable so we assume this is a custom import
return false;
}

function Graph(options, dir) {
this.dir = dir;
this.extensions = options.extensions || [];
this.index = {};
this.follow = options.follow || false;
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) {
graph.addFile(path.resolve(file));
});
}
}

// add a sass file to the graph
Graph.prototype.addFile = function(filepath, parent) {
var entry = this.index[filepath] = this.index[filepath] || {
imports: [],
importedBy: [],
modified: fs.statSync(filepath).mtime
};

var resolvedParent;
var isIndentedSyntax = path.extname(filepath) === '.sass';
var imports = parseImports(fs.readFileSync(filepath, 'utf-8'), isIndentedSyntax);
var cwd = path.dirname(filepath);

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

// recurse into dependencies if not already enumerated
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();

if (resolvedParent) {
resolvedParent = parent.substr(parent.indexOf(resolvedParent));
} else {
resolvedParent = parent;
}

entry.importedBy.push(resolvedParent);
}
};

// visits all files that are ancestors of the provided file
Graph.prototype.visitAncestors = function(filepath, callback) {
this.visit(filepath, callback, function(err, node) {
if (err || !node) return [];
return node.importedBy;
});
};

// visits all files that are descendents of the provided file
Graph.prototype.visitDescendents = function(filepath, callback) {
this.visit(filepath, callback, function(err, node) {
if (err || !node) return [];
return node.imports;
});
};

// a generic visitor that uses an edgeCallback to find the edges to traverse for a node
Graph.prototype.visit = function(filepath, callback, edgeCallback, visited) {
filepath = fs.realpathSync(filepath);
var visited = visited || [];
if (!this.index.hasOwnProperty(filepath)) {
edgeCallback('Graph doesn\'t contain ' + filepath, null);
}
var edges = edgeCallback(null, this.index[filepath]);

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

function processOptions(options) {
return _.assign({
loadPaths: [process.cwd()],
extensions: ['scss', 'css', 'sass'],
}, options);
}

module.exports.parseFile = function(filepath, options) {
if (fs.lstatSync(filepath).isFile()) {
filepath = path.resolve(filepath);
options = processOptions(options);
var graph = new Graph(options);
graph.addFile(filepath);
return graph;
}
// throws
};

module.exports.parseDir = function(dirpath, options) {
if (fs.lstatSync(dirpath).isDirectory()) {
dirpath = path.resolve(dirpath);
options = processOptions(options);
var graph = new Graph(options, dirpath);
return graph;
}
// throws
};
2 changes: 1 addition & 1 deletion lib/watcher.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var grapher = require('sass-graph'),
var grapher = require('./sass-graph/sass-graph'),
clonedeep = require('lodash/cloneDeep'),
path = require('path'),
config = {},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"node-gyp": "^7.1.0",
"npmlog": "^5.0.0",
"request": "^2.88.0",
"sass-graph": "2.2.5",
"scss-tokenizer": "^0.2.3",
"stdout-stream": "^1.4.0",
"true-case-path": "^1.0.2"
},
Expand Down
4 changes: 4 additions & 0 deletions test/sass-graph/fixtures/folder-with-extension/index.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@import 'nested.scss';
@import '_nested.scss';
@import 'nested.scss/leaf';
@import '_nested.scss/leaf';
1 change: 1 addition & 0 deletions test/sass-graph/fixtures/indented-syntax/_c.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import nested/d
1 change: 1 addition & 0 deletions test/sass-graph/fixtures/indented-syntax/b.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import c.sass
1 change: 1 addition & 0 deletions test/sass-graph/fixtures/indented-syntax/index.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import b
1 change: 1 addition & 0 deletions test/sass-graph/fixtures/indented-syntax/nested/_d.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import e
Empty file.
1 change: 1 addition & 0 deletions test/sass-graph/fixtures/load-path-cwd/_b.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import 'c';
1 change: 1 addition & 0 deletions test/sass-graph/fixtures/load-path-cwd/index.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import "b";
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Should not be loaded
@import 'c';
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Should not be loaded
@import 'd';
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Should not be loaded
@import 'c';
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Should not be loaded
@import 'd';
Empty file.
1 change: 1 addition & 0 deletions test/sass-graph/fixtures/load-path/index.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import "b";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import 'c';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import 'd';
2 changes: 2 additions & 0 deletions test/sass-graph/fixtures/load-path/outside-load-path/_b.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Should not be loaded
@import 'c';
2 changes: 2 additions & 0 deletions test/sass-graph/fixtures/load-path/outside-load-path/_c.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Should not be loaded
@import 'd';
Empty file.
1 change: 1 addition & 0 deletions test/sass-graph/fixtures/mutliple-ancestors/entry_a.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import 'leaf.scss';
1 change: 1 addition & 0 deletions test/sass-graph/fixtures/mutliple-ancestors/entry_b.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import 'leaf';
1 change: 1 addition & 0 deletions test/sass-graph/fixtures/mutliple-ancestors/entry_c.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import '_leaf';
1 change: 1 addition & 0 deletions test/sass-graph/fixtures/mutliple-ancestors/entry_d.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import '_leaf.scss';
4 changes: 4 additions & 0 deletions test/sass-graph/fixtures/no-imports/index.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@import 'no-such-file';
@import 'no-such-file.css';
@import 'no-such-file.scss';
@import 'http://no-such-file.com';
1 change: 1 addition & 0 deletions test/sass-graph/fixtures/simple/_c.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import 'nested/d';
1 change: 1 addition & 0 deletions test/sass-graph/fixtures/simple/b.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import 'c.scss';
1 change: 1 addition & 0 deletions test/sass-graph/fixtures/simple/index.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import "b";
1 change: 1 addition & 0 deletions test/sass-graph/fixtures/simple/nested/_d.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import 'e';
Empty file.
30 changes: 30 additions & 0 deletions test/sass-graph/parse-directory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
var assert = require('assert').strict;
var path = require('path');
var sassGraph = require('../../lib/sass-graph/sass-graph');
var graph = require('./util').graph;

describe('sass-graph', function(){
describe('parseDir', function () {
describe('with a simple graph', function() {
it('should return a graph', function() {
graph().fromFixtureDir('simple').assertDecendents([
'b.scss',
'_c.scss',
path.join('nested', '_d.scss'),
path.join('nested', '_e.scss'),
]);
});
});

describe('with mutliple ancestors', function() {
it('should return a graph', function() {
graph().fromFixtureDir('mutliple-ancestors').assertAncestors('_leaf.scss', [
'entry_a.scss',
'entry_b.scss',
'entry_c.scss',
'entry_d.scss',
]);
});
});
});
});
Loading