@@ -43,7 +43,7 @@ function tailwindcss(opts: PluginOptions = {}): AcceptedPlugin {
43
43
let cache = new DefaultMap ( ( ) => {
44
44
return {
45
45
mtimes : new Map < string , number > ( ) ,
46
- build : null as null | ReturnType < typeof compile > [ 'build' ] ,
46
+ compiler : null as null | ReturnType < typeof compile > ,
47
47
css : '' ,
48
48
optimizedCss : '' ,
49
49
}
@@ -76,6 +76,23 @@ function tailwindcss(opts: PluginOptions = {}): AcceptedPlugin {
76
76
OnceExit ( root , { result } ) {
77
77
let inputFile = result . opts . from ?? ''
78
78
let context = cache . get ( inputFile )
79
+ let inputBasePath = path . dirname ( path . resolve ( inputFile ) )
80
+
81
+ function createCompiler ( ) {
82
+ return compile ( root . toString ( ) , {
83
+ loadPlugin : ( pluginPath ) => {
84
+ if ( pluginPath [ 0 ] === '.' ) {
85
+ return require ( path . resolve ( inputBasePath , pluginPath ) )
86
+ }
87
+
88
+ return require ( pluginPath )
89
+ } ,
90
+ } )
91
+ }
92
+
93
+ // Setup the compiler if it doesn't exist yet. This way we can
94
+ // guarantee a `compile()` function is available.
95
+ context . compiler ??= createCompiler ( )
79
96
80
97
let rebuildStrategy : 'full' | 'incremental' = 'incremental'
81
98
@@ -106,50 +123,46 @@ function tailwindcss(opts: PluginOptions = {}): AcceptedPlugin {
106
123
// Do nothing if neither `@tailwind` nor `@apply` is used
107
124
if ( ! hasTailwind && ! hasApply ) return
108
125
109
- let css = ''
110
-
111
- // Look for candidates used to generate the CSS
112
- let { candidates, files, globs } = scanDir ( { base } )
126
+ let css = ''
113
127
114
- // Add all found files as direct dependencies
115
- for ( let file of files ) {
116
- result . messages . push ( {
117
- type : 'dependency' ,
118
- plugin : '@tailwindcss/postcss' ,
119
- file ,
120
- parent : result . opts . from ,
128
+ // Look for candidates used to generate the CSS
129
+ let scanDirResult = scanDir ( {
130
+ base , // Root directory, mainly used for auto content detection
131
+ contentPaths : context . compiler . globs . map ( ( glob ) => ( {
132
+ base : inputBasePath , // Globs are relative to the input.css file
133
+ glob ,
134
+ } ) ) ,
121
135
} )
122
- }
123
-
124
- // Register dependencies so changes in `base` cause a rebuild while
125
- // giving tools like Vite or Parcel a glob that can be used to limit
126
- // the files that cause a rebuild to only those that match it.
127
- for ( let { base, glob } of globs ) {
128
- result . messages . push ( {
129
- type : 'dir-dependency' ,
130
- plugin : '@tailwindcss/postcss' ,
131
- dir : base ,
132
- glob,
133
- parent : result . opts . from ,
134
- } )
135
- }
136
-
137
- if ( rebuildStrategy === 'full' ) {
138
- let basePath = path . dirname ( path . resolve ( inputFile ) )
139
- let { build } = compile ( root . toString ( ) , {
140
- loadPlugin : ( pluginPath ) => {
141
- if ( pluginPath [ 0 ] === '.' ) {
142
- return require ( path . resolve ( basePath , pluginPath ) )
143
- }
144
136
145
- return require ( pluginPath )
146
- } ,
147
- } )
148
- context . build = build
149
- css = build ( hasTailwind ? candidates : [ ] )
150
- } else if ( rebuildStrategy === 'incremental' ) {
151
- css = context . build ! ( candidates )
152
- }
137
+ // Add all found files as direct dependencies
138
+ for ( let file of scanDirResult . files ) {
139
+ result . messages . push ( {
140
+ type : 'dependency' ,
141
+ plugin : '@tailwindcss/postcss' ,
142
+ file,
143
+ parent : result . opts . from ,
144
+ } )
145
+ }
146
+
147
+ // Register dependencies so changes in `base` cause a rebuild while
148
+ // giving tools like Vite or Parcel a glob that can be used to limit
149
+ // the files that cause a rebuild to only those that match it.
150
+ for ( let { base, glob } of scanDirResult . globs ) {
151
+ result . messages . push ( {
152
+ type : 'dir-dependency' ,
153
+ plugin : '@tailwindcss/postcss' ,
154
+ dir : base ,
155
+ glob,
156
+ parent : result . opts . from ,
157
+ } )
158
+ }
159
+
160
+ if ( rebuildStrategy === 'full' ) {
161
+ context . compiler = createCompiler ( )
162
+ css = context . compiler . build ( hasTailwind ? scanDirResult . candidates : [ ] )
163
+ } else if ( rebuildStrategy === 'incremental' ) {
164
+ css = context . compiler . build ! ( scanDirResult . candidates )
165
+ }
153
166
154
167
// Replace CSS
155
168
if ( css !== context . css && optimize ) {
0 commit comments