Skip to content

Commit

Permalink
Add option to exclude files based on regular expression (xzyfer#86)
Browse files Browse the repository at this point in the history
* Add support for import globbing

* Add test for import globbing

* Update readme for import globbing option

* Forgot semis

* Fix spread on old node versions

* Add option to exclude files matching regex

* Remove changes made in other pr

I should probably have made a new branch for that...

* Cache RegExp check

* Check ancestors descendants

* I should run tests before pushing...

* Add tests

* Remove package-lock.json

* Fix typo in readme
  • Loading branch information
MarcelRobitaille authored and xzyfer committed Apr 16, 2018
1 parent f2606d5 commit 18313be
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -3,3 +3,6 @@ output
.sass-cache
test.js
.nyc_output
*.swp
package-lock.json

7 changes: 7 additions & 0 deletions readme.md
Expand Up @@ -82,6 +82,13 @@ Default: `false`

Follow symbolic links.

#### exclude

Type: `RegExp`
Default: `undefined`

Exclude files matching regular expression.

## Example

```js
Expand Down
11 changes: 10 additions & 1 deletion sass-graph.js
Expand Up @@ -42,6 +42,7 @@ function resolveSassPath(sassPath, loadPaths, extensions) {
function Graph(options, dir) {
this.dir = dir;
this.extensions = options.extensions || [];
this.exclude = options.exclude instanceof RegExp ? options.exclude : null;
this.index = {};
this.follow = options.follow || false;
this.loadPaths = _(options.loadPaths).map(function(p) {
Expand All @@ -58,6 +59,8 @@ function Graph(options, dir) {

// add a sass file to the graph
Graph.prototype.addFile = function(filepath, parent) {
if (this.exclude !== null && this.exclude.test(filepath)) return;

var entry = this.index[filepath] = this.index[filepath] || {
imports: [],
importedBy: [],
Expand All @@ -75,6 +78,9 @@ Graph.prototype.addFile = function(filepath, parent) {
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)) {
entry.imports.push(resolved);
Expand All @@ -92,7 +98,10 @@ Graph.prototype.addFile = function(filepath, parent) {
resolvedParent = parent;
}

entry.importedBy.push(resolvedParent);
// check exclcude regex
if (!(this.exclude !== null && this.exclude.test(resolvedParent))) {
entry.importedBy.push(resolvedParent);
}
}
};

Expand Down
1 change: 1 addition & 0 deletions test/fixtures/exclusion-pattern/also-exclude-me.scss
@@ -0,0 +1 @@

2 changes: 2 additions & 0 deletions test/fixtures/exclusion-pattern/dont-exclude.scss
@@ -0,0 +1,2 @@
@import 'also-exclude-me';

Empty file.
3 changes: 3 additions & 0 deletions test/fixtures/exclusion-pattern/index.scss
@@ -0,0 +1,3 @@
@import "dont-exclude";
@import "exclude-me";

10 changes: 10 additions & 0 deletions test/parse-file.js
Expand Up @@ -160,5 +160,15 @@ describe('sass-graph', function(){
});
});
});

describe('with exclusion pattern', function() {
it('should exclude all files matching the regular expression', function() {
graph({ exclude: /exclude-/ })
.fromFixtureFile('exclusion-pattern')
.assertDecendents([
'dont-exclude.scss',
]);
});
});
});
});

0 comments on commit 18313be

Please sign in to comment.