@@ -17,7 +17,7 @@ import { LoadResultCache, createCachedLoad } from '../load-result-cache';
17
17
* The lazy-loaded instance of the postcss stylesheet postprocessor.
18
18
* It is only imported and initialized if postcss is needed.
19
19
*/
20
- let postcss : typeof import ( 'postcss' ) [ 'default' ] | undefined ;
20
+ let postcss : ( typeof import ( 'postcss' ) ) [ 'default' ] | undefined ;
21
21
22
22
/**
23
23
* An object containing the plugin options to use when processing stylesheets.
@@ -121,12 +121,6 @@ export class StylesheetPluginFactory {
121
121
) ;
122
122
123
123
const [ format , , filename ] = args . path . split ( ';' , 3 ) ;
124
- // Only use postcss if Tailwind processing is required.
125
- // NOTE: If postcss is used for more than just Tailwind in the future this check MUST
126
- // be updated to account for the additional use.
127
- // TODO: use better search algorithm for keywords
128
- const needsPostcss =
129
- ! ! postcssProcessor && TAILWIND_KEYWORDS . some ( ( keyword ) => data . includes ( keyword ) ) ;
130
124
131
125
return processStylesheet (
132
126
language ,
@@ -135,7 +129,7 @@ export class StylesheetPluginFactory {
135
129
format ,
136
130
options ,
137
131
build ,
138
- needsPostcss ? postcssProcessor : undefined ,
132
+ postcssProcessor ,
139
133
) ;
140
134
} ) ,
141
135
) ;
@@ -145,8 +139,6 @@ export class StylesheetPluginFactory {
145
139
{ filter : language . fileFilter } ,
146
140
createCachedLoad ( cache , async ( args ) => {
147
141
const data = await readFile ( args . path , 'utf-8' ) ;
148
- const needsPostcss =
149
- ! ! postcssProcessor && TAILWIND_KEYWORDS . some ( ( keyword ) => data . includes ( keyword ) ) ;
150
142
151
143
return processStylesheet (
152
144
language ,
@@ -155,7 +147,7 @@ export class StylesheetPluginFactory {
155
147
extname ( args . path ) . toLowerCase ( ) . slice ( 1 ) ,
156
148
options ,
157
149
build ,
158
- needsPostcss ? postcssProcessor : undefined ,
150
+ postcssProcessor ,
159
151
) ;
160
152
} ) ,
161
153
) ;
@@ -186,8 +178,15 @@ async function processStylesheet(
186
178
} ;
187
179
}
188
180
189
- // Transform with postcss if needed and there are no errors
190
- if ( postcssProcessor && result . contents && ! result . errors ?. length ) {
181
+ // Return early if there are no contents to further process
182
+ if ( ! result . contents ) {
183
+ return result ;
184
+ }
185
+
186
+ // Only use postcss if Tailwind processing is required.
187
+ // NOTE: If postcss is used for more than just Tailwind in the future this check MUST
188
+ // be updated to account for the additional use.
189
+ if ( postcssProcessor && ! result . errors ?. length && hasTailwindKeywords ( result . contents ) ) {
191
190
const postcssResult = await compileString (
192
191
typeof result . contents === 'string'
193
192
? result . contents
@@ -219,6 +218,24 @@ async function processStylesheet(
219
218
return result ;
220
219
}
221
220
221
+ /**
222
+ * Searches the provided contents for keywords that indicate Tailwind is used
223
+ * within a stylesheet.
224
+ * @param contents A string or Uint8Array containing UTF-8 text.
225
+ * @returns True, if the contents contains tailwind keywords; False, otherwise.
226
+ */
227
+ function hasTailwindKeywords ( contents : string | Uint8Array ) : boolean {
228
+ // TODO: use better search algorithm for keywords
229
+ if ( typeof contents === 'string' ) {
230
+ return TAILWIND_KEYWORDS . some ( ( keyword ) => contents . includes ( keyword ) ) ;
231
+ }
232
+
233
+ // Contents is a Uint8Array
234
+ const data = contents instanceof Buffer ? contents : Buffer . from ( contents ) ;
235
+
236
+ return TAILWIND_KEYWORDS . some ( ( keyword ) => data . includes ( keyword ) ) ;
237
+ }
238
+
222
239
/**
223
240
* Compiles the provided CSS stylesheet data using a provided postcss processor and provides an
224
241
* esbuild load result that can be used directly by an esbuild Plugin.
0 commit comments