Stylus 可執(zhí)行性

2023-09-11 15:59 更新

可執(zhí)行性(Executable)

Stylus可執(zhí)行代碼

正因有stylus可執(zhí)行性,Stylus才能將自身轉(zhuǎn)換成CSS.

Usage: stylus [options] [command] [< in [> out]]
              [file|dir ...]

Commands:

  help <prop>     Opens help info for <prop> in
                  your default browser. (OS X only)

Options:

  -u, --use <path>        Utilize the stylus plugin at <path>
  -i, --interactive       Start interactive REPL
  -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
  -V, --version           Display the version of Stylus
  -h, --help              Display help information
STDIO編譯范例

stylus讀取自stdin輸出到stdout, 因此,如下例:

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

在終端機上嘗試Stylus,書寫下面的內(nèi)容,然后為__EOF__按下CTRL-D:

$ stylus
 body
   color red
   font 14px Arial, sans-serif
編譯文件范例

stylus亦接受文件和目錄。例如,一個目錄名為css將在同一目錄編譯并輸出.css文件。

$ stylus css

下面的將會輸出到./public/stylesheets:

$ stylus css --out public/stylesheets

或一些文件:

$ stylus one.styl two.styl

為了開發(fā)的目的,你可以使用linenos選項發(fā)出指令在生成的CSS中顯示Stylus文件名以及行數(shù)。

$ stylus --line-numbers <path>

或是firebug選項,如果你想使用firebug的FireStylus擴展。

$ stylus --firebug <path>
轉(zhuǎn)換CSS

如果你想把CSS轉(zhuǎn)換成簡潔的Stylus語法,可以使用--css標志。

通過標準輸入輸出:

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

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

$ stylus --css test.css

輸出特定的目標:

$ stylus --css test.css /tmp/out.styl
CSS屬性的幫助

在OS X上,stylus help <prop>會打開你默認瀏覽器并顯示給定的<prop>屬性的幫助文檔。

$ stylus help box-shadow
殼層交互(Interactive Shell)

Stylus REPL (Read-Eval-Print-Loop)或“殼層交互(Interactive 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)
利用插件

本例我們將使用nibStylus插件來說明它的CLI使用。

假設(shè)我們有如下的Stylus, 其導入nib并使用nib的linear-gradient()方法:

@import 'nib'

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

我們是使用stylus(1)通過標準輸入輸出試圖渲染的第一個東西可能就像下面這樣:

$ stylus < test.styl

這可能會生成如下的錯誤,因為Stylus不知道去哪里找到nib.

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

對于簡單應(yīng)用Stylus API們的插件,我們可以添加查找路徑。通過使用--include或-I標志:

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

現(xiàn)在生成內(nèi)容如下。您可能注意到了,gradient-data-uri()以及create-gradient-image()以字面量形式輸出了。這是因為,當插件提供JavaScript API的時候,光暴露插件的路徑是不夠的。但是,如果我們僅僅想要的是純粹Stylus nib函數(shù),則足夠了。

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()模塊或調(diào)用style.use(fn())來暴露該插件(定義js函數(shù)等)。

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


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號