Skip to content
Open
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
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,24 @@ importResolve({
"~myUniqueAlias": "path/to/unique/file.less"
}
});

// You can pass an array of import paths that you'd like the resolver to ignore. Ignored imports will remain in the output file.
importResolve({
"ext": "less",
"pathToMain": "path/to/main.less",
"output": "path/to/output.less",
"ignorePaths": [
"path/to/ignored/file.less"
]
});

// You can also ignore imports based on a RegExp pattern. Ignored imports will remain in the output file.
importResolve({
"ext": "less",
"pathToMain": "path/to/main.less",
"output": "path/to/output.less",
"ignorePathPattern": /^~/
});
```

## Tests
Expand Down
29 changes: 18 additions & 11 deletions lib/import-resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ var fs = require('fs'),

function ImportResolver (opts) {
this.aliases = opts.aliases || {};
this.ignorePaths = opts.ignorePaths || [];
this.ignorePathPattern = opts.ignorePathPattern;
this.cwd = '';
this.dist = '';
this.output = opts.output;
Expand Down Expand Up @@ -70,17 +72,20 @@ ImportResolver.prototype.getFile = function (filename) {
ImportResolver.prototype.normalizeImport = function (str) {
if (regex.test(str)) {
str = str.replace(regex, function (m, capture) {
var p,
pathToImport,
aliasKeys = Object.keys(this.aliases),
index = aliasKeys && aliasKeys.length ? aliasKeys.indexOf(capture) : -1;
if (index !== -1) {
pathToImport = this.aliases[aliasKeys[index]];
} else {
pathToImport = capture;
if(this.ignorePaths.indexOf(capture) === -1 && (!this.ignorePathPattern || !this.ignorePathPattern.test(capture))) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about inverting the base case here?

var isIgnored = this.ignorePaths.indexOf(capture) !== -1;
var isPatternIgnored = this.ignorePathPattern && this.ignorePathPattern.test(capture)
if (isIgnored || isPatternIgnored) {
    return m;
}
// ...

To the reader, adding another nesting level makes this method seem more complicated than it actually is with these changes.

var p,
pathToImport,
aliasKeys = Object.keys(this.aliases),
index = aliasKeys && aliasKeys.length ? aliasKeys.indexOf(capture) : -1;
if (index !== -1) {
pathToImport = this.aliases[aliasKeys[index]];
} else {
pathToImport = capture;
}
p = path.resolve(this.cwd, pathToImport);
return m && m.replace(capture, p);
}
p = path.resolve(this.cwd, pathToImport);
return m && m.replace(capture, p);
return m;
}.bind(this));
}
return str;
Expand Down Expand Up @@ -117,7 +122,9 @@ ImportResolver.prototype.resolve = function(oldFile){
}

matches.forEach(function (match) {
oldFile = oldFile.replace(match[0], this.read(match[1]));
if(this.ignorePaths.indexOf(match[1]) === -1 && (!this.ignorePathPattern || !new RegExp(this.ignorePathPattern).test(match[1]))) {
oldFile = oldFile.replace(match[0], this.read(match[1]));
}
}.bind(this));

return oldFile;
Expand Down
3 changes: 3 additions & 0 deletions tests/ignore/ignore-two.styl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.import-two {
color: blue;
}
6 changes: 6 additions & 0 deletions tests/ignore/main.styl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@import '~path/to/be/ignored';
@import 'ignore-two';

.ignore {
display: block;
}
22 changes: 22 additions & 0 deletions tests/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,28 @@ describe('importResolve integration', function () {
expect(standardizeChars(output)).toBe('.alias-one {\n color: blue;\n}\n\n.alias-two {\n background: white;\n}\n\n\n.alias {\n display: block;\n}');
});
});

it('should ignore paths specified in the ignorePaths array', function() {
importResolve({
"pathToMain": "tests/ignore/main.styl",
"ext": "styl",
"ignorePaths": [
"~path/to/be/ignored"
]
}, function (output) {
expect(standardizeChars(output)).toBe('@import \'~path/to/be/ignored\';\n.import-two {\n color: blue;\n}\n\n.ignore {\n display: block;\n}');
});
});

it('should ignore paths that match the specified ignorePathPattern', function() {
importResolve({
"pathToMain": "tests/ignore/main.styl",
"ext": "styl",
"ignorePathPattern": /^~/
}, function (output) {
expect(standardizeChars(output)).toBe('@import \'~path/to/be/ignored\';\n.import-two {\n color: blue;\n}\n\n.ignore {\n display: block;\n}');
});
});
});

describe('file write system', function () {
Expand Down
15 changes: 13 additions & 2 deletions tests/lib/import-resolver.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ describe('ImportResolver', function () {
"pathToMain": normalize("baz/bing"),
"aliases": {
"myAlias": "qux"
}
},
"ignorePaths": [
"~path/to/ignore"
],
"ignorePathPattern": /^~/
});
});
afterEach(function () {
Expand All @@ -39,6 +43,12 @@ describe('ImportResolver', function () {
"myAlias": "qux"
});
});
it('should set ignore paths', function () {
expect(subject.ignorePaths).toEqual(["~path/to/ignore"]);
});
it('should set ignore pattern', function () {
expect(subject.ignorePathPattern).toEqual(/^~/);
});
});

describe('#writeToFile', function (done) {
Expand Down Expand Up @@ -130,7 +140,8 @@ describe('ImportResolver', function () {
"cwd": "foo" + path.sep,
"aliases": {
"myAlias": "./bar/baz/qux"
}
},
"ignorePaths": []
});
});
afterEach(function () {
Expand Down
10 changes: 10 additions & 0 deletions tests/output/path/doesnt/exist/yet.styl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
$variable_1 = 16px
$variable_2 = 3em

$another_var = #444

.some-div
background: $another_var

#some-id
font-weight: normal