Swiftpack.co - johnfairh/swift-sass as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by johnfairh.
johnfairh/swift-sass 2.0.0-pre1
Embed the Dart Sass compiler in Swift with custom importers and functions
⭐️ 5
🕓 2 weeks ago
macOS linux
.package(url: "https://github.com/johnfairh/swift-sass.git", from: "2.0.0-pre1")

Platforms codecov Tests Sass

Swift Sass

Embed the Dart Sass compiler in your Swift program. Write stylesheet importers and SassScript functions in Swift.

This package provides a Swift language interface to a separate Sass implementation. The DartSass module lets you access Dart Sass, the most up to date implementation of the Sass language. It runs the Dart Sass compiler as a separate process and communicates with it using the Sass embedded protocol. If you come across another implementation of the 'compiler' side of the protocol then that should work fine too.

This package doesn't support LibSass right now. More info.



import DartSass

let compiler = try Compiler()

let results = try await compiler.compile(fileURL: scssFileURL)


Although the compiler output is more structured, if this is all you want to do then you're probably better off running the binary directly. The reason to use this package is to provide custom implementations of @use rules to load content, and custom functions to provide application-specific behavior:

struct ExtrasImporter: Importer {
  func canonicalize(importURL: String) async throws -> URL? {
    guard importURL == "extras" else {
      return nil
    return URL(string: "custom://extras")

  func load(canonicalURL: URL) async throws -> ImporterResults {

let customFunctions: SassFunctionMap = [
  "userColorForScore($score)" : { args in
    let score = try args[0].asInt()
    return SassColor(...)

let results = try await compiler.compile(
    fileURL: scssFileURL,
    importers: [
    functions: customFunctions
// stylesheet

@use "extras";

.score-mid {
  color: userColorForScore(50);

DartSass is built on NIO but the user interface is entirely Swift 5.5 async-await.



  • Swift 5.7
  • macOS 13+ (tested on macOS 13.2 x64)
  • Linux (tested on Ubuntu 22.04 x64)
  • Embedded Sass Protocol version 1.2.0


Only with Swift Package Manager, via Xcode or directly:

Package dependency:

.package(name: "swift-sass",
         url: "https://github.com/johnfairh/swift-sass.git",
         from: "2.0.0-pre1")

Target dependency:

.product(name: "DartSass", package: "swift-sass"),

The Swift package bundles the embedded Dart Sass compiler for macOS and Linux (specifically Ubuntu Focal/20.04 64-bit) both x86_64 and arm64. The correct binary is selected at build time. The selection is made based on the host architecture though, so if you want to cross-compile you'll need to assemble the results manually.

For other platforms you will need to either download the correct version from the release page or build it manually, ship it as part of your program's distribution, and use this initializer.

There is no need to install a Dart runtime or SDK as part of this, the dart-sass-embedded program is standalone. The version required is shown in the VERSION_DART_SASS file.

On LibSass

LibSass is the C++ implementation of Sass. In recent years it has fallen behind the specification and reference implementations, and was deprecated in 2020. However, work is underway to revive the project and it may be that LibSass 4 emerges as an alternative Sass implementation with the same level of language support as Dart Sass. As of spring 2023 this revival effort is on hold: I'm not holding my breath.

See the experimental libsass4 branch for the current state of development: if LibSass itself manages to get to a relased V4 then this swift-sass package will support it as an alternative integration.


Welcome: open an issue / [email protected] / @[email protected]


Distributed under the MIT license.


Stars: 5
Last commit: 1 week ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Related Packages

Release Notes

2 weeks ago
  • Rewrite Compiler to be an actor and use NIOAsyncChannel to communicate with the compiler. Interface is approximately the same; syncShutdownGracefully() is gone though to match NIO's approach.

  • Shutdown behaviour has changed slightly: if you have compiler jobs running concurrently to a shutdown request then they are likely to be cancelled rather than left to run before actioning the shutdown.

  • Make all Sass functions async: delete SassFunction types and then rename SassAsyncFunction types.

I expect to end the 'pre' series when NIOAsyncChannel is officially blessed; there is also activity on the Sass side to do with reworking the protocol for higher performance that may get rolled in here depending on timing and impact.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics