Stylus Import

Disclaimer: In all places the @import is used with Stylus sheets, the @require could be used

When using @import without the .css extension, it’s assumed to be a Stylus sheet (e.g., @import "mixins/border-radius").

@import works by iterating an array of directories, and checking 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. This means when you @import blueprint, it will resolve either blueprint.styl or blueprint/index.styl. This is really useful for libraries that want to expose all their features, while still allowing feature subsets 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).

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

Require

Along with @import, Stylus also has @require. It works almost in the same way, with the exception of importing any given file only once.

Block-level import

Stylus supports block-level import. It means that you can use @import not only at root level, but also nested inside other selectors or at-rules.

If you have a bar.styl with this code:

.bar
width: 10px;

Then you can import it inside a foo.styl like this:

.foo
@import 'bar.styl' @media screen and (min-width: 640px)
@import 'bar.styl'

And you’ll get this compiled CSS as a result:

.foo .bar {
width: 10px;
}
@media screen and (min-width: 640px) {
.bar {
width: 10px;
}
}

File globbing

Stylus supports globbing. With it you could import many files using a file mask:

@import 'product/*'

This would import all the stylus sheets from the product directory in such structure:

./product
|-- body.styl
|-- foot.styl
|-- head.styl

Note that this works with @require too, so if you would have also a ./product/index.styl with this content:

@require 'head'
@require 'body'
@require 'foot'

then @require 'product/*' would include each individual sheet only once.

Resolving relative urls inside imports

By default Stylus doesn’t resolve the urls in imported .styl files, so if you’d happen to have a foo.styl with @import "bar/bar.styl" which would have url("baz.png"), it would be url("baz.png") too in a resulting CSS.

But you can alter this behavior by using --resolve-url (or just -r) CLI option to get url("bar/baz.png") in your resulting CSS.

JavaScript Import API

When using the .import(path) method, these imports are deferred until evaluation:

   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);
});

The following statement…

 @import 'mixins/vendor'

…is equivalent to…

 .import('mixins/vendor')

最新文章

  1. 【Kubernetes】K8S网络方案--最近在看的
  2. spring3.0+mybatis+spring快速入门
  3. 从一个简单例子来理解js引用类型指针的工作方式
  4. discuz门户首页-header文件模板语法详解和注释
  5. windows常用运行命令收集(持续更新)
  6. 区域医疗移动医疗影像解决方案1-基于HTML5的PACS
  7. PHP中CURL技术模拟登陆抓取网站信息,用与微信公众平台成绩查询
  8. Python之路第十一天,高级(3)-线程池
  9. 怎么查看chrome网络日志
  10. javascript算法(一)
  11. MYSQL GROUP BY Optimization
  12. 阿里云Ubuntu安装图形界面与中文语言包
  13. 关于交叉熵(cross entropy),你了解哪些
  14. BZOJ1800:fly 飞行棋 (双指针 组合数)
  15. 基于Docker+Jenkins+Gitlab搭建持续集成环境
  16. 用OpenSCAD設計特製的遊戲骰子
  17. MySQL之更新型触发器
  18. 电脑出现 flash update failed 解决方法
  19. Linux巩固记录(8) Hbase shell 基本使用
  20. Mac idea 快捷键

热门文章

  1. Python三次握手和四次挥手
  2. Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/objectweb/asm/Type
  3. Python标准库Random
  4. Token:服务端身份验证的流行方案
  5. 记录 shell学习过程(7) case 以及 shell 的特殊变量
  6. 【Python】程序计时
  7. C++-对象指针的滥用
  8. Qt- 图形界面应用程序的运行模式
  9. python3练习100题——010
  10. C# 元组和值元组