Permalink
Browse files

use() built-in function

simple pass-through to style.use(), allowing the use of Stylus plugins from within Stylus code.
  • Loading branch information...
1 parent a1694db commit 9debaffe8e97105bde069f3e30f4cb92f023b7ce @geddski committed Aug 13, 2012
Showing with 43 additions and 0 deletions.
  1. +28 −0 lib/functions/index.js
  2. +3 −0 test/cases/bifs.use.css
  3. +4 −0 test/cases/bifs.use.styl
  4. +8 −0 test/cases/plugins/add.js
View
@@ -435,6 +435,34 @@ exports.json = function(path){
};
/**
+* Use the given `plugin`
+*
+* Examples:
+*
+* use("plugins/add.js")
+*
+* width add(10, 100)
+* // => width: 110
+*/
+
+exports.use = function(plugin){
+ utils.assertString(plugin, 'path');
+
+ // lookup
+ plugin = plugin.string;
+ var found = utils.lookup(plugin, this.options.paths, this.options.filename);
+ if (!found) throw new Error('failed to locate plugin file ' + plugin);
+
+ // use
+ var fn = require(path.resolve(found));
+ if ('function' != typeof fn) {
+ throw new Error('plugin ' + path + ' does not export a function');
+ }
+ this.renderer.use(fn(this.options));
+ return;
+}
+
+/**
* Unquote the given `str`.
*
* Examples:
View
@@ -0,0 +1,3 @@
+body {
+ width: 7;
+}
View
@@ -0,0 +1,4 @@
+use('plugins/add.js')
+
+body
+ width add(4, 3)
@@ -0,0 +1,8 @@
+var plugin = function(){
+ return function(style){
+ style.define('add', function(a, b) {
+ return a.operate('+', b);
+ });
+ };
+};
+module.exports = plugin;

0 comments on commit 9debaff

Please sign in to comment.