compileString

  • 相容性
    Dart Sass
    自 1.45.0 版本起
    Node Sass

    將內容為 source 的樣式表同步編譯為 CSS。如果成功,則返回 CompileResult;如果失敗,則拋出 Exception。

    這僅允許同步 Importer 和 CustomFunction。

    ⚠️ 注意!

    當使用 sass-embedded npm 套件進行單次編譯時,**compileStringAsync 幾乎總是比 compileString 快**,這是因為使用工作者執行緒模擬同步訊息傳遞的額外開銷,以及並行編譯在主執行緒上被阻塞。

    如果您使用 sass-embedded npm 套件執行多個編譯,使用 Compiler 會比模組層級方法提供一些速度提升,而 AsyncCompiler 則會更快。

    範例

    const sass = require('sass');

    const result = sass.compileString(`
    h1 {
    font-size: 40px;
    code {
    font-face: Roboto Mono;
    }
    }`);
    console.log(result.css);

    參數

    • sourcestring(字串)
    • 選用 optionsStringOptions<"sync">(字串選項)

    回傳 CompileResult(編譯結果)