@@ -19,6 +19,7 @@ type ProjectInfo = {
1919 isTsx : boolean
2020 tailwindConfigFile : string | null
2121 tailwindCssFile : string | null
22+ tailwindVersion : "v3" | "v4" | null
2223 aliasPrefix : string | null
2324}
2425
@@ -43,6 +44,7 @@ export async function getProjectInfo(cwd: string): Promise<ProjectInfo | null> {
4344 isTsx ,
4445 tailwindConfigFile ,
4546 tailwindCssFile ,
47+ tailwindVersion ,
4648 aliasPrefix ,
4749 packageJson ,
4850 ] = await Promise . all ( [
@@ -55,6 +57,7 @@ export async function getProjectInfo(cwd: string): Promise<ProjectInfo | null> {
5557 isTypeScriptProject ( cwd ) ,
5658 getTailwindConfigFile ( cwd ) ,
5759 getTailwindCssFile ( cwd ) ,
60+ getTailwindVersion ( cwd ) ,
5861 getTsConfigAliasPrefix ( cwd ) ,
5962 getPackageInfo ( cwd , false ) ,
6063 ] )
@@ -70,6 +73,7 @@ export async function getProjectInfo(cwd: string): Promise<ProjectInfo | null> {
7073 isTsx,
7174 tailwindConfigFile,
7275 tailwindCssFile,
76+ tailwindVersion,
7377 aliasPrefix,
7478 }
7579
@@ -121,21 +125,50 @@ export async function getProjectInfo(cwd: string): Promise<ProjectInfo | null> {
121125 return type
122126}
123127
128+ export async function getTailwindVersion (
129+ cwd : string
130+ ) : Promise < ProjectInfo [ "tailwindVersion" ] > {
131+ const packageInfo = getPackageInfo ( cwd )
132+
133+ if (
134+ ! packageInfo ?. dependencies ?. tailwindcss &&
135+ ! packageInfo ?. devDependencies ?. tailwindcss
136+ ) {
137+ return null
138+ }
139+
140+ if (
141+ / ^ (?: \^ | ~ ) ? 3 (?: \. \d + ) * (?: - .* ) ? $ / . test (
142+ packageInfo ?. dependencies ?. tailwindcss ||
143+ packageInfo ?. devDependencies ?. tailwindcss ||
144+ ""
145+ )
146+ ) {
147+ return "v3"
148+ }
149+
150+ return "v4"
151+ }
152+
124153export async function getTailwindCssFile ( cwd : string ) {
125- const files = await fg . glob ( [ "**/*.css" , "**/*.scss" ] , {
126- cwd,
127- deep : 5 ,
128- ignore : PROJECT_SHARED_IGNORE ,
129- } )
154+ const [ files , tailwindVersion ] = await Promise . all ( [
155+ fg . glob ( [ "**/*.css" , "**/*.scss" ] , {
156+ cwd,
157+ deep : 5 ,
158+ ignore : PROJECT_SHARED_IGNORE ,
159+ } ) ,
160+ getTailwindVersion ( cwd ) ,
161+ ] )
130162
131163 if ( ! files . length ) {
132164 return null
133165 }
134166
167+ const needle =
168+ tailwindVersion === "v4" ? `@import "tailwindcss"` : "@tailwind base"
135169 for ( const file of files ) {
136170 const contents = await fs . readFile ( path . resolve ( cwd , file ) , "utf8" )
137- // Assume that if the file contains `@tailwind base` it's the main css file.
138- if ( contents . includes ( "@tailwind base" ) ) {
171+ if ( contents . includes ( needle ) ) {
139172 return file
140173 }
141174 }
@@ -237,8 +270,8 @@ export async function getProjectConfig(
237270
238271 if (
239272 ! projectInfo ||
240- ! projectInfo . tailwindConfigFile ||
241- ! projectInfo . tailwindCssFile
273+ ! projectInfo . tailwindCssFile ||
274+ ( projectInfo . tailwindVersion === "v3" && ! projectInfo . tailwindConfigFile )
242275 ) {
243276 return null
244277 }
@@ -249,7 +282,7 @@ export async function getProjectConfig(
249282 tsx : projectInfo . isTsx ,
250283 style : "new-york" ,
251284 tailwind : {
252- config : projectInfo . tailwindConfigFile ,
285+ config : projectInfo . tailwindConfigFile ?? "" ,
253286 baseColor : "zinc" ,
254287 css : projectInfo . tailwindCssFile ,
255288 cssVariables : true ,
0 commit comments