Support Tailwind CSS v4 in project builds

Detect the Tailwind major version from the project's package.json and
branch the build pipeline accordingly. v3 projects keep loading the Play
CDN runtime and a JS tailwind.config via the existing synthetic entry.
v4 projects instead pull in @tailwindcss/browser@4, which auto-scans the
document and reads theme config from CSS (@theme / @import 'tailwindcss')
so no config shim is required.
This commit is contained in:
Alex Gleason committed 2026-04-26 19:56:25 -05:00
1 parent b2c5b1d81e
commit 1fd3d23d4f
3 files changed
+69 -8

No files matched your search

+2 -2
View File
@@ -1,12 +1,12 @@
{
"name": "shakespeare.diy",
"version": "9.11.4",
"version": "9.12.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "shakespeare.diy",
"version": "9.11.4",
"version": "9.12.0",
"license": "AGPL-3.0-or-later",
"dependencies": {
"@capacitor/app": "^8.1.0",
+46 -5
View File
@@ -52,6 +52,37 @@ export interface BuildContext {
tsconfig?: TsConfig;
}
/** Tailwind CSS variant used by a project (or `null` if Tailwind is not enabled). */
export type TailwindVariant = "v3" | "v4" | null;
/**
* Detect which major version of Tailwind CSS a project uses by inspecting the
* `tailwindcss` entry in package.json (dependencies, devDependencies, or
* peerDependencies). Returns `null` when no Tailwind dependency is declared.
*
* The version string is matched loosely: any leading non-digit characters
* (such as `^`, `~`, `>=`, or `v`) are stripped before reading the major
* number. Ranges like `>=3.0.0 <5.0.0`, git URLs, or other non-standard
* specifiers fall through to `null`, which callers should treat as "skip
* Tailwind injection entirely".
*/
export function detectTailwindVariant(packageJson: PackageJson): TailwindVariant {
const spec =
packageJson.dependencies?.tailwindcss ??
packageJson.devDependencies?.tailwindcss ??
packageJson.peerDependencies?.tailwindcss;
if (!spec) return null;
const match = spec.match(/(\d+)/);
if (!match) return null;
const major = Number(match[1]);
if (major >= 4) return "v4";
if (major === 3) return "v3";
return null;
}
/**
* Read package.json, package-lock.json/yarn.lock, and tsconfig.json from project
* Exported for use by deployment adapters that need to bundle workers
@@ -193,12 +224,22 @@ async function bundle(
}
}
// Enable Tailwind CSS if a config file is present
for (const path of ["tailwind.config.ts", "tailwind.config.js"]) {
if (await fileExists(fs, join(projectPath, path))) {
entryPoints.push(`shakespeare:${path}`);
break;
// Enable Tailwind CSS based on the version declared in package.json.
// v3 needs a synthetic entry that loads the Play CDN runtime and assigns
// the project's tailwind.config.{js,ts} to `window.tailwind.config`.
// v4 has no JS config (theming is CSS-based via `@theme`) and the
// `@tailwindcss/browser@4` runtime auto-scans the document, so the shim
// just needs to side-effect-import the runtime.
const tailwindVariant = detectTailwindVariant(context.packageJson);
if (tailwindVariant === "v3") {
for (const path of ["tailwind.config.ts", "tailwind.config.js"]) {
if (await fileExists(fs, join(projectPath, path))) {
entryPoints.push(`shakespeare:${path}`);
break;
}
}
} else if (tailwindVariant === "v4") {
entryPoints.push("shakespeare:tailwindcss@4");
}
const collectedWorkers: WorkerMatch[] = [];
+21 -1
View File
@@ -14,7 +14,9 @@ export function shakespearePlugin(options: ShakespearePluginOptions): Plugin {
name: "shakespeare",
setup(build) {
// Handle Tailwind CSS configuration files
// Handle Tailwind CSS configuration files (v3).
// The synthetic entry loads the v3 Play CDN runtime and assigns the
// project's tailwind.config.{js,ts} to the global `tailwind.config`.
build.onResolve({ filter: /^shakespeare:tailwind\.config\.(js|ts)$/ }, (args) => {
return {
path: args.path,
@@ -34,6 +36,24 @@ export function shakespearePlugin(options: ShakespearePluginOptions): Plugin {
};
});
// Handle Tailwind CSS v4 runtime.
// v4 has no JS config (theming is done in CSS via `@theme`) and the
// `@tailwindcss/browser@4` runtime auto-detects `<style type="text/tailwindcss">`
// blocks in the document, so the shim just side-effect-imports the runtime.
build.onResolve({ filter: /^shakespeare:tailwindcss@4$/ }, (args) => {
return {
path: args.path,
namespace: "shakespeare-tailwind-v4",
};
});
build.onLoad({ filter: /.*/, namespace: "shakespeare-tailwind-v4" }, () => {
return {
contents: `import "${options.esmUrl}/@tailwindcss/browser@4";`,
loader: "js",
};
});
// Handle "sonner" imports with a dummy API
build.onResolve({ filter: /^sonner$/ }, (args) => {
return {