跳至内容

JavaScript API

只需require模块,并使用给定的 Stylus 代码字符串和(可选)options对象调用 render()

利用 Stylus 的框架应传递 filename 选项以提供更好的错误报告。

var stylus = require('stylus');

stylus.render(str, { filename: 'nesting.css' }, function(err, css){
  if (err) throw err;
  console.log(css);
});
var stylus = require('stylus');

stylus.render(str, { filename: 'nesting.css' }, function(err, css){
  if (err) throw err;
  console.log(css);
});

我们也可以以更渐进的方式执行相同操作

var stylus = require('stylus');

stylus(str)
  .set('filename', 'nesting.css')
  .render(function(err, css){
    // logic
  });
var stylus = require('stylus');

stylus(str)
  .set('filename', 'nesting.css')
  .render(function(err, css){
    // logic
  });

.set(设置,值)

应用设置,例如 filename 或导入 paths

.set('filename', __dirname + '/test.styl')
.set('paths', [__dirname, __dirname + '/mixins'])
.set('filename', __dirname + '/test.styl')
.set('paths', [__dirname, __dirname + '/mixins'])

.include(路径)

.set('paths',...) 的渐进替代方法是 .include()。当公开路径的外部 Stylus 库时,这是理想的选择。

stylus(str)
  .include(require('nib').path)
  .include(process.env.HOME + '/mixins')
  .render(...)
stylus(str)
  .include(require('nib').path)
  .include(process.env.HOME + '/mixins')
  .render(...)

.import(路径)

推迟导入给定的 path,直到执行评估。下面的示例本质上与在 Stylus 表单中执行 @import 'mixins/vendor' 相同。

var stylus = require('../')
  , str = require('fs').readFileSync(__dirname + '/test.styl', 'utf8');

stylus(str)
  .set('filename', __dirname + '/test.styl')
  .import('mixins/vendor')
  .render(function(err, css){
  if (err) throw err;
  console.log(css);
});
var stylus = require('../')
  , str = require('fs').readFileSync(__dirname + '/test.styl', 'utf8');

stylus(str)
  .set('filename', __dirname + '/test.styl')
  .import('mixins/vendor')
  .render(function(err, css){
  if (err) throw err;
  console.log(css);
});

.define(名称,节点[, 原始])

通过传递 Node,我们可以定义一个全局变量。当根据另一个变量的可用性公开库中的条件特性时,这很有用。例如,Nib 扩展库有条件地支持 node-canvas,提供图像生成。

但是,这并不总是可用的,因此 Nib 可能定义

.define('has-canvas', stylus.nodes.false);
.define('some-setting', new stylus.nodes.String('some value'));
.define('has-canvas', stylus.nodes.false);
.define('some-setting', new stylus.nodes.String('some value'));

Stylus 也会在可能的情况下将 JavaScript 值转换为其 Stylus 等价值。以下是一些示例

.define('string', 'some string')
.define('number', 15.5)
.define('some-bool', true)
.define('list', [1,2,3])
.define('list', [1,2,[3,4,[5,6]]])
.define('list', { foo: 'bar', bar: 'baz' })
.define('families', ['Helvetica Neue', 'Helvetica', 'sans-serif'])
.define('string', 'some string')
.define('number', 15.5)
.define('some-bool', true)
.define('list', [1,2,3])
.define('list', [1,2,[3,4,[5,6]]])
.define('list', { foo: 'bar', bar: 'baz' })
.define('families', ['Helvetica Neue', 'Helvetica', 'sans-serif'])

注意:在默认情况下,JavaScript 对象变量将强制转换为元组数组表达式。例如,{ foo: 'bar', bar: 'baz' } 将变为表达式 (foo 'bar') (bar 'baz')。如果你想获得 哈希对象 返回,请将 raw 设置为 true

这些相同的规则也适用于 js 函数中的返回值

.define('get-list', function(){
  return ['foo', 'bar', 'baz'];
})
.define('get-list', function(){
  return ['foo', 'bar', 'baz'];
})

.define(name, fn)

此方法允许你向 Stylus 提供一个 JavaScript 定义的函数。可以将它们想象成 JavaScript 到 C++ 的绑定。当你在 Stylus 中做不到某些事情时,就在 JavaScript 中定义它!

在此示例中,我们定义了四个函数:add()sub()image-width()image-height()。这些函数必须返回一个 Node,此构造函数和其他节点可通过 stylus.nodes 获得。

