1
+ import { fileURLToPath } from 'node:url' ;
1
2
import type { AstroIntegration } from 'astro' ;
2
3
import autoprefixerPlugin from 'autoprefixer' ;
3
4
import tailwindPlugin from 'tailwindcss' ;
@@ -23,15 +24,22 @@ async function getPostCssConfig(
23
24
24
25
async function getViteConfiguration (
25
26
tailwindConfigPath : string | undefined ,
26
- viteConfig : UserConfig
27
+ nesting : boolean ,
28
+ root : string ,
29
+ postcssInlineOptions : CSSOptions [ 'postcss' ]
27
30
) : Promise < Partial < UserConfig > > {
28
31
// We need to manually load postcss config files because when inlining the tailwind and autoprefixer plugins,
29
32
// that causes vite to ignore postcss config files
30
- const postcssConfigResult = await getPostCssConfig ( viteConfig . root , viteConfig . css ?. postcss ) ;
33
+ const postcssConfigResult = await getPostCssConfig ( root , postcssInlineOptions ) ;
31
34
32
35
const postcssOptions = postcssConfigResult ?. options ?? { } ;
33
36
const postcssPlugins = postcssConfigResult ?. plugins ?. slice ( ) ?? [ ] ;
34
37
38
+ if ( nesting ) {
39
+ const tailwindcssNestingPlugin = ( await import ( 'tailwindcss/nesting/index.js' ) ) . default ;
40
+ postcssPlugins . push ( tailwindcssNestingPlugin ( ) ) ;
41
+ }
42
+
35
43
postcssPlugins . push ( tailwindPlugin ( tailwindConfigPath ) ) ;
36
44
postcssPlugins . push ( autoprefixerPlugin ( ) ) ;
37
45
@@ -59,18 +67,30 @@ type TailwindOptions = {
59
67
* @default true
60
68
*/
61
69
applyBaseStyles ?: boolean ;
70
+ /**
71
+ * Add CSS nesting support using `tailwindcss/nesting`. See {@link https://tailwindcss.com/docs/using-with-preprocessors#nesting Tailwind's docs}
72
+ * for how this works with `postcss-nesting` and `postcss-nested`.
73
+ */
74
+ nesting ?: boolean ;
62
75
} ;
63
76
64
77
export default function tailwindIntegration ( options ?: TailwindOptions ) : AstroIntegration {
65
78
const applyBaseStyles = options ?. applyBaseStyles ?? true ;
66
79
const customConfigPath = options ?. configFile ;
80
+ const nesting = options ?. nesting ?? false ;
81
+
67
82
return {
68
83
name : '@astrojs/tailwind' ,
69
84
hooks : {
70
85
'astro:config:setup' : async ( { config, updateConfig, injectScript } ) => {
71
86
// Inject the Tailwind postcss plugin
72
87
updateConfig ( {
73
- vite : await getViteConfiguration ( customConfigPath , config . vite ) ,
88
+ vite : await getViteConfiguration (
89
+ customConfigPath ,
90
+ nesting ,
91
+ fileURLToPath ( config . root ) ,
92
+ config . vite . css ?. postcss
93
+ ) ,
74
94
} ) ;
75
95
76
96
if ( applyBaseStyles ) {
0 commit comments