L3F.WIN

Github及Hexo的使用

0%

学习stylus【22】可执行性(EXECUTABLE)

可执行性(EXECUTABLE)

正因有stylus可执行性,Stylus才能将自身转换成CSS.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
Usage: stylus [options] [command] [< in [> out]]
[file|dir ...]

Commands:

help [<type>:]<prop> Opens help info at MDC for <prop> in
your default browser. Optionally
searches other resources of <type>:
safari opera w3c ms caniuse quirksmode

Options:

-i, --interactive Start interactive REPL
-u, --use <path> Utilize the Stylus plugin at <path>
-U, --inline Utilize image inlining via data URI support
-w, --watch Watch file(s) for changes and re-compile
-o, --out <dir> Output to <dir> when passing files
-C, --css <src> [dest] Convert CSS input to Stylus
-I, --include <path> Add <path> to lookup paths
-c, --compress Compress CSS output
-d, --compare Display input along with output
-f, --firebug Emits debug infos in the generated CSS that
can be used by the FireStylus Firebug plugin
-l, --line-numbers Emits comments in the generated CSS
indicating the corresponding Stylus line
-m, --sourcemap Generates a sourcemap in sourcemaps v3 format
--sourcemap-inline Inlines sourcemap with full source text in base64 format
--sourcemap-root <url> "sourceRoot" property of the generated sourcemap
--sourcemap-base <path> Base <path> from which sourcemap and all sources are relative
-P, --prefix [prefix] Prefix all css classes
-p, --print Print out the compiled CSS
--import <file> Import stylus <file>
--include-css Include regular CSS on @import
-D, --deps Display dependencies of the compiled file
--disable-cache Disable caching
--hoist-atrules Move @import and @charset to the top
-r, --resolve-url Resolve relative urls inside imports
--resolve-url-nocheck Like --resolve-url but without file existence check
-V, --version Display the version of Stylus
-h, --help Display help information

STDIO编译范例

stylus读取自stdin输出到stdout, 因此,如下例:

1
$ stylus --compress < some.styl > some.css

在终端机上尝试Stylus,书写下面的内容,然后为EOF按下CTRL-D:

1
2
3
4
$ stylus
body
color red
font 14px Arial, sans-serif

编译文件范例

stylus亦接受文件和目录。例如,一个目录名为css将在同一目录编译并输出.css文件。

1
$ stylus css

下面的将会输出到./public/stylesheets:

1
$ stylus css --out public/stylesheets

或一些文件:

1
$ stylus one.styl two.styl

为了开发的目的,你可以使用linenos选项发出指令在生成的CSS中显示Stylus文件名以及行数。

1
$ stylus --line-numbers <path>

或是firebug选项,如果你想使用firebug的FireStylus扩展

1
$ stylus --firebug <path>

类前缀

stylus可执行文件为您提供了一种方法,使用给定[prefix]的–prefix选项为所有生成的样式添加前缀,

1
$ stylus --prefix foo-

这个代码使用后

1
2
3
4
5
6
.bar
width: 10px
=>
.foo-bar {
width: 10px;
}

所有类都会被添加前缀:插值,扩展等。

转换CSS

如果你想把CSS转换成简洁的Stylus语法,可以使用–css标志。
通过标准输入输出:

1
$ stylus --css < test.css > test.styl

输出基本名一致的.styl文件。

1
$ stylus --css test.css

输出特定的目标:

1
$ stylus --css test.css /tmp/out.styl

CSS属性的帮助

在OS X上,stylus help 会打开你默认浏览器并显示给定的属性的帮助文档。

1
$ stylus help box-shadow

壳层交互

Stylus REPL (Read-Eval-Print-Loop)或“壳层交互(Interactive Shell)”允许你直接在终端机上把玩Stylus的表达式。
注意只有表达式可以生效,而不是选择器之类。为了简单,我们添加-i或–interactive标志:

