shama/stylus-loader — pull request #7 by mzgoddard (2014-05-15 18:35:49+00:00)
Title: Webpack resolve!

**This PR is a work in progress. Do not Merge.** Thought I'd post at this point for comments while I improve style, naming, and comments.

Add webpack resolving!! This lets stylus files `@import 'some-module-that-webpack-can-find'`. It improves how import paths are gathered from files and uses stylus's behaviours to find files in the `paths` options or how it find files in `node_modules`. stylus's behaviour is given priority with webpack resolution as a fallback, but with all the configuration webpack gives! (This is great for different module directories like bower_components or web_modules but also configurable package mains.)

Resolving dependencies in this changeset is two steps. First it finds paths to resolve, resolves those paths, and then does the same for the new files. The second step is performed by stylus's renderer which uses a subclassed `Evaluator` to replace `#visitImport` with a slight change that looks to see if a path was found during the first pass, then fallback to normal stylus behaviour (it needs to do this to find `stylus/lib/functions/index.styl`).

---

Comments on any part are welcome.

The Evaluator is kind of "messy" since it duplicates the source in stylus for `importFile` and `visitImport` except for where it lookups files where it first checks the cached paths. Without a change upstream in stylus I don't see this improving much. I'm going to propose such a change upstream but it may take a bit to figure it out more generally. I'm hoping improving this implementation will help but also be useful to show the need.

- [ ] Improve `lib/` filenames. Specifically `importsresolve` and `importsscrap`.
- [ ] Possibly rename `CachedPathEvaluator` and `PathCache`.
- [ ] Definitely rename `resolvers()`, `resolveImports()`, `resolveAllImports()`, and `collectImportSymbols()`.
- [ ] Replace `when` on request.
I'm a big fan of promises and generally default to using [when](github.com/cujojs/when) but have used other libs and I'm open to replacing it with another. Currently this PR uses when's `reduce` and `node/function.call` as far as functions past the Promise spec.
- [ ] More comments!
- [ ] Restructure `importsresolve` possibly moving functions to be static members of PathCache.
- [ ] Review tests.
I think the current tests this adds improves areas previously missing and speaks well to the new feature. But if I missed any let me know, I'm going to think on this a little to make sure as well.