Tailwind CSS Integration for .NET Projects
$ dotnet add package Tailwind.MSBuild<a name="readme-top"></a>
<div align="center">Tailwind CSS Integration for .NET Projects
<br /> <!-- PROJECT SHIELDS --> <hr /> </div> <details> <summary>Table of Contents</summary> <ol> <li><a href="#about-the-project">About The Project</a></li> <li> <a href="#getting-started">Getting Started</a> <ul> <li><a href="#installation">Installation</a></li> <li><a href="#basic-use">Basic Use</a></li> <li><a href="#watch-mode">Watch Mode</a></li> </ul> </li> <li> <a href="#customize-your-build">Customize Your Build</a> <ul> <li><a href="#setting-build-properties">Setting Build Properties</a></li> <li><a href="#executing-tasks">Executing Tasks</a></li> </ul> </li> <li><a href="#license">License</a></li> </ol> </details> <p align="right">(<a href="#readme-top">back to top</a>)</p> <p align="right">(<a href="#readme-top">back to top</a>)</p> <p align="right">(<a href="#readme-top">back to top</a>)</p> <p align="right">(<a href="#readme-top">back to top</a>)</p> <p align="right">(<a href="#readme-top">back to top</a>)</p> <p align="right">(<a href="#readme-top">back to top</a>)</p> <p align="center">Copyright © 2023 Michael Rasicci</p> <!-- MARKDOWN LINKS & IMAGES -->Tailwind.MSBuild is a NuGet package that adds MSBuild tasks for building Tailwind CSS to your project. With this package, you can easily integrate Tailwind CSS into your .NET project and automatically generate your stylesheets as part of your project's build process. It will also download the Tailwind Standalone CLI so you don't need to have npm installed.
Disclaimer: This project is not affiliated with Tailwind Labs.
Use your preferred method of managing NuGet packages or use one of the following installation methods.
Install Tailwind.MSBuild using the NuGet Package Manager or with the following command in the Package Manager Console:
PM> Install-Package Tailwind.MSBuildYou can add a package reference using the following command when the .NET CLI is available:
> dotnet add package Tailwind.MSBuildYou can manually add the following lines to your .csproj file within an ItemGroup:
<PackageReference Include="Tailwind.MSBuild" Version="2.*">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>Note: You will need to build your project once in order to create the initial configuration when using this method.
Once installed, you can customize Tailwind by modifying the tailwind.input.css files in your configuration directory which defaults to $(MSBuildProjectDirectory)\Properties\. If the directory or file does not exist, a default configuration will be initialized for you the next time you build your project. By default this file will add your project directory as a source path for Tailwind to scan for class names. For detailed instructions on configuring Tailwind CSS, see the official Configuration guide.
Each time you build your project, a css file will be generated at $(MSBuildProjectDirectory)\wwwroot\css\tailwind.css. You can also customize your input and output file paths. For example, to set your input file to "src/styles/tailwind.css" and your output file to "wwwroot/css/tailwind.css", you can add the following to your .csproj file:
<PropertyGroup Label="Tailwind Properties">
<TailwindInputFile>$(MSBuildProjectDirectory)\src\styles\tailwind.css</TailwindInputFile>
<TailwindOutputFile>$(MSBuildProjectDirectory)\wwwroot\css\site.css</TailwindOutputFile>
</PropertyGroup>Tailwind.MSBuild supports watch and hot-reload workflows by starting the Tailwind CLI in watch mode when it can detect a watch build.
Auto-detected scenarios:
$(BuildingInsideVisualStudio) or $(DotNetWatchBuild) is true, TailwindWatch defaults to true and the build runs in watch mode.dotnet watch build or dotnet watch run --no-hot-reload, the TailwindStartWatchForDotNetWatch target runs after GenerateWatchList to start the build in watch mode.Manual override for hot-reload:
dotnet watch run (the default dotnet watch behavior), GenerateWatchList is not run and $(DotNetWatchBuild) is not set so there is no way to detect those builds. Use dotnet watch -p:TailwindWatch=true or add <TailwindWatch>true</TailwindWatch> in your MSBuild or project files to force tailwind to start in watch mode.When watch mode is enabled, Tailwind.MSBuild launches the Tailwind CLI in its own window and does not manage the lifecycle further. Close that window when you want it to stop running.
To avoid duplicate Tailwind CLI instances during watch builds, Tailwind.MSBuild tracks running processes in a lock file. You can override the lock file location with TailwindLockFile.
If you want to change any of the default settings Tailwind.MSBuild uses, you can override them by setting any of the following properties in your .csproj file.
| MSBuild Property Name | Default Value | Description |
|---|---|---|
| TailwindVersion | latest | The version tag of the tailwind release to use. |
| TailwindInstallPath | $(MSBuildThisFileDirectory)..\cli\ | The directory where the tailwindcss cli should be located. |
| TailwindConfigDir | $(MSBuildProjectDirectory)\Properties\ | The directory containing the tailwind configuration files. |
| TailwindInputFile | tailwind.input.css | The name of the input css file. |
| TailwindOutputFile | $(MSBuildProjectDirectory)\wwwroot\css\tailwind.css | The path where the output css file will be located. |
| TailwindMinify | false for Debug builds, true for Release builds | Whether the generated css should be minified or not. |
| TailwindWatch | true when BuildingInsideVisualStudio or DotNetWatchBuild, otherwise false | Whether Tailwind runs in watch mode. |
| TailwindLockFile | $(BaseIntermediateOutputPath)\tailwind-cli\watch.lock | The path to the Tailwind CLI lock file. |
⚠️ A note about
TailwindInstallPath:For the default install path,
$(MSBuildThisFileDirectory)expands to the directory where Tailwind.MSBuild.props was extracted to. This means the default value ofTailwindInstallPathis the equivilant of{NuGetPackageCache}\tailwind.msbuild\*VERSION*\cli\.
Here is a sample configuration that overrides every setting.
<PropertyGroup Label="Tailwind Properties">
<!-- Lock Tailwind Version -->
<TailwindVersion>v4.1.18</TailwindVersion>
<!-- Place Tailwind CLI in obj directory -->
<TailwindInstallPath>$(BaseIntermediateOutputPath)\tailwind-cli\</TailwindInstallPath>
<!-- Custom input and output file paths -->
<TailwindConfigDir>$(MSBuildProjectDirectory)\Tailwind\</TailwindConfigDir>
<!-- File names are relative to the TailwindConfigDir unless an absolute path is specified -->
<TailwindInputFile>input.css</TailwindInputFile>
<TailwindOutputFile>..\wwwroot\css\site.min.css</TailwindOutputFile>
<!-- Always minify the generated css -->
<TailwindMinify>true</TailwindMinify>
<!-- Force watch mode -->
<TailwindWatch>true</TailwindWatch>
<!-- Override lock file location -->
<TailwindLockFile>$(BaseIntermediateOutputPath)\tailwind-cli\watch.lock</TailwindLockFile>
</PropertyGroup>Tailwind.MSBuild.targets defines a build target named BuildTailwind that runs before the BeforeBuild target. This target executes two tasks that Tailwind.MSBuild implements. The first task is GetTailwindCLI which returns the absolute path to the tailwind CLI to the build engine. The output is stored in a property which is then passed to BuildTailwindCSS as the value for the StandaloneCliPath parameter.
For advanced scenarios where you need to run the tasks during a different point in the build process, see the tables below for the parameters required for each task and the associated MSBuild Property passed by default. It is recommended to read Tailwind.MSBuild.targets as an example of how to properly invoke the MSBuild tasks.
| Task Parameter | MSBuild Property |
|---|---|
| Version | TailwindVersion |
| RootInstallPath | TailwindInstallPath |
<GetTailwindCLI Version="$(TailwindVersion)" RootInstallPath="$(TailwindInstallPath)">
<Output TaskParameter="StandaloneCliPath" PropertyName="StandaloneCliPath" />
</GetTailwindCLI>| Task Parameter | MSBuild Property |
|---|---|
| StandaloneCliPath | Output of GetTailwindCLI |
| ConfigDir | TailwindConfigDir |
| InputFile | TailwindInputFile |
| OutputFile | TailwindOutputFile |
| Minify | TailwindMinify |
| Watch | TailwindWatch |
| WatchLockFile | TailwindLockFile |
| ProjectDirectory | MSBuildProjectDirectory |
<BuildTailwindCSS StandaloneCliPath="$(StandaloneCliPath)"
ConfigDir="$(TailwindConfigDir)"
InputFile="$(TailwindInputFile)"
OutputFile="$(TailwindOutputFile)"
Minify="$(TailwindMinify)"
Watch="$(TailwindWatch)"
WatchLockFile="$(TailwindLockFile)"
ProjectDirectory="$(MSBuildProjectDirectory)">
<Output TaskParameter="GeneratedCssFile" PropertyName="GeneratedCssFile" />
</BuildTailwindCSS>Distributed under the MIT License. See LICENSE.md for more information.