1
2
3
4
5
6
7
8
9
10
11
12
13
$ stylus -i
> color = white
=> #fff
> color - rgb(200,50,0)
=> #37cdff
> color
=> #fff
> color -= rgb(200,50,0)
=> #37cdff
> color
=> #37cdff
> rgba(color, 0.5)
=> rgba(55,205,255,0.5)

解析导入中的相关URL

默认情况下,Stylus不会解析导入的.styl文件中的url,所以如果你碰巧有一个带有@import”bar/bar.styl”的foo.styl,它有一个url(“baz.png”),它在得到的CSS中也会是url(“baz.png”)。
但是你可以通过使用–resolve-url(或者只是-r)选项来获得你的结果CSS中的url(“bar/baz.png”)来改变这种行为。列出依赖关系
您可以使用–deps(或者只是-D)标志来获取编译文件的依赖关系列表。
例如,假设我们有test.styl:

1
2
3
4
5
6
7
8
9
10
11
12
13
@import 'foo'
@import 'bar'
And inside foo.styl:

@import 'baz'
Running:

$ stylus --deps test.styl
Will give us list of the imports paths:

foo.styl
baz.styl
bar.styl

请注意,目前这不适用于动态生成的路径。

利用插件
本例我们将使用nibStylus插件来说明它的CLI使用。
假设我们有如下的Stylus, 其导入nib并使用nib的linear-gradient()方法:

1
2
3
4
@import 'nib'

body
background: linear-gradient(20px top, white, black)

我们是使用stylus(1)通过标准输入输出试图渲染的第一个东西可能就像下面这样:

1
$ stylus < test.styl

这可能会生成如下的错误,因为Stylus不知道去哪里找到nib.

1
2
3
4
5
6
7
Error: stdin:3
1|
2|
> 3| @import 'nib'
4|
5| body
6| background: linear-gradient(20px top, white, black)

对于简单应用Stylus API们的插件,我们可以添加查找路径。通过使用–include或-I标志:

1
$ stylus < test.styl --include ../nib/lib

现在生成内容如下。您可能注意到了,gradient-data-uri()以及create-gradient-image()以字面量形式输出了。这是因为,当插件提供JavaScript API的时候,光暴露插件的路径是不够的。但是,如果我们仅仅想要的是纯粹Stylus nib函数,则足够了。

1
2
3
4
5
6
7
body {
background: url(gradient-data-uri(create-gradient-image(20px, top)));
background: -webkit-gradient(linear, left top, left bottom, color-stop(0, #fff), color-stop(1, #000));
background: -webkit-linear-gradient(top, #fff 0%, #000 100%);
background: -moz-linear-gradient(top, #fff 0%, #000 100%);
background: linear-gradient(top, #fff 0%, #000 100%);
}

因此,我们需要做的是使用–use或-u标志。其会找寻node模块(有或者没有.js扩展名)路径,这里的require()模块或调用style.use(fn())来暴露该插件(定义js函数等)。

1
2
3
4
5
6
7
8
9
$ stylus < test.styl --use ../nib/lib/nib
=>
body {
background: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAAUCAYAAABMDlehAAAABmJLR0QA/wD/AP+gvaeTAAAAI0lEQVQImWP4+fPnf6bPnz8zMH358oUBwkIjKJBgYGNj+w8Aphk4blt0EcMAAAAASUVORK5CYII=");
background: -webkit-gradient(linear, left top, left bottom, color-stop(0, #fff), color-stop(1, #000));
background: -webkit-linear-gradient(top, #fff 0%, #000 100%);
background: -moz-linear-gradient(top, #fff 0%, #000 100%);
background: linear-gradient(top, #fff 0%, #000 100%);
}

如果您需要将参数传递给插件,请使用–with选项。 - 评估任何有效的javascript表达式并将其值传递给插件。 例如:

1
$ stylus < test.styl --use ../node_modules/autoprefixer-stylus --with "{ browsers: ['ie 7', 'ie 8'] }"