Skip to content

Commit 8eb8b4b

Browse files
committed
Add preferPathResolver option
Let `stylus-loader` users opt for webpack resolving before stylus. This can allow for most common stylus files to be found in parallel through webpack's async resolver instead of blocking on file IO with stylus's sync resolver.
1 parent dfc9e57 commit 8eb8b4b

File tree

2 files changed

+70
-44
lines changed

2 files changed

+70
-44
lines changed

Diff for: README.md

+10
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@ stylus: {
8181

8282
where `~` resolves to `node_modules/`
8383

84+
### Prefer webpack resolving
85+
86+
`stylus-loader` currently prefers resolving paths with stylus's resovling utilities and then falling back to webpack when it can't find files. Use the `preferPathResolver` option with the value `'webpack'` to swap this. This has the benefit of using webpack's async resolving instead of stylus's sync resolving. If you have a lot of dependencies in your stylus files this'll let those dependencies be found in parallel.
87+
88+
```js
89+
styus: {
90+
preferPathResolver: 'webpack',
91+
}
92+
```
93+
8494
## Install
8595

8696
`npm install stylus-loader stylus --save-dev`

Diff for: lib/pathcache.js

+60-44
Original file line numberDiff line numberDiff line change
@@ -88,53 +88,69 @@ PathCache.prototype.allDeps = function() {
8888
function resolvers(options, webpackResolver) {
8989
var evaluator = new Evaluator(nodes.null, options);
9090
var whenWebpackResolver = whenNodefn.lift(webpackResolver);
91-
return [
92-
// Stylus's normal resolver for single files.
93-
function(context, path) {
94-
// Stylus adds .styl to paths for normal "paths" lookup if it isn't there.
95-
if (!/.styl$/.test(path)) {
96-
path += '.styl';
97-
}
9891

99-
var paths = options.paths.concat(context);
100-
var found = utils.find(path, paths, options.filename)
101-
if (found) {
102-
return normalizePaths(found);
103-
}
104-
},
105-
// Stylus's normal resolver for node_modules packages. Cannot locate paths
106-
// inside a package.
107-
function(context, path) {
108-
// Stylus calls the argument name. If it exists it should match the name
109-
// of a module in node_modules.
110-
if (!path) {
111-
return null;
112-
}
92+
// Stylus's normal resolver for single files.
93+
var stylusFile = function(context, path) {
94+
// Stylus adds .styl to paths for normal "paths" lookup if it isn't there.
95+
if (!/.styl$/.test(path)) {
96+
path += '.styl';
97+
}
11398

114-
var paths = options.paths.concat(context);
115-
var found = utils.lookupIndex(path, paths, options.filename);
116-
if (found) {
117-
return {path: normalizePaths(found), index: true};
118-
}
119-
},
120-
// Fallback to resolving with webpack's configured resovler.
121-
function(context, path) {
122-
// Follow the webpack stylesheet idiom of '~path' meaning a path in a
123-
// modules folder and a unprefixed 'path' meaning a relative path like
124-
// './path'.
125-
path = loaderUtils.urlToRequest(path, options.root);
126-
// First try with a '.styl' extension.
127-
return whenWebpackResolver(context, path + '.styl')
128-
// If the user adds ".styl" to resolve.extensions, webpack can find
129-
// index files like stylus but it uses all of webpack's configuration,
130-
// by default for example the module could be web_modules.
131-
.catch(function() { return whenWebpackResolver(context, path); })
132-
.catch(function() { return null; })
133-
.then(function(result) {
134-
return Array.isArray(result) && result[1] && result[1].path || result
135-
});
99+
var paths = options.paths.concat(context);
100+
var found = utils.find(path, paths, options.filename)
101+
if (found) {
102+
return normalizePaths(found);
103+
}
104+
};
105+
106+
// Stylus's normal resolver for node_modules packages. Cannot locate paths
107+
// inside a package.
108+
var stylusIndex = function(context, path) {
109+
// Stylus calls the argument name. If it exists it should match the name
110+
// of a module in node_modules.
111+
if (!path) {
112+
return null;
136113
}
137-
];
114+
115+
var paths = options.paths.concat(context);
116+
var found = utils.lookupIndex(path, paths, options.filename);
117+
if (found) {
118+
return {path: normalizePaths(found), index: true};
119+
}
120+
};
121+
122+
// Fallback to resolving with webpack's configured resovler.
123+
var webpackResolve = function(context, path) {
124+
// Follow the webpack stylesheet idiom of '~path' meaning a path in a
125+
// modules folder and a unprefixed 'path' meaning a relative path like
126+
// './path'.
127+
path = loaderUtils.urlToRequest(path, options.root);
128+
// First try with a '.styl' extension.
129+
return whenWebpackResolver(context, path + '.styl')
130+
// If the user adds ".styl" to resolve.extensions, webpack can find
131+
// index files like stylus but it uses all of webpack's configuration,
132+
// by default for example the module could be web_modules.
133+
.catch(function() { return whenWebpackResolver(context, path); })
134+
.catch(function() { return null; })
135+
.then(function(result) {
136+
return Array.isArray(result) && result[1] && result[1].path || result
137+
});
138+
};
139+
140+
if (options.preferPathResolver === 'webpack') {
141+
return [
142+
webpackResolve,
143+
stylusFile,
144+
stylusIndex
145+
];
146+
}
147+
else {
148+
return [
149+
stylusFile,
150+
stylusIndex,
151+
webpackResolve
152+
];
153+
}
138154
}
139155

140156
function reduceResolvers(resolvers, context, path) {

0 commit comments

Comments
 (0)