Skip to content

Commit

Permalink
Added @import index resolution support
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Mar 8, 2011
1 parent ab9935e commit a5c0099
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
8 changes: 8 additions & 0 deletions docs/import.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ will render to the literal css __@import__ shown below:
When using __@import__ without the `.css` extension, it is assumed to be a Stylus sheet, for example `@import "mixins/border-radius"`.

__@import__ works by iterating an array of directories, and seeing if this file lives in any of them, similar to node's `require.paths`. This array defaults to a single path which is derived from the `filename` option's dirname. So if your filename is `/tmp/testing/stylus/main.styl`, then import will look in `/tmp/testing/stylus/`.

__@import__ also supports index styles, meaning if you `@import blueprint`, it will resolve either `blueprint.styl` or `blueprint/index.styl`, useful for libraries to expose all of their features, but still allow a subset of the library to be imported. For example a common lib structure might be:

./tablet
|-- index.styl
|-- vendor.styl
|-- buttons.styl
|-- images.styl

In the example below we set the `paths` options to provide additional paths to Stylus. Within `./test.styl` we could then `@import "mixins/border-radius"` or `@import "border-radius"` since `./mixins` is exposed to Stylus.

Expand Down
3 changes: 2 additions & 1 deletion lib/visitor/evaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ Evaluator.prototype.visitImport = function(import){
, root = this.root
, i = this.rootIndex
, stylus = require('../stylus')
, path = import.path
, path = name = import.path
, relative = this.importPath;

// Literal
Expand All @@ -527,6 +527,7 @@ Evaluator.prototype.visitImport = function(import){
// Lookup
if (relative) this.paths.push(relative);
found = utils.lookup(path, this.paths, this.filename);
found = found || utils.lookup(name + '/index.styl', this.paths, this.filename);
if (relative) this.paths.pop();

// Throw if import failed
Expand Down

0 comments on commit a5c0099

Please sign in to comment.