Stylus-import
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')
Stylus-import的更多相关文章
- 【前端开发】优化代码之减少引入,css预编译语言的优点,stylus的使用
前言:我必须得承认在最最最开始的时候,我对于css的预编译是非常不以为然的,这是错误的.一般在页面编写过程中,我会将需要reset的css放在reset.css中,讲会需要重复用到的放置到public ...
- 【stylus】stylus在webstrom中的识别
<style lang="stylus" rel="stylesheet/stylus"> @import './common/stylus/mix ...
- stylus
stylus介绍 是个什么鬼?对于开发来说,CSS的弱点在于静态化.我们需要一个真正能提高开发效率的工具, LESS , SASS 都在这方面做了一些贡献. Stylus 是一个CSS的预处理框架,2 ...
- VUE -- stylus入门使用方法
sizes() 15px 10px sizes()[0] // => 15px stylus介绍 是个什么鬼?对于开发来说,CSS的弱点在于静态化.我们需要一个真正能提高开发效率的工具,LESS ...
- 在vue-cli 3中, 给stylus、sass样式传入共享的全局变量
在开发中有时,我们定义了大量的基础样式变量,例如: 大量的vue单文件组件会用到这些变量,每个组件都引人一次又太麻烦.全局引入是个不错的方法,于是,在main.js 中引入variable.styl文 ...
- 移动端1px 边框完整方案(四个方向)
使用stylus(预处理) 需要一个函数接收两个参数 第一个需要在哪个方向出现边框 第二个边框颜色 $border1px(face,$color) 根据传入的方向属性,调整其他参数 l 左右方向 t ...
- vue翻页器,包括上一页,下一页,跳转
翻页组件 -- 子组件<template> <div class="pager-wrapper" ref="pager"> <di ...
- 关于Vue-cli 组件引入CSS样式文件
在 Vue-cli 组件 .vue 文件中引入 css 样式表出错 由于使用Vue-cli后, 引入css 样式表 不需要 多余../../ 之类路径 现在写法也发生了改变 <style ...
- Vue(小案例)底部tab栏和顶部title栏的实现
---恢复内容开始--- 一.前言 1.底部tab栏实现 2.顶部title栏实现 二.主要内容 1.底部tab栏实现(将底部导航提取到公共的组件中) 具体效果:当点击切换不同的tab的时候,对应 ...
- 移动端根据不同DPR加载大小不同的图片
1.首先创建mixin.styl文件代码如下: bg-image($url) // 创建bg-image($url)函数 background-image: url($url + "@2x. ...
随机推荐
- phpstorm同步服务器文件
配置服务器 1.连接配置 打开菜单栏 Tools -> Deployment -> Configuration 点击 + 选择 SFTP,并填写相关服务器信息: Type:连接类型,这里选 ...
- Redis到底是多线程还是单线程?线程安全吗,还需要加锁吗?
0. redis单线程问题 单线程指的是网络请求模块使用了一个线程(所以不需考虑并发安全性),即一个线程处理所有网络请求,其他模块仍用了多个线程. 1. 为什么说redis能够快速执行 (1) 绝大部 ...
- c语言该怎么入门?C语言入门教程(非常详细)
C语言是一门面向过程的编译型语言,它的运行速度极快,仅次于汇编语言.C语言是计算机产业的核心语言,操作系统.硬件驱动.关键组件.数据库等都离不开C语言:不学习C语言,就不能了解计算机底层. 这套「C语 ...
- 论如何用python发qq消息轰炸虐狗好友
因为我的某个好友在情人节的时候秀恩爱,所以我灵光一闪制作了qq消息轰炸并记录了下来. PS:另外很多人在学习Python的过程中,往往因为遇问题解决不了或者没好的教程从而导致自己放弃,为此我整理啦从基 ...
- jQuery---jQuery初体验
jQuery初体验 1. 引入jquery文件 2. 入口函数标准 jQuery优点总结 (对应的就是js的缺点): 查找元素的方法多种多样,非常灵活 拥有隐式迭代特性,因此不需要手写for循环 完全 ...
- Cleaning Data in R
目录 R 中清洗数据 常见三种查看数据的函数 Exploring raw data 使用dplyr包里面的glimpse函数查看数据结构 \(提取指定元素 ```{r} # Histogram of ...
- asp.net mvc获取http body中的json
今天遇到一个对方直接post 一共json对象字符串过来的场景.按照以前的旧方案方法可以直接在request.inputstream里面读取,然后反序列化即可.(当然估计ASP.NET MVC应该有更 ...
- SPDK-nvmf与不同传输类型的公共接口
SPDK-nvmf与不同传输类型的公共接口 不同类型的传输层到nvmf的公共命令请求接口 nvmf_fc_hwqp_handle_request() -->cmd_iu = buffer-> ...
- stopPropagation() 方法
定义和用法 不再派发事件. 终止事件在传播过程的捕获.目标处理或起泡阶段进一步传播.调用该方法后,该节点上处理该事件的处理程序将被调用,事件不再被分派到其他节点. 语法 event.stopPropa ...
- 基于PO和单例设计模式用python+selenium进行ui自动化框架设计
一)框架目录的结构 二)config包当中的config.ini文件主要是用来存项目的绝对路径,是为了后续跑用例和生成测试报告做准备然后目前的配置文件大都会用yaml,ini,excel,还有.py也 ...