@@ -59,7 +59,7 @@ export default function tailwindcss(): Plugin[] {
59
59
if ( ! module ) {
60
60
// The module for this root might not exist yet
61
61
if ( root . builtBeforeTransform ) {
62
- return
62
+ continue
63
63
}
64
64
65
65
// Note: Removing this during SSR is not safe and will produce
@@ -196,17 +196,17 @@ export default function tailwindcss(): Plugin[] {
196
196
197
197
let root = roots . get ( id )
198
198
199
+ // If the root was built outside of the transform hook (e.g. in the
200
+ // Svelte preprocessor), we still want to mark all dependencies of the
201
+ // root as watched files.
199
202
if ( root . builtBeforeTransform ) {
200
203
root . builtBeforeTransform . forEach ( ( file ) => this . addWatchFile ( file ) )
201
204
root . builtBeforeTransform = undefined
202
- // When a root was built before this transform hook, the candidate
203
- // list might be outdated already by the time the transform hook is
204
- // called.
205
- //
206
- // This requires us to build the CSS file again. However, we do not
207
- // expect dependencies to have changed, so we can avoid a full
208
- // rebuild.
209
- root . requiresRebuild = false
205
+ }
206
+
207
+ // We only process Svelte `<style>` tags in the `sveltePreprocessor`
208
+ if ( isSvelteStyle ( id ) ) {
209
+ return src
210
210
}
211
211
212
212
if ( ! options ?. ssr ) {
@@ -240,16 +240,17 @@ export default function tailwindcss(): Plugin[] {
240
240
241
241
let root = roots . get ( id )
242
242
243
+ // If the root was built outside of the transform hook (e.g. in the
244
+ // Svelte preprocessor), we still want to mark all dependencies of the
245
+ // root as watched files.
243
246
if ( root . builtBeforeTransform ) {
244
247
root . builtBeforeTransform . forEach ( ( file ) => this . addWatchFile ( file ) )
245
248
root . builtBeforeTransform = undefined
246
- // When a root was built before this transform hook, the candidate
247
- // list might be outdated already by the time the transform hook is
248
- // called.
249
- //
250
- // Since we already do a second render pass in build mode, we don't
251
- // need to do any more work here.
252
- return
249
+ }
250
+
251
+ // We only process Svelte `<style>` tags in the `sveltePreprocessor`
252
+ if ( isSvelteStyle ( id ) ) {
253
+ return src
253
254
}
254
255
255
256
// We do a first pass to generate valid CSS for the downstream plugins.
@@ -268,6 +269,9 @@ export default function tailwindcss(): Plugin[] {
268
269
// by vite:css-post.
269
270
async renderStart ( ) {
270
271
for ( let [ id , root ] of roots . entries ( ) ) {
272
+ // Do not do a second render pass on Svelte `<style>` tags.
273
+ if ( isSvelteStyle ( id ) ) continue
274
+
271
275
let generated = await regenerateOptimizedCss (
272
276
root ,
273
277
// During the renderStart phase, we can not add watch files since
@@ -304,13 +308,18 @@ function isPotentialCssRootFile(id: string) {
304
308
( extension === 'css' ||
305
309
( extension === 'vue' && id . includes ( '&lang.css' ) ) ||
306
310
( extension === 'astro' && id . includes ( '&lang.css' ) ) ||
307
- ( extension === 'svelte' && id . includes ( '&lang.css' ) ) ) &&
311
+ isSvelteStyle ( id ) ) &&
308
312
// Don't intercept special static asset resources
309
313
! SPECIAL_QUERY_RE . test ( id )
310
314
311
315
return isCssFile
312
316
}
313
317
318
+ function isSvelteStyle ( id : string ) {
319
+ let extension = getExtension ( id )
320
+ return extension === 'svelte' && id . includes ( '&lang.css' )
321
+ }
322
+
314
323
function optimizeCss (
315
324
input : string ,
316
325
{ file = 'input.css' , minify = false } : { file ?: string ; minify ?: boolean } = { } ,
@@ -552,50 +561,63 @@ class Root {
552
561
// enabled. This allows us to transform CSS in `<style>` tags and create a
553
562
// stricter version of CSS that passes the Svelte compiler.
554
563
//
555
- // Note that these files will undergo a second pass through the vite transpiler
556
- // later. This is necessary to compute `@tailwind utilities;` with the right
557
- // candidate list .
564
+ // Note that these files will not undergo a second pass through the vite
565
+ // transpiler later. This means that `@tailwind utilities;` will not be up to
566
+ // date .
558
567
//
559
- // In practice, it is not recommended to use `@tailwind utilities;` inside
560
- // Svelte components. Use an external `.css` file instead.
568
+ // In practice, it is discouraged to use `@tailwind utilities;` inside Svelte
569
+ // components, as the styles it create would be scoped anyways. Use an external
570
+ // `.css` file instead.
561
571
function svelteProcessor ( roots : DefaultMap < string , Root > ) {
572
+ let preprocessor = sveltePreprocess ( )
573
+
562
574
return {
563
575
name : '@tailwindcss/svelte' ,
564
576
api : {
565
- sveltePreprocess : sveltePreprocess ( {
566
- aliases : [
567
- [ 'postcss' , 'tailwindcss' ] ,
568
- [ 'css' , 'tailwindcss' ] ,
569
- ] ,
570
- async tailwindcss ( {
577
+ sveltePreprocess : {
578
+ markup : preprocessor . markup ,
579
+ script : preprocessor . script ,
580
+ async style ( {
571
581
content,
572
- attributes,
573
582
filename,
583
+ ...rest
574
584
} : {
575
585
content : string
576
- attributes : Record < string , string >
577
586
filename ?: string
587
+ attributes : Record < string , string | boolean >
588
+ markup : string
578
589
} ) {
579
- if ( ! filename ) return
590
+ if ( ! filename ) return preprocessor . style ?.( { ...rest , content, filename } )
591
+
592
+ // Create the ID used by Vite to identify the `<style>` contents. This
593
+ // way, the Vite `transform` hook can find the right root and thus
594
+ // track the right dependencies.
580
595
let id = filename + '?svelte&type=style&lang.css'
581
596
582
597
let root = roots . get ( id )
598
+
599
+ // Since a Svelte pre-processor call means that the CSS has changed,
600
+ // we need to trigger a rebuild.
601
+ root . requiresRebuild = true
602
+
583
603
// Mark this root as being built before the Vite transform hook is
584
604
// called. We capture all eventually added dependencies so that we can
585
605
// connect them to the vite module graph later, when the transform
586
606
// hook is called.
587
607
root . builtBeforeTransform = [ ]
608
+
588
609
let generated = await root . generate ( content , ( file ) =>
589
610
root ?. builtBeforeTransform ?. push ( file ) ,
590
611
)
591
612
592
613
if ( ! generated ) {
593
614
roots . delete ( id )
594
- return { code : content , attributes }
615
+ return preprocessor . style ?. ( { ... rest , content, filename } )
595
616
}
596
- return { code : generated , attributes }
617
+
618
+ return preprocessor . style ?.( { ...rest , content : generated , filename } )
597
619
} ,
598
- } ) ,
620
+ } ,
599
621
} ,
600
622
}
601
623
}
0 commit comments