Setting up
First install the package for transpiling the scss files.
dotnet add package AspNetCore.SassCompiler
Note that this will also add the dart runtime binaries as this uses dart-sass under the hood.
Initialization
Since this is only used for transpiling the scss/sass files, you only want to run it in development environments, so register it with a debug #if directive during service registration.
//Code omitted
#if DEBUG
builder.Services.AddSassCompiler();
#endifConfiguration
The settings for the transpiler live in your appsettings.json file - ideally appsettings.Development.json. A basic example could look like this
{
"SassCompiler": {
"Source": "Styles",
"Target": "wwwroot/styles",
"Arguments": "--style=compressed",
"GenerateScopedCss": true,
"ScopedCssFolders": ["Pages", "Components", "Layout", "../MySolution.MyOtherClassLibraryProject/Pages"],
"IncludePaths": [],
"Compilations": [
{
"Source": "wwwroot/styles/main.scss",
"Target": "wwwroot/styles/main.css"
}
],
"Configurations": {
"Debug": {
"Arguments": "--style=expanded"
}
}
}
}There are two main compilation areas declared here
- The
Compilationsarray defines an input file and an output file, denoted bySourceandTarget, respectively. These are going to want to be referenced in your html somewhere (make sure to reference the compiled .css file, and not the .scss file!)
<!-- code omitted -->
<link rel="stylesheet" href="styles/main.css" />- The
ScopedCssFoldersarray defines a collection of folders where scoped css can be found and transpiled. If you’re using scoped css in another project, you can also reference relative paths here. With this in place you can now just writeIndex.razor.scssand the transpiler will generateIndex.razor.css, as well as a source map for the sass file. Make sure you also reference scoped css in your main razor file
<!-- code omitted -->
<link rel="stylesheet" href="MySolution.MyProject.styles.css" />With all this in place you should now be able to write sass instead of css and it all work seamlessly!