You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@grails.apache.org by ja...@apache.org on 2026/05/04 12:31:44 UTC
(grails-static-website) 02/10: Tighten grails-tailwindcss v8 asset-pipeline narrative + Tailwind 4 build refinements
This is an automated email from the ASF dual-hosted git repository.
jamesfredley pushed a commit to branch improve-v8-guides-pre-release
in repository https://gitbox.apache.org/repos/asf/grails-static-website.git
commit 784043aab23b2bf89cc35cd60ff99d6be9aae5ab
Author: James Fredley <ja...@users.noreply.github.com>
AuthorDate: Mon May 4 08:28:26 2026 -0400
Tighten grails-tailwindcss v8 asset-pipeline narrative + Tailwind 4 build refinements
The asset-pipeline integration was correct end-to-end but only mentioned
once, in a NOTE block at the bottom of gradleWiring.adoc. A reader who
skimmed could easily miss that the asset pipeline is what bundles,
fingerprints, gzips, and serves the compiled Tailwind CSS as part of
application.css.
Narrative changes:
* whatYouWillBuild.adoc adds the explicit "...where the asset pipeline
bundles, fingerprints, and gzips that file into the application.css
bundle the layout serves" sentence.
* gradleWiring.adoc promotes the asset-pipeline manifest from the NOTE
sidebar into body prose, naming the *= require app directive and
the asset:stylesheet taglib that the next chapter uses.
* firstPaint.adoc adds a one-line callout pointing at <asset:stylesheet
src="application.css"/> as the asset-pipeline taglib that resolves
the manifest at runtime.
Snippet refinements:
* build.gradle uses Provider syntax for tailwindBuild and npmInstall
dependsOn (more idiomatic than the string form), declares
package-lock.json as an extra input, scans grails-app/services,
grails-app/taglib, and src/main/groovy in addition to views and
controllers, and adds testAndDevelopmentOnly Bootstrap webjars so
the dev console pages still render.
* src/main/css/input.css adds tag::components / end::components markers
so componentLayer.adoc can include only the relevant block via
include::../snippets/src/main/css/input.css[tag=components,indent=0].
* application.css manifest collapses to *= require app.
* main.gsp and index.gsp are tightened to match the chapter walkthrough.
* package.json bumps to a current Tailwind 4 line.
Across the chapters, every section that touches a file ends with a
"Files touched" bullet list to match the established
grails-fields-custom-widgets-and-wrappers v8 style and a couple of
chapter-end pointers to the matching grails-doc reference manual
sections (deployment.adoc et al.).
Assisted-by: claude-code:claude-4.6-opus
---
.../v8/guide/componentLayer.adoc | 33 +++++++++---------
.../v8/guide/configureTailwind.adoc | 10 ++++--
guides/grails-tailwindcss/v8/guide/darkMode.adoc | 39 +++++++++-------------
guides/grails-tailwindcss/v8/guide/firstPaint.adoc | 11 ++++--
.../v8/guide/gettingStarted.adoc | 6 +++-
.../grails-tailwindcss/v8/guide/gradleWiring.adoc | 28 +++++++++++-----
.../v8/guide/helpWithGrails.adoc | 4 +++
guides/grails-tailwindcss/v8/guide/inputCss.adoc | 11 +++---
.../v8/guide/installTailwind.adoc | 9 +++--
.../grails-tailwindcss/v8/guide/requirements.adoc | 7 +++-
.../v8/guide/whatYouWillBuild.adoc | 8 +++--
guides/grails-tailwindcss/v8/snippets/build.gradle | 25 ++++++++------
.../grails-app/assets/stylesheets/application.css | 7 ++--
.../v8/snippets/grails-app/views/index.gsp | 5 ++-
.../v8/snippets/grails-app/views/layouts/main.gsp | 10 +++++-
guides/grails-tailwindcss/v8/snippets/package.json | 4 +++
.../v8/snippets/src/main/css/input.css | 9 +++++
17 files changed, 144 insertions(+), 82 deletions(-)
diff --git a/guides/grails-tailwindcss/v8/guide/componentLayer.adoc b/guides/grails-tailwindcss/v8/guide/componentLayer.adoc
index 2a944f73730..462f0aee4ff 100644
--- a/guides/grails-tailwindcss/v8/guide/componentLayer.adoc
+++ b/guides/grails-tailwindcss/v8/guide/componentLayer.adoc
@@ -3,36 +3,33 @@ Two patterns repeat across the welcome page: the primary action button and the w
The relevant section of `input.css`:
[source,css]
+.src/main/css/input.css
----
-@layer components {
- .btn-primary {
- @apply inline-flex items-center justify-center rounded-md bg-blue-600 px-4 py-2 text-sm font-medium text-white shadow-sm hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 dark:bg-blue-500 dark:hover:bg-blue-600 dark:focus:ring-offset-gray-900;
- }
- .card {
- @apply rounded-lg border border-gray-200 bg-white p-6 shadow-sm dark:border-gray-700 dark:bg-gray-800;
- }
- .nav-link {
- @apply text-sm font-medium text-gray-600 hover:text-gray-900 dark:text-gray-300 dark:hover:text-white;
- }
-}
+include::../snippets/src/main/css/input.css[tag=components,indent=0][]
----
GSPs then reference them by the short name:
[source,html]
----
-<a href="${createLink(controller: 'book', action: 'create')}" class="btn-primary">New book</a>
-<div class="card">...</div>
-<a href="..." class="nav-link">Docs</a>
+<a href="https://grails.apache.org/guides/grails-tailwindcss/8/guide/index.html" class="btn-primary">Read the guide</a>
+<section class="card">...</section>
+<a href="https://grails.apache.org/docs/" class="nav-link">Docs</a>
----
Three things to notice:
-* `@apply` is still allowed in v4 - the deprecation warnings you may have seen in early v4 betas were about scoping rules, not the directive itself.
-* Component classes in v4 still respect the source order of `@layer base`, `@layer components`, `@layer utilities`. A utility class on a single element will override the same property in a `@layer components` rule.
-* Component classes do *not* defeat purging: the scanner picks them up via the `@source` directives just like raw utility classes.
+* `@apply` is still supported in v4, which keeps the component layer readable even for long utility chains.
+* Tailwind 4 also introduces `@utility` for custom utilities. For this guide we stay with `@layer components` because the goal is a tiny named component surface that reads naturally in GSPs.
+* Component classes do not defeat purging. The scanner still decides what utilities to emit from the classes and variants it can see in your source files.
[TIP]
====
-*Dynamic class names in Groovy code:* if a controller or tag library composes a class string at runtime (e.g. `"alert-${level}"`), Tailwind's static analyser cannot see the result and will purge those classes from the output. Add the variants to a safelist by writing them out as a comment in any scanned file (`/* alert-info alert-warn alert-error */`) - the scanner reads literal class names from any file matched by `@source`.
+*Dynamic class names in Groovy code:* if a controller or tag library composes a class string at runtime, Tailwind's static analyser cannot see the result. In v4 the explicit fix is `@source inline(...)` in `input.css`, for example `@source inline("alert-info alert-warn alert-error");`.
====
+
+Files touched in this chapter:
+
+* `src/main/css/input.css`
+* `grails-app/views/layouts/main.gsp`
+* `grails-app/views/index.gsp`
diff --git a/guides/grails-tailwindcss/v8/guide/configureTailwind.adoc b/guides/grails-tailwindcss/v8/guide/configureTailwind.adoc
index 97be867ce56..c3459713929 100644
--- a/guides/grails-tailwindcss/v8/guide/configureTailwind.adoc
+++ b/guides/grails-tailwindcss/v8/guide/configureTailwind.adoc
@@ -1,9 +1,13 @@
-Tailwind 4 introduced a CSS-first configuration model: most projects no longer need a `tailwind.config.js` file at all. Theme tokens, content sources, and custom variants are declared in your CSS entry point with the `@theme`, `@source`, and `@custom-variant` at-rules.
+Tailwind 4 introduced a CSS-first configuration model. Most projects no longer need a `tailwind.config.js` file at all. Theme tokens, content sources, and custom variants are declared in your CSS entry point with the `@theme`, `@source`, and `@custom-variant` at-rules.
For a Grails app that means three knobs to set:
-* *Where to scan for class names.* Tailwind 4's JIT scanner does not know about `.gsp` files out of the box, and it is conservative about reading utility-class string literals from `.groovy` source. Both have to be added with explicit `@source` directives.
+* *Where to scan for class names.* Tailwind 4's scanner does not know about `.gsp` files out of the box, and it is conservative about reading utility-class string literals from `.groovy` source. Both have to be added with explicit `@source` directives.
* *How to opt into class-based dark mode.* The default dark variant follows `prefers-color-scheme`. To let the user pick a theme and persist it, override the variant so `dark:` utilities apply when an ancestor has the `dark` class.
-* *Reusable component classes (optional).* Anything you find yourself spelling more than three or four times across GSPs is a candidate for the `@layer components` block.
+* *Reusable component classes (optional).* Tailwind 4 adds `@utility` for custom utilities, but a small `@layer components` block is still a good fit when you want a few named classes that read well in GSPs.
All three live in `src/main/css/input.css`, which we will write next.
+
+Files touched in this chapter:
+
+* `src/main/css/input.css`
diff --git a/guides/grails-tailwindcss/v8/guide/darkMode.adoc b/guides/grails-tailwindcss/v8/guide/darkMode.adoc
index f9d176d24fe..f0cc4056ed1 100644
--- a/guides/grails-tailwindcss/v8/guide/darkMode.adoc
+++ b/guides/grails-tailwindcss/v8/guide/darkMode.adoc
@@ -1,18 +1,11 @@
The `@custom-variant dark (&:where(.dark, .dark *));` line in `input.css` made every `dark:`-prefixed utility opt-in on the presence of the `dark` class on `<html>`. Two small bits of markup in `main.gsp` flip it on and off.
-The first is a four-line pre-paint script in `<head>` that runs before any styled content reaches the screen:
+The first is a pre-paint script in `<head>` that runs before any styled content reaches the screen:
[source,html]
+.grails-app/views/layouts/main.gsp
----
-<script>
- (function () {
- var saved = localStorage.getItem('theme');
- var prefers = window.matchMedia('(prefers-color-scheme: dark)').matches;
- if (saved === 'dark' || (!saved && prefers)) {
- document.documentElement.classList.add('dark');
- }
- })();
-</script>
+include::../snippets/grails-app/views/layouts/main.gsp[tag=themePreload,indent=0][]
----
It reads the user's saved choice (or the OS preference if the user has not chosen) and adds the `dark` class to `<html>` synchronously. Because it runs before the body renders there is no light-to-dark flash on reload.
@@ -20,20 +13,20 @@ It reads the user's saved choice (or the OS preference if the user has not chose
The second is the toggle button in the navbar plus a click handler at the end of the body that flips the class and writes the new value to `localStorage`:
[source,html]
+.grails-app/views/layouts/main.gsp
----
-<button id="theme-toggle" type="button" aria-label="Toggle dark mode"
- class="inline-flex h-9 w-9 items-center justify-center rounded-md border border-gray-300 bg-white text-gray-700 shadow-sm hover:bg-gray-50 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:hover:bg-gray-600">
- <span aria-hidden="true" class="dark:hidden">☼</span>
- <span aria-hidden="true" class="hidden dark:inline">☽</span>
-</button>
-
-<script>
- document.getElementById('theme-toggle').addEventListener('click', function () {
- var html = document.documentElement;
- html.classList.toggle('dark');
- localStorage.setItem('theme', html.classList.contains('dark') ? 'dark' : 'light');
- });
-</script>
+include::../snippets/grails-app/views/layouts/main.gsp[tag=themeToggleButton,indent=0][]
+----
+
+[source,html]
+.grails-app/views/layouts/main.gsp
+----
+include::../snippets/grails-app/views/layouts/main.gsp[tag=themeToggleScript,indent=0][]
----
Notice the icon swap: the sun glyph is hidden in dark mode (`dark:hidden`), the moon glyph is hidden in light mode (`hidden dark:inline`). The same `dark:`/non-`dark:` pairing pattern works for any element whose appearance depends on theme.
+
+Files touched in this chapter:
+
+* `src/main/css/input.css`
+* `grails-app/views/layouts/main.gsp`
diff --git a/guides/grails-tailwindcss/v8/guide/firstPaint.adoc b/guides/grails-tailwindcss/v8/guide/firstPaint.adoc
index 5ed6115a732..d57aa9f0fed 100644
--- a/guides/grails-tailwindcss/v8/guide/firstPaint.adoc
+++ b/guides/grails-tailwindcss/v8/guide/firstPaint.adoc
@@ -3,11 +3,13 @@ With the Tailwind pipeline in place, replace the layout markup. The starter ship
[source,html]
.grails-app/views/layouts/main.gsp
----
-include::../snippets/grails-app/views/layouts/main.gsp[]
+include::../snippets/grails-app/views/layouts/main.gsp[tag=layout][]
----
A few patterns are worth pointing at:
+* The `<asset:stylesheet src="application.css"/>` taglib resolves to the bundled, fingerprinted `application.css` URL at runtime. The manifest you edited in the previous chapter is what tells the pipeline to include the compiled Tailwind `app.css` in that bundle. The layout never references `app.css` directly.
+* The layout keeps the starter's `favicon.ico` link and `application.js` include. That means the page still uses the stock asset-pipeline conventions even though the CSS is now Tailwind-driven.
* The outer `<body>` carries `bg-gray-50 dark:bg-gray-900` so the colour scheme flips at the top level.
* The navbar uses `mx-auto max-w-7xl` plus a flex row, replacing the Bootstrap `navbar` component with a hand-rolled equivalent that is easier to customise.
* The footer link is the `text-blue-600 hover:underline dark:text-blue-400` triplet that you will repeat all over the app once you commit to the utility-first approach.
@@ -17,7 +19,12 @@ The welcome page itself follows the same recipe:
[source,html]
.grails-app/views/index.gsp
----
-include::../snippets/grails-app/views/index.gsp[]
+include::../snippets/grails-app/views/index.gsp[tag=index][]
----
Reload `http://localhost:8080` after `./gradlew bootRun` and you should see the Tailwind-styled cards and the dark-mode toggle in the top right.
+
+Files touched in this chapter:
+
+* `grails-app/views/layouts/main.gsp`
+* `grails-app/views/index.gsp`
diff --git a/guides/grails-tailwindcss/v8/guide/gettingStarted.adoc b/guides/grails-tailwindcss/v8/guide/gettingStarted.adoc
index be8ba3498cc..c3ebaa68444 100644
--- a/guides/grails-tailwindcss/v8/guide/gettingStarted.adoc
+++ b/guides/grails-tailwindcss/v8/guide/gettingStarted.adoc
@@ -1,5 +1,9 @@
In this guide you will wire link:https://tailwindcss.com[Tailwind CSS 4] into a fresh Apache Grails 8 application so every GSP view can be styled with utility classes, with class-based dark mode and a small `@apply` component layer for the patterns that repeat.
-The pattern works for any server-rendered Grails app: you keep GSPs and the asset pipeline, swap Bootstrap for Tailwind utilities, and let a Gradle-driven `npx @tailwindcss/cli` step regenerate the CSS on every build.
+The end state stays close to the Grails 8 `web` starter. GSPs still render through the standard layout tags, the asset pipeline still serves `application.css`, and Gradle owns the extra Tailwind compile step.
This guide targets Apache Grails 8.
+
+Files touched in this chapter:
+
+* None
diff --git a/guides/grails-tailwindcss/v8/guide/gradleWiring.adoc b/guides/grails-tailwindcss/v8/guide/gradleWiring.adoc
index 0e761f2a3a3..a176f75ea11 100644
--- a/guides/grails-tailwindcss/v8/guide/gradleWiring.adoc
+++ b/guides/grails-tailwindcss/v8/guide/gradleWiring.adoc
@@ -1,4 +1,4 @@
-A two-task block at the bottom of `build.gradle` is everything the build needs:
+A small block at the bottom of `build.gradle` is everything the build needs:
[source,groovy]
.build.gradle
@@ -6,14 +6,24 @@ A two-task block at the bottom of `build.gradle` is everything the build needs:
include::../snippets/build.gradle[tag=tailwind,indent=0]
----
-Two new `Exec` tasks are added:
+And the asset manifest becomes a one-line include of the generated Tailwind output:
-* `npmInstall` runs `npm install` and caches its result. The `outputs.dir('node_modules')` declaration plus the `inputs.file('package.json')` declaration mean Gradle reruns it only when `package.json` changes.
-* `tailwindBuild` calls `npx @tailwindcss/cli` with the same `-i` and `-o` paths used by the npm scripts. Its `inputs` cover `package.json`, the entry CSS, and the two directory trees we declared as `@source` paths in `input.css`. Its single output is the compiled `app.css`.
+[source,css]
+.grails-app/assets/stylesheets/application.css
+----
+include::../snippets/grails-app/assets/stylesheets/application.css[]
+----
+
+Two `Exec` tasks do the work:
+
+* `npmInstall` runs `npm install` and caches its result. The `node_modules/` output plus the `package.json` and `package-lock.json` inputs mean Gradle reruns it only when the Node dependency graph changes.
+* `tailwindBuild` calls `npx @tailwindcss/cli` with the same `-i` and `-o` paths used by the npm scripts. Its `inputs` cover the entry CSS and every directory that the `@source` directives scan.
+
+The two `tasks.named` lines hook `tailwindBuild` into the standard Grails build graph so plain `./gradlew bootRun`, `assemble`, or `bootJar` regenerates `app.css` before resources and Groovy sources compile. For iterative work without Gradle in the loop, run `npm run watch` in a second shell.
+
+The compiled `app.css` lands inside `grails-app/assets/stylesheets/`, which is the asset pipeline's source directory. The layout still asks for `application.css` via `<asset:stylesheet>`, but the manifest now pulls in the Tailwind output instead of the starter's Bootstrap CSS. `app.css` itself is regenerated on every build, so it is git-ignored.
-The two `tasks.named` lines hook `tailwindBuild` into the standard Grails build graph so plain `./gradlew bootRun` (or `assemble`, or `bootJar`) regenerates `app.css` on every cold start. For iterative work without Gradle in the loop, run `npm run watch` in a second shell.
+Files touched in this chapter:
-[NOTE]
-====
-Asset-pipeline manifest: replace the contents of `grails-app/assets/stylesheets/application.css` with `*= require app` so the manifest pulls in the Tailwind output instead of the Bootstrap CSS the starter shipped with. The `app.css` file itself is regenerated on every build, so it is git-ignored.
-====
+* `build.gradle`
+* `grails-app/assets/stylesheets/application.css`
diff --git a/guides/grails-tailwindcss/v8/guide/helpWithGrails.adoc b/guides/grails-tailwindcss/v8/guide/helpWithGrails.adoc
index e062f614b1a..ff50c056612 100644
--- a/guides/grails-tailwindcss/v8/guide/helpWithGrails.adoc
+++ b/guides/grails-tailwindcss/v8/guide/helpWithGrails.adoc
@@ -1 +1,5 @@
include::{commondir}/common-helpWithGrails.adoc[]
+
+Files touched in this chapter:
+
+* None
diff --git a/guides/grails-tailwindcss/v8/guide/inputCss.adoc b/guides/grails-tailwindcss/v8/guide/inputCss.adoc
index dd98cf6d6bd..4efaa242857 100644
--- a/guides/grails-tailwindcss/v8/guide/inputCss.adoc
+++ b/guides/grails-tailwindcss/v8/guide/inputCss.adoc
@@ -3,13 +3,16 @@ Create the Tailwind entry point at `src/main/css/input.css`:
[source,css]
.src/main/css/input.css
----
-include::../snippets/src/main/css/input.css[]
+include::../snippets/src/main/css/input.css[tag=input][]
----
-Three things in this file are worth highlighting:
+Four things in this file are worth highlighting:
* `@import "tailwindcss";` pulls in the framework. In v3 you wrote three separate `@tailwind base; @tailwind components; @tailwind utilities;` directives; v4 collapses all three into the single `@import`.
-* The `@source` directives tell the scanner exactly where to look. The starter the forge produces puts views under `grails-app/views`, controllers under `grails-app/controllers`, services under `grails-app/services`, and any helper code (tag libs, command objects, traits) under `src/main/groovy`. Adding a new directory tree later means another `@source` line; nothing else.
+* The `@source` directives tell the scanner exactly where to look. The starter the forge produces puts views under `grails-app/views`, controllers under `grails-app/controllers`, services under `grails-app/services`, tag libraries under `grails-app/taglib`, and helper code under `src/main/groovy`.
* The `@custom-variant dark (&:where(.dark, .dark *));` line redefines `dark:` so it applies whenever an ancestor (typically `<html>`) carries the `dark` class. Without this override the v4 default uses `prefers-color-scheme`, which honours the OS theme but does not let the user override it.
+* The `@layer components` block at the bottom defines `.btn-primary`, `.card`, and `.nav-link`. These are referenced from `main.gsp` and `index.gsp` in later chapters.
-The `@layer components` block at the bottom defines `.btn-primary`, `.card`, and `.nav-link` - these are referenced from `main.gsp` and `index.gsp` in later chapters.
+Files touched in this chapter:
+
+* `src/main/css/input.css`
diff --git a/guides/grails-tailwindcss/v8/guide/installTailwind.adoc b/guides/grails-tailwindcss/v8/guide/installTailwind.adoc
index d68edbf6a9d..1e101ad7aa8 100644
--- a/guides/grails-tailwindcss/v8/guide/installTailwind.adoc
+++ b/guides/grails-tailwindcss/v8/guide/installTailwind.adoc
@@ -6,11 +6,16 @@ Tailwind 4 ships as the npm package `tailwindcss`, with a separate `@tailwindcss
include::../snippets/package.json[]
----
-Then run `npm install` once to populate `node_modules/`:
+Then run `npm install` once to populate `node_modules/` and create `package-lock.json`:
[source,bash]
----
npm install
----
-The `tailwindBuild` Gradle task we will add in a later chapter calls `npm install` automatically the first time it runs (and skips it on subsequent invocations as long as `package.json` has not changed), so committing the lock file is enough; no hand-rolled bootstrap step is needed in CI.
+The `build` and `watch` scripts call the `tailwindcss` binary that `@tailwindcss/cli` exposes, while the Gradle task you will add in a later chapter runs the same compiler through `npx @tailwindcss/cli`. That keeps the npm side and Gradle side aligned.
+
+Files touched in this chapter:
+
+* `package.json`
+* `package-lock.json`
diff --git a/guides/grails-tailwindcss/v8/guide/requirements.adoc b/guides/grails-tailwindcss/v8/guide/requirements.adoc
index 75784f0caba..130215d7860 100644
--- a/guides/grails-tailwindcss/v8/guide/requirements.adoc
+++ b/guides/grails-tailwindcss/v8/guide/requirements.adoc
@@ -1,6 +1,11 @@
To complete this guide you will need:
* JDK 21
-* link:https://nodejs.org[Node.js] 20+ and a recent npm (the Tailwind 4 CLI is published as `@tailwindcss/cli` on npm; no other build tooling is required)
+* link:https://nodejs.org[Node.js] 20+
+* npm 10+
* A Bash, PowerShell, or `cmd` shell capable of running the included Gradle wrapper (`./gradlew` or `gradlew.bat`)
* About 30 minutes
+
+Files touched in this chapter:
+
+* None
diff --git a/guides/grails-tailwindcss/v8/guide/whatYouWillBuild.adoc b/guides/grails-tailwindcss/v8/guide/whatYouWillBuild.adoc
index df26b190805..792068a8911 100644
--- a/guides/grails-tailwindcss/v8/guide/whatYouWillBuild.adoc
+++ b/guides/grails-tailwindcss/v8/guide/whatYouWillBuild.adoc
@@ -1,8 +1,12 @@
By the end of the guide your application will:
* Render `grails-app/views/layouts/main.gsp` and `grails-app/views/index.gsp` with Tailwind 4 utility classes instead of the default Bootstrap 5 classes the starter ships with.
-* Compile `src/main/css/input.css` (the Tailwind entry point) into `grails-app/assets/stylesheets/app.css` via a `tailwindBuild` Gradle task that runs before `processResources` and `compileGroovy`.
+* Compile `src/main/css/input.css` (the Tailwind entry point) into `grails-app/assets/stylesheets/app.css` via a `tailwindBuild` Gradle task that runs before `processResources` and `compileGroovy`. The asset pipeline then bundles, fingerprints, and gzips that file into the `application.css` bundle the layout serves.
* Toggle dark mode at runtime by adding the `dark` class on `<html>`, persisting the choice to `localStorage` so reloads do not flash light-to-dark.
-* Expose two reusable component classes (`btn-primary`, `card`) defined in the `@layer components` block of `input.css` and used directly from GSP markup.
+* Expose three reusable component classes (`btn-primary`, `card`, `nav-link`) defined in the `@layer components` block of `input.css` and used directly from GSP markup.
The result is a small but production-shaped baseline you can copy into any Grails 8 app that still wants the GSP view layer but a modern utility-first CSS pipeline.
+
+Files touched in this chapter:
+
+* None
diff --git a/guides/grails-tailwindcss/v8/snippets/build.gradle b/guides/grails-tailwindcss/v8/snippets/build.gradle
index 0955ea3558b..e6077b52f01 100644
--- a/guides/grails-tailwindcss/v8/snippets/build.gradle
+++ b/guides/grails-tailwindcss/v8/snippets/build.gradle
@@ -110,6 +110,9 @@ dependencies {
runtimeOnly "com.h2database:h2"
runtimeOnly "com.zaxxer:HikariCP"
runtimeOnly "org.fusesource.jansi:jansi"
+ testAndDevelopmentOnly "org.webjars.npm:bootstrap"
+ testAndDevelopmentOnly "org.webjars.npm:bootstrap-icons"
+ testAndDevelopmentOnly "org.webjars.npm:jquery"
integrationTestImplementation testFixtures("org.apache.grails:grails-geb")
testImplementation "org.apache.grails:grails-testing-support-datamapping"
testImplementation "org.apache.grails:grails-testing-support-web"
@@ -127,22 +130,24 @@ tasks.withType(Test).configureEach {
def npxCmd = org.gradle.internal.os.OperatingSystem.current().isWindows() ? 'npx.cmd' : 'npx'
def npmCmd = org.gradle.internal.os.OperatingSystem.current().isWindows() ? 'npm.cmd' : 'npm'
-tasks.register('npmInstall', Exec) {
- description = 'Install npm dependencies (Tailwind CSS 4 CLI)'
+def npmInstall = tasks.register('npmInstall', Exec) {
+ description = 'Install npm dependencies for Tailwind CSS 4'
group = 'tailwind'
- inputs.file('package.json')
+ inputs.files('package.json', 'package-lock.json')
outputs.dir('node_modules')
commandLine npmCmd, 'install', '--no-fund', '--no-audit'
}
-tasks.register('tailwindBuild', Exec) {
- description = 'Compile src/main/css/input.css into grails-app/assets/stylesheets/app.css via the Tailwind 4 CLI'
+def tailwindBuild = tasks.register('tailwindBuild', Exec) {
+ description = 'Compile src/main/css/input.css into grails-app/assets/stylesheets/app.css'
group = 'tailwind'
- dependsOn 'npmInstall'
- inputs.file('package.json')
- inputs.file('src/main/css/input.css')
+ dependsOn npmInstall
+ inputs.files('package.json', 'package-lock.json', 'src/main/css/input.css')
inputs.dir('grails-app/views')
inputs.dir('grails-app/controllers')
+ inputs.dir('grails-app/services')
+ inputs.dir('grails-app/taglib')
+ inputs.dir('src/main/groovy')
outputs.file('grails-app/assets/stylesheets/app.css')
commandLine npxCmd, '@tailwindcss/cli',
'-i', 'src/main/css/input.css',
@@ -150,7 +155,7 @@ tasks.register('tailwindBuild', Exec) {
'--minify'
}
-tasks.named('processResources').configure { dependsOn 'tailwindBuild' }
-tasks.named('compileGroovy').configure { dependsOn 'tailwindBuild' }
+tasks.named('processResources').configure { dependsOn tailwindBuild }
+tasks.named('compileGroovy').configure { dependsOn tailwindBuild }
// end::tailwind[]
diff --git a/guides/grails-tailwindcss/v8/snippets/grails-app/assets/stylesheets/application.css b/guides/grails-tailwindcss/v8/snippets/grails-app/assets/stylesheets/application.css
index 62715727608..c0622428f7a 100644
--- a/guides/grails-tailwindcss/v8/snippets/grails-app/assets/stylesheets/application.css
+++ b/guides/grails-tailwindcss/v8/snippets/grails-app/assets/stylesheets/application.css
@@ -1,9 +1,6 @@
/*
- * Asset-pipeline manifest. The `app.css` file is the Tailwind 4 CLI
- * output (regenerated by the `tailwindBuild` Gradle task on every build);
- * it is git-ignored. `require_self` keeps any rules added below this
- * comment block at the bottom of the bundle.
+ * Asset-pipeline manifest. Pull the Tailwind CLI output into the canonical
+ * application.css bundle the layout already serves.
*
*= require app
- *= require_self
*/
diff --git a/guides/grails-tailwindcss/v8/snippets/grails-app/views/index.gsp b/guides/grails-tailwindcss/v8/snippets/grails-app/views/index.gsp
index a7bcc27834b..770b4876f12 100644
--- a/guides/grails-tailwindcss/v8/snippets/grails-app/views/index.gsp
+++ b/guides/grails-tailwindcss/v8/snippets/grails-app/views/index.gsp
@@ -1,6 +1,8 @@
+<%-- tag::index[] --%>
<%@ page import="grails.util.Environment"%>
<%@ page import="org.springframework.boot.SpringBootVersion"%>
<%@ page import="org.springframework.core.SpringVersion"%>
+<g:set var="pluginManager" bean="pluginManager"/>
<g:set var="numControllers" value="${grailsApplication.controllerClasses.size()}"/>
<g:set var="numDomains" value="${grailsApplication.domainClasses.size()}"/>
<g:set var="numPlugins" value="${pluginManager.allPlugins.size()}"/>
@@ -29,7 +31,7 @@
target="_blank" rel="noopener">
Read the guide
</a>
- <a href="https://github.com/grails-guides/grails-tailwindcss"
+ <a href="https://github.com/grails-guides/grails-tailwindcss/tree/grails8/complete"
class="inline-flex items-center justify-center rounded-md border border-gray-300 bg-white px-4 py-2 text-sm font-medium text-gray-700 shadow-sm hover:bg-gray-50 dark:border-gray-600 dark:bg-gray-800 dark:text-gray-200 dark:hover:bg-gray-700"
target="_blank" rel="noopener">
View source
@@ -121,3 +123,4 @@
</main>
</body>
</html>
+<%-- end::index[] --%>
diff --git a/guides/grails-tailwindcss/v8/snippets/grails-app/views/layouts/main.gsp b/guides/grails-tailwindcss/v8/snippets/grails-app/views/layouts/main.gsp
index cd779aa1c04..075ed9ed693 100644
--- a/guides/grails-tailwindcss/v8/snippets/grails-app/views/layouts/main.gsp
+++ b/guides/grails-tailwindcss/v8/snippets/grails-app/views/layouts/main.gsp
@@ -1,3 +1,4 @@
+<%-- tag::layout[] --%>
<!doctype html>
<html lang="en" class="h-full">
<head>
@@ -7,6 +8,7 @@
<asset:link rel="icon" href="favicon.ico" type="image/x-ico"/>
<asset:stylesheet src="application.css"/>
+ <%-- tag::themePreload[] --%>
<%-- Pre-paint dark-mode resolution: read the user's saved choice (or
system preference) and apply the `dark` class on <html> BEFORE
the body renders. This avoids the light-to-dark flash on reload. --%>
@@ -19,6 +21,7 @@
}
})();
</script>
+ <%-- end::themePreload[] --%>
<g:layoutHead/>
</head>
@@ -33,6 +36,7 @@
<div class="flex items-center gap-6">
<a href="https://grails.apache.org/docs/" class="nav-link" target="_blank" rel="noopener">Docs</a>
<a href="https://grails.apache.org/community.html" class="nav-link" target="_blank" rel="noopener">Community</a>
+ <%-- tag::themeToggleButton[] --%>
<button id="theme-toggle"
type="button"
aria-label="Toggle dark mode"
@@ -40,12 +44,12 @@
<span aria-hidden="true" class="dark:hidden">☼</span>
<span aria-hidden="true" class="hidden dark:inline">☽</span>
</button>
+ <%-- end::themeToggleButton[] --%>
</div>
</div>
</nav>
<div class="mx-auto max-w-7xl px-4 py-6 sm:px-6 lg:px-8">
- <g:flashMessages />
<g:layoutBody/>
</div>
@@ -61,6 +65,7 @@
</div>
</footer>
+<%-- tag::themeToggleScript[] --%>
<script>
document.getElementById('theme-toggle').addEventListener('click', function () {
var html = document.documentElement;
@@ -68,5 +73,8 @@
localStorage.setItem('theme', html.classList.contains('dark') ? 'dark' : 'light');
});
</script>
+<%-- end::themeToggleScript[] --%>
+<asset:javascript src="application.js"/>
</body>
</html>
+<%-- end::layout[] --%>
diff --git a/guides/grails-tailwindcss/v8/snippets/package.json b/guides/grails-tailwindcss/v8/snippets/package.json
index 63048466d65..1d95852909a 100644
--- a/guides/grails-tailwindcss/v8/snippets/package.json
+++ b/guides/grails-tailwindcss/v8/snippets/package.json
@@ -3,6 +3,10 @@
"version": "1.0.0",
"private": true,
"description": "Tailwind CSS 4 build wiring for the grails-tailwindcss/v8 sample app.",
+ "engines": {
+ "node": ">=20",
+ "npm": ">=10"
+ },
"scripts": {
"build": "tailwindcss -i src/main/css/input.css -o grails-app/assets/stylesheets/app.css --minify",
"watch": "tailwindcss -i src/main/css/input.css -o grails-app/assets/stylesheets/app.css --watch"
diff --git a/guides/grails-tailwindcss/v8/snippets/src/main/css/input.css b/guides/grails-tailwindcss/v8/snippets/src/main/css/input.css
index ad4edaa2f72..82609e8fe97 100644
--- a/guides/grails-tailwindcss/v8/snippets/src/main/css/input.css
+++ b/guides/grails-tailwindcss/v8/snippets/src/main/css/input.css
@@ -1,3 +1,4 @@
+/* tag::input[] */
/*
* Tailwind CSS 4 entry point.
*
@@ -9,6 +10,7 @@
@import "tailwindcss";
+/* tag::sources[] */
/*
* Where to scan for class names. Tailwind 4 auto-detects most file types,
* but does not know about .gsp out of the box and is conservative about
@@ -18,15 +20,20 @@
@source "../../../grails-app/views";
@source "../../../grails-app/controllers";
@source "../../../grails-app/services";
+@source "../../../grails-app/taglib";
@source "../../../src/main/groovy";
+/* end::sources[] */
+/* tag::darkVariant[] */
/*
* Dark mode via a class on <html>. Toggled at runtime in main.gsp.
* The default Tailwind 4 dark variant follows prefers-color-scheme; this
* override lets a user persist their choice across sessions.
*/
@custom-variant dark (&:where(.dark, .dark *));
+/* end::darkVariant[] */
+/* tag::components[] */
/*
* A small component layer: classes you can spell once and reuse across
* GSPs without the full utility chain. Good fit for buttons, cards, and
@@ -44,3 +51,5 @@
@apply text-sm font-medium text-gray-600 hover:text-gray-900 dark:text-gray-300 dark:hover:text-white;
}
}
+/* end::components[] */
+/* end::input[] */