MillsSoftware.CoreSassCompiler 1.0.17

dotnet add package MillsSoftware.CoreSassCompiler --version 1.0.17
NuGet\Install-Package MillsSoftware.CoreSassCompiler -Version 1.0.17
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MillsSoftware.CoreSassCompiler" Version="1.0.17" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MillsSoftware.CoreSassCompiler --version 1.0.17
#r "nuget: MillsSoftware.CoreSassCompiler, 1.0.17"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install MillsSoftware.CoreSassCompiler as a Cake Addin
#addin nuget:?package=MillsSoftware.CoreSassCompiler&version=1.0.17

// Install MillsSoftware.CoreSassCompiler as a Cake Tool
#tool nuget:?package=MillsSoftware.CoreSassCompiler&version=1.0.17

Core Sass Compiler

The Basic Idea

.NET Core module to add SASS compilation and minification to any ASP.NET Core project.

How to Install

  1. Install nuget package via GitHub Packages. Note that this package is NOT on the main nuget feed yet.

  2. Add to services via dependency injection (here added to Program.cs in a .NET 6 project):

using MillsSoftware.CoreSassCompiler;

builder.Services.AddCoreSassCompiler(
    new SassProfile()
    {
        Name = "main-css",
        Url = "/css/main",
        InputFile = Path.Combine(environment.WebRootPath, "scss", "index.scss"),
        Minify = true,
        CacheMinutes = 1440,
        ChangeToken = environment.WebRootFileProvider.Watch("scss/*.scss")
    }
);

This sets up a profile that defines what SASS should be compiled (using LibSassHost) and whether it should be minified (using NUglify). The compiled SASS will be cached for the number of minutes specified. The cached compilation has a dependency on the scss/*.scss files, so editing any of these files results in a new compilation. Note that multiple profiles can be added if necessary.

  1. Link to the compiled SASS via a tag helper (here added in the _Layout.cshtml):
<sass name="main-css" />

Note that this won't work until you add the following directive to _ViewImports.cshtml that allows your views to use the new tag helper:

@addTagHelper *, MillsSoftware.CoreSassCompiler

The end result in your rendered layout is a standard link tag with a hash based on the file content:

<link rel="stylesheet" href="/css/main?UeBVvswRn5/Iod8IXrQ61A==" />
  1. Add a controller to render the CSS:
using Microsoft.AspNetCore.Mvc;
using MillsSoftware.CoreSassCompiler;

namespace MySite.Controllers
{
    [Route("css")]
    public class CssController : Controller
    {
        private readonly SassCompiler _compiler;
        private readonly IWebHostEnvironment _environment;

        public CssController(SassCompiler compiler, IWebHostEnvironment environment)
        {
            _compiler = compiler;
            _environment = environment;
        }

        [HttpGet("main")]
        public IActionResult Main()
        {
            var compilation = _compiler.GetCompilation("main-css");
            if (compilation == null) return NotFound();

            if (!compilation.IsSuccess && _environment.IsDevelopment())
            {
                return Content($"{compilation.SassErrors} {compilation.MinifierErrors}");
            }

            return Content(compilation.SassResult ?? "", "text/css");
        }
    }
}

Future Changes

  1. Add to the main nuget package feed.

  2. Set up an automatic controller to return the CSS. This would eliminate step 4 above, but would potential not give you as much control over the output (e.g. what to do when the compilation fails).

  3. Add HTTP cache headers to the CSS output.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.17 441 9/22/2022
1.0.16 406 9/22/2022
1.0.15 392 9/22/2022
1.0.14 369 9/22/2022
1.0.12 374 9/21/2022
1.0.10 378 8/3/2022