Permalink
Browse files

Added more lookup patterns for the @import. Support Node.js modules.

  • Loading branch information...
1 parent 9d873b5 commit 1800848f1105c65b2ae429f7153b40ca66744541 @Panya Panya committed Jan 3, 2014
View
@@ -2,6 +2,6 @@ css
lib-cov
testing
.DS_Store
-node_modules
+/node_modules
coverage.html
lib-cov
@@ -17,7 +17,9 @@ var Visitor = require('./')
, Scope = require('../stack/scope')
, utils = require('../utils')
, bifs = require('../functions')
+ , basename = require('path').basename
, dirname = require('path').dirname
+ , relative = require('path').relative
, join = require('path').join
, colors = require('../colors')
, debug = require('debug')('stylus:evaluator')
@@ -234,6 +236,44 @@ Evaluator.prototype.evaluate = function(){
};
/**
+ * Lookup index file for @import.
+ *
+ * @param {String} name
+ * @return {Array}
+ * @api private
+ */
+
+Evaluator.prototype.lookupIndex = function(name){
+ // foo/index.styl
+ var found = utils.find(join(name, 'index.styl'), this.paths, this.filename)
+ , self = this
+ , dir = dirname(this.filename);
+ if (!found) {
+ // foo/foo.styl
+ found = utils.find(join(name, basename(name).replace(/\.styl/i, '') + '.styl'), this.paths, this.filename);
+ }
+ if (!found && !~name.indexOf('node_modules')) {
+ // node_modules/foo/.. or node_modules/foo.styl/..
+ found = lookupPackage(join('node_modules', name));
+ }
+ return found;
+
+ function lookupPackage(dir) {
+ var package = utils.lookup(join(dir, 'package.json'), self.paths, self.filename);
+ if (!package) {
+ return /\.styl$/i.test(dir) ? self.lookupIndex(dir) : lookupPackage(dir + '.styl');
+ }
+ var main = require(relative(__dirname, package)).main;
+ if (main) {
+ found = utils.find(join(dir, main), self.paths, self.filename);
+ } else {
+ found = self.lookupIndex(dir);
+ }
+ return found;
+ }
+};
+
+/**
* Visit Group.
*/
@@ -836,7 +876,7 @@ Evaluator.prototype.visitImport = function(imported){
// Lookup
found = utils.find(path, this.paths, this.filename);
if (!found) {
- found = utils.find(join(name, 'index.styl'), this.paths, this.filename);
+ found = this.lookupIndex(name);
index = true;
}
@@ -0,0 +1,9 @@
+body {
+ content: 'a';
+}
+body {
+ content: 'b';
+}
+body {
+ content: 'c';
+}
@@ -0,0 +1 @@
+@import 'import.lookup'
@@ -0,0 +1,3 @@
+@import 'lookup-a'
+@import 'lookup-b'
+@import 'lookup-c'
@@ -0,0 +1,2 @@
+body
+ content 'a'
@@ -0,0 +1,3 @@
+{
+ "main": "test.styl"
+}
@@ -0,0 +1,2 @@
+body
+ content 'b'
@@ -0,0 +1,2 @@
+body
+ content 'c'

0 comments on commit 1800848

Please sign in to comment.