Skip to content

Commit 29f54ce

Browse files
committed
Add fallback for v4
1 parent fa61eae commit 29f54ce

File tree

2 files changed

+41
-12
lines changed

2 files changed

+41
-12
lines changed

src/versions/assets.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// @ts-ignore
2+
import index from 'tailwindcss-v4/index.css'
3+
// @ts-ignore
4+
import preflight from 'tailwindcss-v4/preflight.css'
5+
// @ts-ignore
6+
import theme from 'tailwindcss-v4/theme.css'
7+
// @ts-ignore
8+
import utilities from 'tailwindcss-v4/utilities.css'
9+
10+
export const assets: Record<string, string> = {
11+
tailwindcss: index,
12+
'tailwindcss/index': index,
13+
'tailwindcss/index.css': index,
14+
15+
'tailwindcss/preflight': preflight,
16+
'tailwindcss/preflight.css': preflight,
17+
18+
'tailwindcss/theme': theme,
19+
'tailwindcss/theme.css': theme,
20+
21+
'tailwindcss/utilities': utilities,
22+
'tailwindcss/utilities.css': utilities,
23+
}

src/versions/v4.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
// @ts-check
21
import * as fs from 'node:fs/promises'
32
import * as path from 'node:path'
43
import { pathToFileURL } from 'node:url'
54
import { createJiti, type Jiti } from 'jiti'
5+
import * as v4 from 'tailwindcss-v4'
66
import { resolveCssFrom, resolveJsFrom } from '../resolve'
77
import type { UnifiedApi } from '../types'
8+
import { assets } from './assets'
89

910
interface DesignSystem {
1011
getClassOrder(classList: string[]): [string, bigint | null][]
@@ -40,11 +41,10 @@ interface ApiV4 {
4041

4142
export async function loadV4(mod: ApiV4 | null, stylesheet: string | null): Promise<UnifiedApi> {
4243
// This is not Tailwind v4
44+
let isFallback = false
4345
if (!mod || !mod.__unstable__loadDesignSystem) {
44-
throw new Error('Unable to load Tailwind CSS v4: Your installation of Tailwind CSS is not v4')
45-
46-
// TODO
47-
// mod = (await import('tailwindcss-v4')) as ApiV4
46+
mod = v4 as ApiV4
47+
isFallback = true
4848
}
4949

5050
// Create a Jiti instance that can be used to load plugins and config files
@@ -63,9 +63,7 @@ export async function loadV4(mod: ApiV4 | null, stylesheet: string | null): Prom
6363
} else {
6464
importBasePath = process.cwd()
6565
stylesheet = path.join(importBasePath, 'fake.css')
66-
67-
// TODO: bundled theme.css file?
68-
css = ''
66+
css = assets['tailwindcss/theme.css']
6967
}
7068

7169
// Load the design system and set up a compatible context object that is
@@ -90,11 +88,19 @@ export async function loadV4(mod: ApiV4 | null, stylesheet: string | null): Prom
9088
}),
9189

9290
loadStylesheet: async (id: string, base: string) => {
93-
let resolved = resolveCssFrom(base, id)
91+
try {
92+
let resolved = resolveCssFrom(base, id)
93+
94+
return {
95+
base: path.dirname(resolved),
96+
content: await fs.readFile(resolved, 'utf-8'),
97+
}
98+
} catch (err) {
99+
if (isFallback && id in assets) {
100+
return { base, content: assets[id] }
101+
}
94102

95-
return {
96-
base: path.dirname(resolved),
97-
content: await fs.readFile(resolved, 'utf-8'),
103+
throw err
98104
}
99105
},
100106

0 commit comments

Comments
 (0)