var stylus = require('../')
  , nodes = stylus.nodes
  , utils = stylus.utils
  , fs = require('fs');

function add(a, b) {
  return a.operate('+', b);
}

function sub(a, b) {
  return a.operate('-', b);
}

function imageDimensions(img) {
  // assert that the node (img) is a String node, passing
  // the param name for error reporting
  utils.assertType(img, 'string', 'img');
  var path = img.val;

  // Grab bytes necessary to retrieve dimensions.
  // if this was real you would do this per format,
  // instead of reading the entire image :)
  var data = fs.readFileSync(__dirname + '/' + path);

  // GIF
  // of course you would support.. more :)
  if ('GIF' == data.slice(0, 3).toString()) {
    var w = data.slice(6, 8)
      , h = data.slice(8, 10);
    w = w[1] << 8 | w[0];
    h = h[1] << 8 | h[0];
  }

  return [w, h];
}

function imageWidth(img) {
  return new nodes.Unit(imageDimensions(img)[0]);
}

function imageHeight(img) {
  return new nodes.Unit(imageDimensions(img)[1]);
}

stylus(str)
  .set('filename', 'js-functions.styl')
  .define('add', add)
  .define('sub', sub)
  .define('image-width', imageWidth)
  .define('image-height', imageHeight)
  .render(function(err, css){
    if (err) throw err;
    console.log(css);
  });
var stylus = require('../')
  , nodes = stylus.nodes
  , utils = stylus.utils
  , fs = require('fs');

function add(a, b) {
  return a.operate('+', b);
}

function sub(a, b) {
  return a.operate('-', b);
}

function imageDimensions(img) {
  // assert that the node (img) is a String node, passing
  // the param name for error reporting
  utils.assertType(img, 'string', 'img');
  var path = img.val;

  // Grab bytes necessary to retrieve dimensions.
  // if this was real you would do this per format,
  // instead of reading the entire image :)
  var data = fs.readFileSync(__dirname + '/' + path);

  // GIF
  // of course you would support.. more :)
  if ('GIF' == data.slice(0, 3).toString()) {
    var w = data.slice(6, 8)
      , h = data.slice(8, 10);
    w = w[1] << 8 | w[0];
    h = h[1] << 8 | h[0];
  }

  return [w, h];
}

function imageWidth(img) {
  return new nodes.Unit(imageDimensions(img)[0]);
}

function imageHeight(img) {
  return new nodes.Unit(imageDimensions(img)[1]);
}

stylus(str)
  .set('filename', 'js-functions.styl')
  .define('add', add)
  .define('sub', sub)
  .define('image-width', imageWidth)
  .define('image-height', imageHeight)
  .render(function(err, css){
    if (err) throw err;
    console.log(css);
  });

如需进一步参考(在文档完成之前),请参阅以下文件

- `lib/nodes/*`
- `lib/utils.js`
- `lib/nodes/*`
- `lib/utils.js`

.use(fn)

调用时,给定的 fn 会使用渲染器调用,允许使用以上所有方法。这允许插件轻松地公开自身,定义函数、路径等。

var mylib = function(style){
  style.define('add', add);
  style.define('sub', sub);
};

stylus(str)
  .use(mylib)
  .render(...)
var mylib = function(style){
  style.define('add', add);
  style.define('sub', sub);
};

stylus(str)
  .use(mylib)
  .render(...)

使用选项调用 render() 方法时,use 选项可以给定一个函数或一组函数,以便使用渲染器调用它们。

stylus.render(str, { use: mylib }, function(err, css){
  if (err) throw err;
  console.log(css);
});
stylus.render(str, { use: mylib }, function(err, css){
  if (err) throw err;
  console.log(css);
});

.deps()

返回依赖项(导入文件)数组

stylus('@import "a"; @import "b"')
  .deps();

// => ['a.styl', 'b.styl']
stylus('@import "a"; @import "b"')
  .deps();

// => ['a.styl', 'b.styl']

另请参阅 --deps CLI 标志

stylus.resolver([options])

可选内置函数,可用于解析导入文件中相对 URL

stylus(str)
  .define('url', stylus.resolver())
  .render(function(err, css) {

  });
stylus(str)
  .define('url', stylus.resolver())
  .render(function(err, css) {

  });

另请参阅 --resolve-url CLI 标志

选项

- `paths` additional resolution path(s)
- `nocheck` don't check file existence
- `paths` additional resolution path(s)
- `nocheck` don't check file existence