@@ -59,7 +59,7 @@ export default function tailwindcss(): Plugin[] {
5959 if ( ! module ) {
6060 // The module for this root might not exist yet
6161 if ( root . builtBeforeTransform ) {
62- return
62+ continue
6363 }
6464
6565 // Note: Removing this during SSR is not safe and will produce
@@ -196,19 +196,20 @@ export default function tailwindcss(): Plugin[] {
196196
197197 let root = roots . get ( id )
198198
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.
199202 if ( root . builtBeforeTransform ) {
200203 root . builtBeforeTransform . forEach ( ( file ) => this . addWatchFile ( file ) )
201204 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.
209205 root . requiresRebuild = false
210206 }
211207
208+ // We process Svelte <style> tags in the Svelte preprocessor
209+ if ( isSvelteStyle ( id ) ) {
210+ return src
211+ }
212+
212213 if ( ! options ?. ssr ) {
213214 // Wait until all other files have been processed, so we can extract
214215 // all candidates before generating CSS. This must not be called
@@ -252,6 +253,11 @@ export default function tailwindcss(): Plugin[] {
252253 return
253254 }
254255
256+ // We process Svelte <style> tags in the Svelte preprocessor
257+ if ( isSvelteStyle ( id ) ) {
258+ return src
259+ }
260+
255261 // We do a first pass to generate valid CSS for the downstream plugins.
256262 // However, since not all candidates are guaranteed to be extracted by
257263 // this time, we have to re-run a transform for the root later.
@@ -268,6 +274,8 @@ export default function tailwindcss(): Plugin[] {
268274 // by vite:css-post.
269275 async renderStart ( ) {
270276 for ( let [ id , root ] of roots . entries ( ) ) {
277+ if ( isSvelteStyle ( id ) ) continue
278+
271279 let generated = await regenerateOptimizedCss (
272280 root ,
273281 // During the renderStart phase, we can not add watch files since
@@ -304,13 +312,18 @@ function isPotentialCssRootFile(id: string) {
304312 ( extension === 'css' ||
305313 ( extension === 'vue' && id . includes ( '&lang.css' ) ) ||
306314 ( extension === 'astro' && id . includes ( '&lang.css' ) ) ||
307- ( extension === 'svelte' && id . includes ( '&lang.css' ) ) ) &&
315+ isSvelteStyle ( id ) ) &&
308316 // Don't intercept special static asset resources
309317 ! SPECIAL_QUERY_RE . test ( id )
310318
311319 return isCssFile
312320}
313321
322+ function isSvelteStyle ( id : string ) {
323+ let extension = getExtension ( id )
324+ return extension === 'svelte' && id . includes ( '&lang.css' )
325+ }
326+
314327function optimizeCss (
315328 input : string ,
316329 { file = 'input.css' , minify = false } : { file ?: string ; minify ?: boolean } = { } ,
@@ -559,43 +572,57 @@ class Root {
559572// In practice, it is not recommended to use `@tailwind utilities;` inside
560573// Svelte components. Use an external `.css` file instead.
561574function svelteProcessor ( roots : DefaultMap < string , Root > ) {
575+ let preprocessor = sveltePreprocess ( )
576+
562577 return {
563578 name : '@tailwindcss/svelte' ,
564579 api : {
565- sveltePreprocess : sveltePreprocess ( {
566- aliases : [
567- [ 'postcss' , 'tailwindcss' ] ,
568- [ 'css' , 'tailwindcss' ] ,
569- ] ,
570- async tailwindcss ( {
580+ sveltePreprocess : {
581+ markup : preprocessor . markup ,
582+ script : preprocessor . script ,
583+ async style ( {
571584 content,
572- attributes,
573585 filename,
586+ ...rest
574587 } : {
575588 content : string
576- attributes : Record < string , string >
577589 filename ?: string
590+ attributes : Record < string , string | boolean >
591+ markup : string
578592 } ) {
579- if ( ! filename ) return
593+ if ( ! filename ) return preprocessor . style ?.( { ...rest , content, filename } )
594+
595+ // Create the ID used by Vite to identify the `<style>` contents.
580596 let id = filename + '?svelte&type=style&lang.css'
581597
582598 let root = roots . get ( id )
599+
583600 // Mark this root as being built before the Vite transform hook is
584601 // called. We capture all eventually added dependencies so that we can
585602 // connect them to the vite module graph later, when the transform
586603 // hook is called.
587604 root . builtBeforeTransform = [ ]
605+
606+ // Since a Svelte pre-processor call means that the CSS has changed,
607+ // we need to trigger a rebuild.
608+ root . requiresRebuild = true
609+
588610 let generated = await root . generate ( content , ( file ) =>
589611 root ?. builtBeforeTransform ?. push ( file ) ,
590612 )
591613
614+ // TODO: Warn when `@tailwind utilities;` is used inside Svelte
615+ // <style> tags. Right now, we don't have a way to know if this
616+ // feature is used, since core does the `@import` resolution.
617+
592618 if ( ! generated ) {
593619 roots . delete ( id )
594- return { code : content , attributes }
620+ return preprocessor . style ?. ( { ... rest , content, filename } )
595621 }
596- return { code : generated , attributes }
622+
623+ return preprocessor . style ?.( { ...rest , content : generated , filename } )
597624 } ,
598- } ) ,
625+ } ,
599626 } ,
600627 }
601628}
0 commit comments