跳至内容

使用 Stylus 进行样式设计

一旦你拥有 Node.js,安装 Stylus 非常容易。因此,获取适用于你的平台的二进制文件,并确保其中还包括 npm(Node 的包管理器)。

现在,在你的终端中输入

# npm
$ npm install stylus -g

# pnpm
$ pnpm add -g stylus
# npm
$ npm install stylus -g

# pnpm
$ pnpm add -g stylus

如果你想要一种具有以下特性或下面列出的特性的 Node.js 表达式 CSS 语言,请访问 GitHub 获取更多信息。

Stylus cli

Stylus 随附 stylus 可执行文件,用于将 Stylus 转换为 CSS。

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
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*,例如

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

在终端中尝试 Stylus!在下面输入内容,然后按 CTRL-D 表示 __EOF__

$ stylus
body
  color red
  font 14px Arial, sans-serif
$ stylus
body
  color red
  font 14px Arial, sans-serif

编译文件示例

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

$ stylus css
$ stylus css

以下内容将输出到 ./public/stylesheets

$ stylus css --out public/stylesheets
$ stylus css --out public/stylesheets

或几个文件

$ stylus one.styl two.styl
$ stylus one.styl two.styl

出于开发目的,你可以使用 linenos 选项在生成的 CSS 中发出注释,指示 Stylus 文件名和行号

$ stylus --line-numbers <path>
$ stylus --line-numbers <path>

或者,如果你想使用 Firebug 的 FireStylus 扩展,可以使用 firebug 选项

$ stylus --firebug <path>
$ stylus --firebug <path>

类名前缀

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

$ stylus --prefix foo-
$ stylus --prefix foo-

与以下代码一起使用

.bar
  width: 10px
.bar
  width: 10px

将产生

.foo-bar {
  width: 10px;
}
.foo-bar {
  width: 10px;
}

所有类都将添加前缀:插值、扩展等。

转换 CSS

如果你希望将 CSS 转换为简洁的 Stylus 语法,请使用 --css 标志。

通过 stdio

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

输出具有相同基本名称的 .styl 文件

$ stylus --css test.css
$ stylus --css test.css

输出到特定目标

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

CSS 属性帮助

在 OS X 上,stylus help <prop> 将打开您的默认浏览器并显示给定 <prop> 的帮助文档。

$ stylus help box-shadow
$ stylus help box-shadow

交互式 Shell

Stylus REPL(读-求值-打印-循环)或“交互式 Shell”允许您直接从终端中使用 Stylus 表达式。

请注意,这仅适用于表达式——不适用于选择器等。要使用,只需添加 -i--interactive 标志

$ 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)
$ 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

@import 'foo'
@import 'bar'
@import 'foo'
@import 'bar'

而在 foo.styl

@import 'baz'
@import 'baz'

运行

$ stylus --deps test.styl
$ stylus --deps test.styl

将为我们提供导入路径列表

foo.styl
baz.styl
bar.styl
foo.styl
baz.styl
bar.styl

请注意,目前这对于动态生成的路径不起作用.

使用插件

对于此示例,我们将使用 nib Stylus 插件来说明其 CLI 用法。

假设我们有以下 Stylus,它导入 nib 以使用其 linear-gradient() 函数。

@import 'nib'

body
  background: linear-gradient(20px top, white, black)
@import 'nib'

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

我们首次尝试通过 stdio 使用 stylus(1) 进行渲染可能如下所示

$ stylus < test.styl
$ stylus < test.styl

这将产生以下错误(因为 Stylus 不知道在哪里找到 nib)。

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

对于仅提供 Stylus API 的插件,我们可以将路径添加到 Stylus 查找路径。我们通过使用 --include-I 标志来实现。

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

现在产生以下输出。(您可能注意到,对 gradient-data-uri()create-gradient-image() 的调用输出为文字。这是因为当插件提供 JavaScript API 时,仅公开库路径是不够的。但是,如果我们只想使用纯 Stylus nib 函数,那就没问题了。)

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%);
}
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 标志。它需要一个指向节点模块的路径(带有或不带有 .js 扩展名)。此 require() 需要模块,期望一个函数作为 module.exports 导出,然后调用 style.use(fn()) 来公开插件(定义其 js 函数等)。

$ stylus < test.styl --use ../nib/lib/nib
$ 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%);
}
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 选项。--with 会计算任何有效的 JavaScript 表达式,并将它的值传递给插件。例如

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