1. 程式人生 > >展望未來:使用 PostCSS 和 cssnext 書寫 CSS

展望未來:使用 PostCSS 和 cssnext 書寫 CSS

htm title rop img blank 利用 未來 包含 轉換成

原文鏈接:A look into writing future CSS with PostCSS and cssnext

譯者:nzbin

像 Twitter,Google,BBC 使用的一樣,我打算對 CSS 後處理器 PostCSS 的世界一探究竟。

PostCSS是什麽?

PostCSS 是使用 javascript 插件轉換 CSS 的後處理器。PostCSS 本身不會對你的 CSS 做任何事情,你需要安裝一些 plugins 才能開始工作。這不僅使其模塊化,同時功能也會更強。

它的工作原理就是解析 CSS 並將其轉換成一個 CSS 的節點樹,這可以通過 javascript 來控制(也就是插件發揮作用)。然後返回修改後的樹並保存。它與 Sass(一種預處理器)的工作原理不同,你基本上是用一種不同的語言來編譯 CSS 。

預處理和後處理到底是什麽?

為了用簡單的方式解釋預處理和後處理的不同,我將以單位轉換為例。當書寫 Sass 時,我們可以用函數將 px 轉換成 rem

/* input */
.selector { margin-bottom: rem(20px); }

/* output, assuming base font size is 1rem */
.selector { margin-bottom: 1.25rem; }

這種方式節省了我們手工計算的的時間。不過通過 PostCSS,我們能夠做的更好。因為是後處理的緣故,我們不需要任何函數來編譯CSS。我們可以直接書寫 px ,它可以自動地轉換成 rem

/* input */
.selector { margin-bottom: 20px; }

/* output, assuming base font size is 1rem */
.selector { margin-bottom: 1.25rem; }

PostCSS 會在每一個 px 值出現並運行計算之前處理聲明,將其轉換成 rem 的值。註意:因為 PostCSS 模塊化的本質,你需要使用 postcss-pxtorem 才能完成轉換。

使用 cssnext 書寫未來的 CSS

我們可以在樣式表中利用 cssnext 額外增加的一些 css 規範。cssnext 是一個 PostCSS 的包,其中包含了大量的特性組件,比如 custom properties 和 custom selectors:

技術分享
/* custom properties */
:root {
  --heading-color: #ff0000;
}

/* custom selectors */
@custom-selector :--headings h1, h2, h3, h4, h5, h6;

/* usage */
:--headings { 
  color: var(--heading-color);
}
技術分享

通過 cssnext,上述代碼會被處理成以下內容

技術分享
h1,
h2,
h3,
h4,
h5,
h6 { 
  color: #ff0000;
}
技術分享

這真的很簡潔,其中還有很多令人興奮的特性。因為我們書寫的是未來規範的 css,所以 PostCSS 的生成步驟不需要瀏覽器去執行。你可以 點擊這裏 查看所有的特性。

接下來,我將用 PostCSS 的自定義函數擴展 CSS 的功能。

更上一層樓:用自定義函數擴展 CSS 的功能

使用 cssnext,我們可以通過 javascript 創建自定義函數來操作被解析的 CSS。在 Sass 中,我們經常使用生成行距的函數(根據基本的 line-height 計算),它有助於創建簡單且可維護的垂直韻律。

技術分享
$line-height: 32px;

/* vertical rhythm function */
@function vr($amount) {
  @return $line-height * $amount;
}

/* input */
.selector { margin-bottom: vr(2) }

/* output */
.selector { margin-bottom: 64px; }
技術分享

如果用PostCSS做的話,我們可以自定義CSS組件而不是函數。

/* input */
.selector { margin-bottom: 2vr }

/* output */
.selector { margin-bottom: 64px; }

我更喜歡這種方法,因為他看起來更自然,如同寫原生 CSS 一樣。JavaScript 的代碼如下:

技術分享
var vr = function (css) {
  var lineHeight = 32;

  css.eachDecl(function (decl) {
    if (decl.value.match(/vr/)) {
      decl.value = lineHeight * parseFloat(decl.value) + ‘px‘;
    }
  });
};
技術分享

看起來工作量好像增加了,但是如果你對 JavaScript 不熟悉的話,事實上也沒有多少東西。以下是帶註釋的 JavaScript 代碼:

技術分享
// Define the function to include in the PosCSS transforms list, and pass in the CSS node tree.
var vr = function (css) {

  // Set the line-height.
  var lineHeight = 32;

  // Loop through each CSS declaration using PostCSS‘s `eachDecl()` and pass in the declaration (`decl`) string.
  css.eachDecl(function (decl) {

    // Match if the declaration value has the ‘vr‘ unit.
    if (decl.value.match(/vr/)) {

      // Replace the declaration value with the calculated value.
      // Use parseFloat() to remove the `vr` unit from the string and return a number.
      decl.value = lineHeight * parseFloat(decl.value) + ‘px‘;
    }
  });
};
技術分享

感興趣的朋友可以看看我編寫的這個高級插件postcss-vertical-rhythm,它無需引入額外的插件只需要加入到 PostCSS transforms 中。

這只是你可以用 PostCSS 實現的一個小例子。希望你能夠看到它的強大之處以及和預處理器的不同。我建議你閱讀文檔以及插件列表,了解哪些東西可以在你的項目中使用。

速度

PostCSS 聲稱比預處理器快 3-30 倍。出於好奇,我在 10000 個選擇器的 5 個屬性上使用上述 Sass 函數和 PostCSS 函數,也就是處理 50000 次,以下是對比結果。

Libsass 3.2

技術分享

PostCSS

技術分享

令我吃驚的是,PostCSS 在處理這個函數時幾乎快了 13 秒。難以置信的快了 34 倍。很顯然真實生活中不會出現這樣的腳本,但是這個有趣的測試能夠看出它的執行效率,我們希望有一個能降低構建速度的新工具。值得一提的是,libsass 在3.3 版本中提升了運行速度,所以我很期待新版本的運行速度到底提升了多少。

思考

PostCSS 看起來很有前途。我喜歡它的模塊化,我可以編寫我需要的插件,並且根據需要自定義功能。嘗試未來的 CSS 語法是一件令人興奮的事。

當然,我並不是說我們應該放棄 Sass。Sass 是一個很好的工具,我建議任何人應該先從預處理器開始學習。如果你喜歡冒險的話,我肯定推薦你試一試 PostCSS。

我很樂意聽到任何人在實際項目中使用 PostCSS 的想法和經驗。請與我保持聯系!

展望未來:使用 PostCSS 和 cssnext 書寫 CSS