CssExtractor.MSBuild 1.5.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package CssExtractor.MSBuild --version 1.5.0
                    
NuGet\Install-Package CssExtractor.MSBuild -Version 1.5.0
                    
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="CssExtractor.MSBuild" Version="1.5.0">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CssExtractor.MSBuild" Version="1.5.0" />
                    
Directory.Packages.props
<PackageReference Include="CssExtractor.MSBuild">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CssExtractor.MSBuild --version 1.5.0
                    
#r "nuget: CssExtractor.MSBuild, 1.5.0"
                    
#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.
#:package CssExtractor.MSBuild@1.5.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CssExtractor.MSBuild&version=1.5.0
                    
Install as a Cake Addin
#tool nuget:?package=CssExtractor.MSBuild&version=1.5.0
                    
Install as a Cake Tool

CssExtractor.MSBuild

Overview

CssExtractor.MSBuild is a custom MSBuild task designed to extract CSS classes and styles from various file types, deduplicate them, and write them to a single output file. This output can be utilized by CSS processors such as Tailwind CSS. The task is configurable via regular expressions, allowing users to specify which files to exclude and how to extract CSS classes.

Features

  • Automatically extracts CSS classes from HTML, CSHTML, Razor, and C# files using built-in patterns
  • Includes comprehensive patterns for standard HTML class attributes and common web frameworks
  • Supports custom regex patterns for extracting dynamically generated CSS classes in C# code
  • Deduplicates CSS classes to ensure a clean output
  • Smart default output locations for library vs application projects
  • Configurable file inclusion/exclusion patterns

Installation

To use CssExtractor.MSBuild in your project, include it as a PackageReference in your project file (.csproj):


<ItemGroup>
  <PackageReference Include="CssExtractor.MSBuild" Version="1.3.0">
    <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    
    <PrivateAssets>all</PrivateAssets>
  </PackageReference>
</ItemGroup>

Configuration

CssExtractor.MSBuild works out-of-the-box with sensible defaults. It automatically scans common file types (HTML, CSHTML, Razor, and C# files) using built-in patterns that detect:

  • Standard HTML class attributes
  • React-style className attributes
  • Various CSS-in-JS patterns
  • Common templating syntaxes

Built-in Patterns

The extractor includes comprehensive built-in regex patterns for finding CSS classes in:

  • HTML files (**/*.html)
  • Razor files (**/*.razor)
  • CSHTML files (**/*.cshtml)
  • C# files (**/*.cs)

Custom Patterns for Dynamic CSS Classes

While the built-in patterns handle most standard cases, you can define custom patterns to extract CSS classes that are dynamically generated in your C# code. Suppose you have some fluent API calls like:

var component = builder.WithClass("btn btn-primary");
var icon = element.WithIcon("fas fa-user");

You can define custom extraction patterns in your project file as follows:

<PropertyGroup>
  <CssExtractorCustomPatterns>
    \.WithClass\s*\(\s*"([^"]+)"\s*\)
    ;
    \.WithIcon\s*\(\s*"([^"]+)"\s*\)
  </CssExtractorCustomPatterns>
</PropertyGroup>

Advanced Configuration

There is another way to specify custom patterns that is transitive, meaning consumers of your library can also benefit from these patterns without needing to redefine them. This is useful for libraries that want to provide a consistent extraction experience.

Create a file named content/css-extractor-patterns.txt in your project as follows:

\.WithClass\s*\(\s*"([^"]+)"\s*\)
\.WithIcon\s*\(\s*"([^"]+)"\s*\)

Then, update your project file to pack it:

<ItemGroup>
    <None Include="content/css-extractor-patterns.txt" Pack="true" PackagePath="content/css-extractor-patterns.txt" />
</ItemGroup>

You can also customize files to include/exclude and output location:

<PropertyGroup>
  
  <CssExtractorOutputFile>$(MSBuildProjectDirectory)/styles/extracted-classes.txt</CssExtractorOutputFile>
</PropertyGroup>

<ItemGroup>
  
  <CssExtractorIncludeFiles Include="**/*.tsx" />
  <CssExtractorIncludeFiles Include="**/*.jsx" />
  
  
  <CssExtractorExcludeFiles Include="**/tests/**" />
</ItemGroup>

License

This project is licensed under the MIT License. See the LICENSE file for more details.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on CssExtractor.MSBuild:

Package Downloads
Htmx.Components

A comprehensive ASP.NET Core library for building interactive web applications with server-side rendering and HTMX integration. Provides ready-to-use components, state management, and built-in authorization with minimal JavaScript.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.7.4 194 7/10/2025
1.7.3 131 7/10/2025
1.7.2 131 7/10/2025
1.7.1 133 7/10/2025
1.7.0 130 7/10/2025
1.6.0 132 7/9/2025
1.5.0 131 7/9/2025
1.4.0 133 7/9/2025
1.3.2 134 7/7/2025
1.3.0 137 7/7/2025
1.2.0 135 7/4/2025
1.0.1 130 7/3/2025