Skip to content

Commit 466faa8

Browse files
MarcelRobitaillexzyfer
authored andcommittedApr 16, 2018
Add option to exclude files based on regular expression (#86)
* 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
1 parent 7fbc560 commit 466faa8

File tree

8 files changed

+36
-1
lines changed

8 files changed

+36
-1
lines changed
 

‎.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@ output
33
.sass-cache
44
test.js
55
.nyc_output
6+
*.swp
7+
package-lock.json
8+

‎readme.md

+7
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ Default: `false`
8282

8383
Follow symbolic links.
8484

85+
#### exclude
86+
87+
Type: `RegExp`
88+
Default: `undefined`
89+
90+
Exclude files matching regular expression.
91+
8592
## Example
8693

8794
```js

‎sass-graph.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ function resolveSassPath(sassPath, loadPaths, extensions) {
4242
function Graph(options, dir) {
4343
this.dir = dir;
4444
this.extensions = options.extensions || [];
45+
this.exclude = options.exclude instanceof RegExp ? options.exclude : null;
4546
this.index = {};
4647
this.follow = options.follow || false;
4748
this.loadPaths = _(options.loadPaths).map(function(p) {
@@ -58,6 +59,8 @@ function Graph(options, dir) {
5859

5960
// add a sass file to the graph
6061
Graph.prototype.addFile = function(filepath, parent) {
62+
if (this.exclude !== null && this.exclude.test(filepath)) return;
63+
6164
var entry = this.index[filepath] = this.index[filepath] || {
6265
imports: [],
6366
importedBy: [],
@@ -75,6 +78,9 @@ Graph.prototype.addFile = function(filepath, parent) {
7578
resolved = resolveSassPath(imports[i], loadPaths, this.extensions);
7679
if (!resolved) continue;
7780

81+
// check exclcude regex
82+
if (this.exclude !== null && this.exclude.test(resolved)) continue;
83+
7884
// recurse into dependencies if not already enumerated
7985
if (!_.includes(entry.imports, resolved)) {
8086
entry.imports.push(resolved);
@@ -92,7 +98,10 @@ Graph.prototype.addFile = function(filepath, parent) {
9298
resolvedParent = parent;
9399
}
94100

95-
entry.importedBy.push(resolvedParent);
101+
// check exclcude regex
102+
if (!(this.exclude !== null && this.exclude.test(resolvedParent))) {
103+
entry.importedBy.push(resolvedParent);
104+
}
96105
}
97106
};
98107

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@import 'also-exclude-me';
2+

‎test/fixtures/exclusion-pattern/exclude-me.scss

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@import "dont-exclude";
2+
@import "exclude-me";
3+

‎test/parse-file.js

+10
Original file line numberDiff line numberDiff line change
@@ -160,5 +160,15 @@ describe('sass-graph', function(){
160160
});
161161
});
162162
});
163+
164+
describe('with exclusion pattern', function() {
165+
it('should exclude all files matching the regular expression', function() {
166+
graph({ exclude: /exclude-/ })
167+
.fromFixtureFile('exclusion-pattern')
168+
.assertDecendents([
169+
'dont-exclude.scss',
170+
]);
171+
});
172+
});
163173
});
164174
});

0 commit comments

Comments
 (0)
Please sign in to comment.