Avoiding npm & nodejs when using asp.net and tailwindcss using Bun
Using tailwindcss with asp.net can be annoying with having to install node and npm on windows and updating it etc. Installing bun is much simpler and doesn’t require npm.
Introduction to Bun
Bun is a package manager that aims to simplify the development process by eliminating the need for a node_modules
folder and a package.json
file. It works by installing packages into a global cache and using them from there, effectively making it work like Deno. This approach can significantly speed up the build process and reduce the clutter in your project directories.
Setting Up Your ASP.NET Project
To get started, you’ll need to have an ASP.NET project set up. If you don’t have one, you can create a new project using the .NET CLI with the command dotnet new webapp -o MyWebApp
.
Integrating Bun with ASP.NET
Adding Bun to Your Project
First, you need to ensure that Bun is installed on your system. If it’s not, you can install it by following the instructions on the Bun website. Once installed, you can verify the installation by running bun -v
in your terminal.
Configuring Your Project to Use Bun
To make your ASP.NET project use Bun for building and reloading, you need to add specific targets to your .csproj
file. These targets will check for Bun’s presence and use it to build your CSS with Tailwind CSS.
Open your .csproj
file and add the following targets:
To get hot reloading open a new shell and do bun tailwind.config.js watch
.
Setting Up Tailwind CSS with Bun
Next, you’ll need to set up Tailwind CSS in your project. Create a tailwind.config.js
file in your project root with the following content:
This configuration file not only sets up Tailwind CSS but also acts as a script manager, allowing you to build and watch your CSS files with Bun.
Creating Your Stylesheet
Create a Styles/app.css
file in your project with the following content:
Then, add a reference to this stylesheet in your .cshtml
files to apply the Tailwind CSS styles.
Docker Integration
If you’re using Docker for your development environment, you can integrate Bun by updating your Dockerfile
to install Bun. Here’s how you can do it:
This will ensure that Bun is installed in your Docker container, making it available for building your project.
Conclusion
This works pretty well and is fast but idea integrations stop working so i just create a package.json and basically removed all benefits of this later lol. You still dont need npm and node though :D
Credit
Based on https://stackoverflow.com/questions/57669027/how-do-you-add-tailwind-css-into-a-blazor-app