SharpScss is a P/Invoke .NET wrapper around libsass to convert SCSS to CSS.
Based on the version of
libsass 3.6.6
- Pure P/Invoke .NET wrapper, no C++/CLI involved
- Supports converting from a string or from a file
- Supports include paths
- Supports for source maps
- Supports for
libsassuser custom importer callback inScssOptions.TryImport - Supports for
netstandard2.0andnet8.0+ - Supports the following platforms:
win-x86win-x64win-armwin-arm64linux-x64linux-armlinux-arm64linux-musl-x64linux-musl-armlinux-musl-arm64osx-x64osx-arm64
For older .NET2.0, .NET3.5, .NET4.x+ and netstandard1.3, you need to download the 1.4.0 version.
SharpScss API is simply composed of a main Scss class:
Scss.ConvertToCss: to convert aSCSSstring to aCSS
var result = Scss.ConvertToCss("div {color: #FFF;}")
Console.WriteLine(result.Css);
Scss.ConvertFileToCss: to convert aSCSSfile to aCSS
var result = Scss.ConvertFileToCss("test.scss")
Console.WriteLine(result.Css);
Using the ScssOptions you can specify additional parameters:
var result = Scss.ConvertToCss(@"div {color: #FFF;}", new ScssOptions()
{
InputFile = "Test.scss",
OutputFile = "Test.css", // Note: It will not generate the file,
// only used for exception reporting
// includes and source maps
GenerateSourceMap = true
});
Console.WriteLine(result.Css);
Console.WriteLine(result.SourceMap);
You can use also custom dynamic import through the delegate ScssOptions.TryImport. Note that in that cases ScssOptions.IncludePaths is not used
and it is the responsability of the TryImport to perform the resolution (e.g on a virtual file system):
var result = Scss.ConvertToCss(@"@import ""foo"";", new ScssOptions()
{
InputFile = "test.scss",
TryImport = (ref string file, string path, out string scss, out string map) =>
{
// Add resolve the file
// file = resolvedFilePath; // Can change the file resolved
scss = ...; // TODO: handle the loading of scss for the specified file
map = null;
return true;
}
});
SharpScss depends on the native runtime libsass. This runtime is compiled for the following platform/runtime:
win-x86win-x64win-armwin-arm64linux-x64linux-armlinux-arm64linux-musl-x64linux-musl-armlinux-musl-arm64osx-x64osx-arm64
On .NET Core (net8.0), the runtime is selected based on the Runtime Identifier - RID of your project.
- You can add to your csproj the specific targeting runtimes your
net8.0with<RuntimeIdentifiers>win-x86;linux-x64</RuntimeIdentifiers>or<RuntimeIdentifier>if you have only one runtime to target (See Additions to the csproj format for .NET Core)
Currently, the compiled version of libsass shipped with SharpScss is a custom build from the fork xoofx/libsass
This fork is mainly allowing to compile libsass without the MSVC C/C++ Runtime on Windows and provide a GitHub CI action to compile all different platforms.
This software is released under the BSD-Clause 2 license.
Alexandre Mutel aka xoofx