Git Product home page Git Product logo

tailwind.extensions.aspnetcore's Introduction

Use Tailwind's JIT mode with dotnet run and dotnet watch run

Makes it possible to use the new "Just In Time" builds for Tailwind (Tailwind 3+) with ASP.NET Core.

Works for Blazor WASM applications that are hosted via ASP.NET Core, and Blazor Server applications.

Note it doesn't work with Blazor WASM apps that aren't hosted via ASP.NET Core.

Usage

Create a new Hosted Blazor WASM, or Blazor Server project.

CD to the Client App's folder (if Blazor WASM) or Blazor Server App's folder.

Run these commands:

npm install -D tailwindcss cross-env
npx tailwindcss init

This will install Tailwind and the handy cross-env utility via NPM, then create a tailwind.config.js file.

Now update the tailwind.config.js file to include all your .razor and .cshtml files.

module.exports = {
  content: ["**/*.razor", "**/*.cshtml", "**/*.html"],
  theme: {
    extend: {},
  },
  plugins: [],
}

Now you'll want to create the Tailwind input stylesheet. This is the stylesheet that Tailwind will then pick up and build.

Here's the minimum you'll need...

Styles\input.css

@tailwind base;
@tailwind components;
@tailwind utilities;

Finally, update your package.json file to add this script.

  "scripts": {
    "tailwind": "cross-env NODE_ENV=development ./node_modules/tailwindcss/lib/cli.js -i ./Styles/input.css -o ./wwwroot/css/output.css --watch"
  },

Make sure .Styles/input.css is pointing to the css file you created in the last step. You can control where the resulting css file is created by specifying your own value for the -o parameter.

That takes care of the Tailwind setup, now we just need to make this run when you run your ASP.NET Core project during Development.

Make sure you switch to the Server App's folder if you're using Blazor WASM (hosted).

Run this command to install the Tailwind AspNetCore NuGet package.

dotnet add package Tailwind.Extensions.AspNetCore --version 1.0.0-beta2

Now head over to Program.cs and add this code before app.Run();

if (app.Environment.IsDevelopment())
{
    app.RunTailwind("tailwind", "./");
}

The second argument is the path to the folder containing your package.json file. If you're using Blazor WASM you'll probably need something like this...

if (app.Environment.IsDevelopment())
{
    app.RunTailwind("tailwind", "../Client/");
}

You'll also need to add this using statement:

using Tailwind;

Now, run dotnet watch run and try modifying your Razor components (using Tailwind's utility classes).

You should see logs indicating that tailwind has rebuilt the CSS stylesheet successfully.

Known Issues

If you use dotnet watch run to launch your application you'll likely find everything works as you'd expect: you can make changes to your razor files and see those changes reflected in the browser.

With Visual Studio the experience is somewhat more hit and miss. It looks like there is an issue whereby VS hot reload doesn't detect and apply changes to .css files when you have a Blazor WASM project hosted via ASP.NET Core.

There's an open Visual Studio feedback item about that here: Hot Reload For CSS Not Working With Blazor WebAssembly Hosted

Might be worth up-voting that if you're having difficulties.

tailwind.extensions.aspnetcore's People

Contributors

jonhilt avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.