@@ -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,17 +196,17 @@ 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.
209- root . requiresRebuild = false
205+ }
206+
207+ // We only process Svelte `<style>` tags in the `sveltePreprocessor`
208+ if ( isSvelteStyle ( id ) ) {
209+ return src
210210 }
211211
212212 if ( ! options ?. ssr ) {
@@ -240,16 +240,17 @@ export default function tailwindcss(): Plugin[] {
240240
241241 let root = roots . get ( id )
242242
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.
243246 if ( root . builtBeforeTransform ) {
244247 root . builtBeforeTransform . forEach ( ( file ) => this . addWatchFile ( file ) )
245248 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
253254 }
254255
255256 // We do a first pass to generate valid CSS for the downstream plugins.
@@ -268,6 +269,9 @@ export default function tailwindcss(): Plugin[] {
268269 // by vite:css-post.
269270 async renderStart ( ) {
270271 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+
271275 let generated = await regenerateOptimizedCss (
272276 root ,
273277 // During the renderStart phase, we can not add watch files since
@@ -304,13 +308,18 @@ function isPotentialCssRootFile(id: string) {
304308 ( extension === 'css' ||
305309 ( extension === 'vue' && id . includes ( '&lang.css' ) ) ||
306310 ( extension === 'astro' && id . includes ( '&lang.css' ) ) ||
307- ( extension === 'svelte' && id . includes ( '&lang.css' ) ) ) &&
311+ isSvelteStyle ( id ) ) &&
308312 // Don't intercept special static asset resources
309313 ! SPECIAL_QUERY_RE . test ( id )
310314
311315 return isCssFile
312316}
313317
318+ function isSvelteStyle ( id : string ) {
319+ let extension = getExtension ( id )
320+ return extension === 'svelte' && id . includes ( '&lang.css' )
321+ }
322+
314323function optimizeCss (
315324 input : string ,
316325 { file = 'input.css' , minify = false } : { file ?: string ; minify ?: boolean } = { } ,
@@ -552,50 +561,63 @@ class Root {
552561// enabled. This allows us to transform CSS in `<style>` tags and create a
553562// stricter version of CSS that passes the Svelte compiler.
554563//
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 .
558567//
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.
561571function svelteProcessor ( roots : DefaultMap < string , Root > ) {
572+ let preprocessor = sveltePreprocess ( )
573+
562574 return {
563575 name : '@tailwindcss/svelte' ,
564576 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 ( {
571581 content,
572- attributes,
573582 filename,
583+ ...rest
574584 } : {
575585 content : string
576- attributes : Record < string , string >
577586 filename ?: string
587+ attributes : Record < string , string | boolean >
588+ markup : string
578589 } ) {
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.
580595 let id = filename + '?svelte&type=style&lang.css'
581596
582597 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+
583603 // Mark this root as being built before the Vite transform hook is
584604 // called. We capture all eventually added dependencies so that we can
585605 // connect them to the vite module graph later, when the transform
586606 // hook is called.
587607 root . builtBeforeTransform = [ ]
608+
588609 let generated = await root . generate ( content , ( file ) =>
589610 root ?. builtBeforeTransform ?. push ( file ) ,
590611 )
591612
592613 if ( ! generated ) {
593614 roots . delete ( id )
594- return { code : content , attributes }
615+ return preprocessor . style ?. ( { ... rest , content, filename } )
595616 }
596- return { code : generated , attributes }
617+
618+ return preprocessor . style ?.( { ...rest , content : generated , filename } )
597619 } ,
598- } ) ,
620+ } ,
599621 } ,
600622 }
601623}
0 commit comments