W3cubDocs

/Sass

compileString

  • Compatibility:
    Dart Sass
    since 1.45.0
    Node Sass

    Synchronously compiles a stylesheet whose contents is source to CSS. If it succeeds it returns a CompileResult, and if it fails it throws an Exception.

    This only allows synchronous Importers and CustomFunctions.

    When using the sass-embedded npm package for single compilations, compileStringAsync is almost always faster than compileString, due to the overhead of emulating synchronous messaging with worker threads and concurrent compilations being blocked on main thread.

    If you are running multiple compilations with the sass-embedded npm package, using a Compiler will provide some speed improvements over the module-level methods, and an AsyncCompiler will be much faster.

    Example

    constsass = require('sass');

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

    Parameters

    Returns CompileResult

© 2006–2025 the Sass team, and numerous contributors
Licensed under the MIT License.
https://sass-lang.com/documentation/js-api/functions/compileString