Skip to content

Commit dfc9e57

Browse files
committed
Make PathCache stylus resolving a little more like stylus standalone
When running stylus outside of stylus-loader it synchronously resolves files while evaluating. While it evaluates as it imports a file, it adds the context (its directory path) of that file to the stylus instance's `paths` array. `paths` is used to look up relative paths by iterating from the end of the array to the start. `stylus-loader` since we added PathCache, left paths alone. This was an oversight that leads to relative paths not working (unless stylus can't find it and we fall back to resolving with webpack). `stylus-loader` can't modify paths like stylus does since it can potentially resolve multiple dependencies at once. We can concat and create separate `paths` copies. This change does that by appending the context variable that for stylus resolving PathCache was otherwise throwing away. This doesn't accurately follow stylus yet. If you had a tree of stylus files depending on each other `paths` would have a layer for each context as it goes deeper. So this change just represents the last context and not the contexts in between. This should hopefully cover 99% of relative path use.
1 parent 28c0c5f commit dfc9e57

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

lib/pathcache.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ function resolvers(options, webpackResolver) {
9696
path += '.styl';
9797
}
9898

99-
var found = utils.find(path, options.paths, options.filename)
99+
var paths = options.paths.concat(context);
100+
var found = utils.find(path, paths, options.filename)
100101
if (found) {
101102
return normalizePaths(found);
102103
}
@@ -110,7 +111,8 @@ function resolvers(options, webpackResolver) {
110111
return null;
111112
}
112113

113-
var found = utils.lookupIndex(path, options.paths, options.filename);
114+
var paths = options.paths.concat(context);
115+
var found = utils.lookupIndex(path, paths, options.filename);
114116
if (found) {
115117
return {path: normalizePaths(found), index: true};
116118
}

test/basic.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,5 +164,6 @@ describe("basic", function() {
164164
(typeof css).should.be.eql("string");
165165
css.should.match(/\.a-color/);
166166
css.should.match(/\.b-color/);
167+
css.should.not.match(/\.c-color/);
167168
});
168169
});

test/fixtures/context/color.styl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.c-color {
2+
color: #ccc;
3+
}

0 commit comments

Comments
 (0)