@@ -51,14 +51,7 @@ const ALIASES = new Map([
51
51
52
52
const STUBS = {
53
53
ASTRO_CONFIG : `import { defineConfig } from 'astro/config';\n// https://astro.build/config\nexport default defineConfig({});` ,
54
- TAILWIND_CONFIG : `/** @type {import('tailwindcss').Config} */
55
- export default {
56
- content: ['./src/**/*.{astro,html,js,jsx,md,mdx,svelte,ts,tsx,vue}'],
57
- theme: {
58
- extend: {},
59
- },
60
- plugins: [],
61
- }\n` ,
54
+ TAILWIND_GLOBAL_CSS : `@import "tailwindcss";` ,
62
55
SVELTE_CONFIG : `\
63
56
import { vitePreprocess } from '@astrojs/svelte';
64
57
@@ -154,23 +147,28 @@ export async function add(names: string[], { flags }: AddOptions) {
154
147
switch ( installResult ) {
155
148
case UpdateResult . updated : {
156
149
if ( integrations . find ( ( integration ) => integration . id === 'tailwind' ) ) {
157
- await setupIntegrationConfig ( {
158
- root,
159
- logger,
150
+ const dir = new URL ( './styles/' , new URL ( userConfig . srcDir ?? './src/' , root ) ) ;
151
+ const styles = new URL ( './global.css' , dir ) ;
152
+ if ( ! existsSync ( styles ) ) {
153
+ logger . info (
154
+ 'SKIP_FORMAT' ,
155
+ `\n ${ magenta ( `Astro will scaffold ${ green ( './src/styles/global.css' ) } .` ) } \n` ,
156
+ ) ;
160
157
161
- flags,
162
- integrationName : 'Tailwind' ,
163
- possibleConfigFiles : [
164
- './tailwind.config.cjs' ,
165
- './tailwind.config.mjs' ,
166
- './tailwind.config.ts' ,
167
- './tailwind.config.mts' ,
168
- './tailwind.config.cts' ,
169
- './tailwind.config.js' ,
170
- ] ,
171
- defaultConfigFile : './tailwind.config.mjs' ,
172
- defaultConfigContent : STUBS . TAILWIND_CONFIG ,
173
- } ) ;
158
+ if ( await askToContinue ( { flags } ) ) {
159
+ if ( ! existsSync ( dir ) ) {
160
+ await fs . mkdir ( dir ) ;
161
+ }
162
+ await fs . writeFile ( styles , STUBS . TAILWIND_GLOBAL_CSS , 'utf-8' ) ;
163
+ } else {
164
+ logger . info (
165
+ 'SKIP_FORMAT' ,
166
+ `\n @astrojs/tailwind requires additional configuration. Please refer to https://docs.astro.build/en/guides/integrations-guide/tailwind/` ,
167
+ ) ;
168
+ }
169
+ } else {
170
+ logger . debug ( 'add' , `Using existing tailwind configuration` ) ;
171
+ }
174
172
}
175
173
if ( integrations . find ( ( integration ) => integration . id === 'svelte' ) ) {
176
174
await setupIntegrationConfig ( {
@@ -290,6 +288,8 @@ export async function add(names: string[], { flags }: AddOptions) {
290
288
) } `,
291
289
) ;
292
290
}
291
+ } else if ( integration . id === 'tailwind' ) {
292
+ addVitePlugin ( mod , 'tailwindcss' , '@tailwindcss/vite' ) ;
293
293
} else {
294
294
addIntegration ( mod , integration ) ;
295
295
}
@@ -358,6 +358,7 @@ export async function add(names: string[], { flags }: AddOptions) {
358
358
} to your project:\n${ list } `,
359
359
) ,
360
360
) ;
361
+ logger . info ( 'SKIP_FORMAT' , msg . success ( "Import './src/styles/global.css' in a layout" ) ) ;
361
362
}
362
363
}
363
364
@@ -456,6 +457,31 @@ function addIntegration(mod: ProxifiedModule<any>, integration: IntegrationInfo)
456
457
}
457
458
}
458
459
460
+ function addVitePlugin ( mod : ProxifiedModule < any > , pluginId : string , packageName : string ) {
461
+ const config = getDefaultExportOptions ( mod ) ;
462
+
463
+ if ( ! mod . imports . $items . some ( ( imp ) => imp . local === pluginId ) ) {
464
+ mod . imports . $append ( {
465
+ imported : 'default' ,
466
+ local : pluginId ,
467
+ from : packageName ,
468
+ } ) ;
469
+ }
470
+
471
+ config . vite ??= { } ;
472
+ config . vite . plugins ??= [ ] ;
473
+ if (
474
+ ! config . vite . plugins . $ast . elements . some (
475
+ ( el : ASTNode ) =>
476
+ el . type === 'CallExpression' &&
477
+ el . callee . type === 'Identifier' &&
478
+ el . callee . name === pluginId ,
479
+ )
480
+ ) {
481
+ config . vite . plugins . push ( builders . functionCall ( pluginId ) ) ;
482
+ }
483
+ }
484
+
459
485
export function setAdapter (
460
486
mod : ProxifiedModule < any > ,
461
487
adapter : IntegrationInfo ,
@@ -796,6 +822,13 @@ async function validateIntegrations(integrations: string[]): Promise<Integration
796
822
) ;
797
823
}
798
824
825
+ if ( integration === 'tailwind' ) {
826
+ dependencies = [
827
+ [ '@tailwindcss/vite' , '^4.0.0' ] ,
828
+ [ 'tailwindcss' , '^4.0.0' ] ,
829
+ ] ;
830
+ }
831
+
799
832
return { id : integration , packageName, dependencies, type : integrationType } ;
800
833
} ) ,
801
834
) ;
0 commit comments