1. 程式人生 > 其它 >為何不推薦使用 Sass 作為 css 前處理器

為何不推薦使用 Sass 作為 css 前處理器

Sass Vs Scss

Sass 有兩種語法。
第一個被稱為 SCSS(Sassy CSS),在本參考文獻中一直使用,它是 CSS語法的擴充套件。這意味著每個有效的 CSS 樣式表,都是具有相同含義的有效 SCSS 檔案,兩者完全相容。下文描述的 Sass 功能增強了此語法。使用此語法的副檔名為.scss
第二種或更舊的語法稱為縮排語法(有時也稱為“ Sass”),提供了一種更為簡潔的 CSS 編寫方式。它使用縮排而不是方括號來表示選擇器的巢狀,並使用換行符而不是分號來分隔屬性。使用此語法的檔案副檔名為.sass

以上出自Sass 官網對兩者的解釋。因為 scss 完全相容 css,目前一般常用的是 scss;但 scss 也是 Sass 語法一部分,因此就以此為標題;言下之意,無論是.scss還是.sass皆不推薦使用。

http://www.ssnd.com.cn 化妝品OEM代加工

不推薦使用的理由

Sass 是採用 Ruby 語言編寫的一款 CSS 預處理語言,如果安裝並單獨使用sass,這並無什麼問題;

# 安裝 sass
npm install -g sass

# 使用 sass
sass source/stylesheets/index.scss build/stylesheets/index.css

但在工程化專案中,就另當別論了;,需要藉助 node-sass。它雖然能夠以驚人的速度,通過連線中介軟體自動將.scss檔案本地編譯為css,但同時存在很多問題,導致在有的時候引發巨大痛點,這便是“不推薦使用 Sass 作為 css 前處理器”主要理由。

Node-sass is a library that provides binding for Node.jsto LibSass, the C version of the popular stylesheet preprocessor, Sass.

node-sass 存在的痛點

node 版本與 node-sass 版本不相容

node-sass 與 Node.js版本相關聯;這就導致,一旦本地 Node.js 升級,就會出現 node-sass 無法工作的情況,如下報錯:

Module build failed: ModuleBuildError: Module build failed: Error: Node Sass does not yet support your current environment:
This usually happens because your environment has changed since runningnpminstall. Run npm rebuild node-sass to build the binding for your current environment.
Module build failed (from ./node_modules/sass-loader/index.js):Error: Missing binding /.../xxx/node_modules/node-sass/vendor/darwin-x64-64/binding.node
Node Sass could not find a binding for your current environment: OS X 64-bit with Node.js 10.x

通常遇見類似問題,你就需要通過 rebuild 或重新安裝 node-sass 來解決,徒增煩惱;當然,也可以藉助nvm來切換至 node-sass 對應 Node.js 版本;

npm rebuild node-sass

# Or
npm uninstall node-sass
npm install node-sass

# Or
nvm use [node-sass 對用的 Node 版本]

需要 node-gyp 作為先決條件

node-sass需要 node-gyp 作為先決條件,而node-gyp又需要您安裝了相容版本的 Python,嘖嘖,這真是,不出問題還好;出現就得好一番折騰(而且其報錯並不是很友好,就需要定位排查、查閱各種資料來修復)。

node-gyprequires that you have installed a compatible version of Python, one of: v2.7, v3.5, v3.6, v3.7, or v3.8. If you have multiple Python versions installed ......

先前因為需要,在 Mac 上,需要將 Python2 升級至 Python3,之後就導致各種node-sass問題,好一番折騰才修復。

gyp verb check python checking for Python executable "python" in the PATH
gyp verbwhichsucceeded python /usr/local/bin/python
gyp ERR! configure error
gyp ERR! stack Error: Command failed: /usr/local/bin/python -c import sys; print "%s.%s.%s" % sys.version_info[:3];
gyp ERR! stack File "", line 1
gyp ERR! stack import sys; print "%s.%s.%s" % sys.version_info[:3];
gyp ERR! stack ^
gyp ERR! stack SyntaxError: invalid syntax
/Users/xxx/.node-gyp/12.13.0/include/node/v8.h:3039:5: note: candidateconstructor not
viable: requires 2 arguments, but 1 was provided
Utf8Value(Isolate* isolate, Localv8::Valueobj);>

binding.node 源無法訪問或速度慢

實際上 node-sass 依賴了一個二進位制檔案 binding.node,從 npm 源安裝完本體後還會從 github(預設源) 下載 binding.node;這就導致預設情況下,下載 node-sass 依賴很遲緩。

以上,SASS 不僅需要額外安裝 node-sass (很慢),而且跟本地開發環境(Node.js,Python)高度掛鉤,容易出現各種詭異問題,故而不推薦使用;而且,SASS 所提供的常用功能,Less、Stylus 也同樣具備;而且 Less、Stylus 易於安裝,使用便捷,何樂不為?

推薦使用Less 或 Stylus

Less:Less 是一門 CSS 預處理語言,它擴充套件了 CSS 語言,增加了變數、Mixin、函式等特性,使 CSS 更易維護和擴充套件。Less 可以執行在 Node 或瀏覽器端。

Stylus:Stylus 是一種創新的樣式表語言,可編譯為 CSS。受 SASS 的啟發,Stylus 是用 node.js 構建的,並能夠在本互動式教程中說明的瀏覽器中執行。

人生苦短兮,預處理 CSS,我選 Less。