1
- // @ts -check
2
1
import * as fs from 'node:fs/promises'
3
2
import * as path from 'node:path'
4
3
import { pathToFileURL } from 'node:url'
5
4
import { createJiti , type Jiti } from 'jiti'
5
+ import * as v4 from 'tailwindcss-v4'
6
6
import { resolveCssFrom , resolveJsFrom } from '../resolve'
7
7
import type { UnifiedApi } from '../types'
8
+ import { assets } from './assets'
8
9
9
10
interface DesignSystem {
10
11
getClassOrder ( classList : string [ ] ) : [ string , bigint | null ] [ ]
@@ -40,11 +41,10 @@ interface ApiV4 {
40
41
41
42
export async function loadV4 ( mod : ApiV4 | null , stylesheet : string | null ) : Promise < UnifiedApi > {
42
43
// This is not Tailwind v4
44
+ let isFallback = false
43
45
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 any
47
+ isFallback = true
48
48
}
49
49
50
50
// 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
63
63
} else {
64
64
importBasePath = process . cwd ( )
65
65
stylesheet = path . join ( importBasePath , 'fake.css' )
66
-
67
- // TODO: bundled theme.css file?
68
- css = ''
66
+ css = assets [ 'tailwindcss/theme.css' ]
69
67
}
70
68
71
69
// 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
90
88
} ) ,
91
89
92
90
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
+ }
94
102
95
- return {
96
- base : path . dirname ( resolved ) ,
97
- content : await fs . readFile ( resolved , 'utf-8' ) ,
103
+ throw err
98
104
}
99
105
} ,
100
106
0 commit comments