Rajtík's blog

← Back to home

Programming 3 min read

nette-tailwind: Nette web-project with Tailwind 4

I released nette-tailwind, a Nette web-project with Tailwind CSS 4 added to Vite. How to get it running and what to watch out for.

Three stacked blocks, a red one on top, bold lettering on the bottom one

There is a new repository on GitHub, rajtik76/nette-tailwind. It is based on the official nette/web-project, and I wired Tailwind CSS 4 into its Vite build. It runs on PHP 8.4, Nette 3.2, Latte 3 and Vite 8. Tailwind is loaded through the @tailwindcss/vite plugin right next to @nette/vite-plugin.

It is not on Packagist yet, so composer create-project will not help. Clone the repository:

git clone https://github.com/rajtik76/nette-tailwind.git
cd nette-tailwind
composer install
npm install
npm run build

Do not skip the last command. Vite is enabled in config/common.neon, and until you build the assets, the page fails with Failed to read Vite manifest. For development, run npm run dev and php -S localhost:8000 -t www side by side. In debug mode, {asset 'main.js'} loads files from the Vite dev server, so changes to templates and CSS show up immediately.

Two things you will not see at first

The wiring itself is a few lines. But Vite's root in this project is the assets/ folder, and Tailwind looks for classes only there. Latte templates live in app/, so assets/main.css adds that folder as another source:

@import "tailwindcss";
@source "../app";

Without the second line the build would pass, but the classes used in templates would be missing from the resulting CSS. If you add templates outside app/, add another @source for them.

The second thing concerns Laravel Herd and Valet. Both run index.php through their own router, so Nette cannot derive the correct wwwDir from SCRIPT_FILENAME. That is why app/Bootstrap.php sets it explicitly. For both you also have to write a custom driver that sets www/ as the document root.

What it adds to web-project

The composer run ci:check command runs a lint of Latte templates and NEON configuration, PHPStan at level 8 and smoke tests in Nette Tester. The same runs in GitHub Actions on PHP 8.4 and 8.5. Templates are checked by a custom latte-lint script that boots the application container, so it knows the app's filters and functions. Unlike Latte\Tools\Linter, it fails on warnings too. The reason: a template with an unknown filter would fail at render time.

The welcome and error pages got a blueprint look: a dark blue ground with a grid and cubes. Colors and the font live in @theme in assets/main.css, and all 4xx templates share one layout. The 500.phtml and 503.phtml pages are different: they use neither Tailwind nor the layout, because they must render even when the application or the build is broken. Their CSS is inline, so when you change colors in main.css, you have to update them in both templates by hand.

The skeleton stays bare on purpose, the database runs only in sqlite::memory:.