Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added support for Renderer#define(name, node) to define a global

  • Loading branch information...
commit 05ef2d5b7e436877cb9b6b9ff5281eaeb066c246 1 parent a4dd849
TJ Holowaychuk tj authored
Showing with 22 additions and 2 deletions.
  1. +6 −0 docs/js.md
  2. +9 −2 lib/renderer.js
  3. +7 −0 lib/visitor/evaluator.js
6 docs/js.md
View
@@ -50,6 +50,12 @@ Defer importing of the given `path` until evaluation is performed. The example b
console.log(css);
});
+### .define(name, node)
+
+ By passing a `Node`, we may define a global variable. This is useful when exposing conditional features within your library depending on the availability of another. For example the "Nib" extensions library conditionally supports node-canvas, providing image generation, however this is not always available, so Nib may define:
+
+ .define('has-canvas', stylus.nodes.false);
+
### .define(name, fn)
This method allows you to provide a JavaScript-defined function to Stylus, think of these as you would JavaScript to C++ bindings. When you have something you cannot do within Stylus, you define it in JavaScript.
11 lib/renderer.js
View
@@ -25,6 +25,7 @@ var Parser = require('./parser')
var Renderer = module.exports = function Renderer(str, options) {
options = options || {};
+ options.globals = {};
options.functions = {};
options.imports = [__dirname + '/functions'];
options.filename = options.filename || 'stylus';
@@ -101,17 +102,23 @@ Renderer.prototype.use = function(fn){
};
/**
- * Define function with the given `name`. Optionally
+ * Define function or global var with the given `name`. Optionally
* the function may accept full expressions, by setting `raw`
* to `true`.
*
* @param {String} name
- * @param {Function} fn
+ * @param {Function|Node} fn
* @return {Renderer} for chaining
* @api public
*/
Renderer.prototype.define = function(name, fn, raw){
+ if (fn.nodeName) {
+ this.options.globals[name] = fn;
+ return this;
+ }
+
+ // function
this.options.functions[name] = fn;
if (undefined != raw) fn.raw = raw;
return this;
7 lib/visitor/evaluator.js
View
@@ -39,6 +39,7 @@ var Evaluator = module.exports = function Evaluator(root, options) {
this.stack = new Stack;
this.imports = options.imports || [];
this.functions = options.functions || {};
+ this.globals = options.globals || {};
this.paths = options.paths || [];
this.filename = options.filename;
this.paths.push(dirname(options.filename || '.'));
@@ -98,6 +99,7 @@ Evaluator.prototype.setup = function(){
* Populate the global scope with:
*
* - css colors
+ * - user-defined globals
*
* @api private
*/
@@ -110,6 +112,11 @@ Evaluator.prototype.populateGlobalScope = function(){
, node = new nodes.Ident(name, rgba);
scope.add(node);
});
+
+ var globals = this.globals;
+ Object.keys(globals).forEach(function(name){
+ scope.add(new nodes.Ident(name, globals[name]));
+ });
};
/**
Please sign in to comment.
Something went wrong with that request. Please try again.