@@ -544,20 +544,33 @@ function cleanupContext(context) {
544544// plugins) then return it
545545function setupContext ( configOrPath ) {
546546 return ( result , root ) => {
547+ let foundTailwind = false
548+
549+ root . walkAtRules ( 'tailwind' , ( rule ) => {
550+ foundTailwind = true
551+ } )
552+
547553 let sourcePath = result . opts . from
548554 let [ tailwindConfig , userConfigPath , tailwindConfigHash ] = getTailwindConfig ( configOrPath )
549555
550556 let contextDependencies = new Set ( )
551- contextDependencies . add ( sourcePath )
552557
553- if ( userConfigPath !== null ) {
554- contextDependencies . add ( userConfigPath )
558+ // If there are no @tailwind rules, we don't consider this CSS file or it's dependencies
559+ // to be dependencies of the context. Can reuse the context even if they change.
560+ // We may want to think about `@layer` being part of this trigger too, but it's tough
561+ // because it's impossible for a layer in one file to end up in the actual @tailwind rule
562+ // in another file since independent sources are effectively isolated.
563+ if ( foundTailwind ) {
564+ contextDependencies . add ( sourcePath )
565+ for ( let message of result . messages ) {
566+ if ( message . type === 'dependency' ) {
567+ contextDependencies . add ( message . file )
568+ }
569+ }
555570 }
556571
557- for ( let message of result . messages ) {
558- if ( message . type === 'dependency' ) {
559- contextDependencies . add ( message . file )
560- }
572+ if ( userConfigPath !== null ) {
573+ contextDependencies . add ( userConfigPath )
561574 }
562575
563576 let contextDependenciesChanged =
@@ -572,29 +585,26 @@ function setupContext(configOrPath) {
572585 }
573586
574587 // If the config file used already exists in the cache, return that.
575- console . log ( 'dependencies changed: ' , contextDependenciesChanged )
576- console . log ( tailwindConfigHash )
577- console . log ( configContextMap . keys ( ) )
578- console . log ( configContextMap . has ( tailwindConfigHash ) )
579-
580- // DAMN YOU CONTEXTDEPENDENCIESCHANGED
581- // Ideas:
582- // - Can we incorporate the presence of @tailwind rules somehow? If it has no Tailwind rules, be
583- // more lenient about the dependencies changing?
584588 if ( ! contextDependenciesChanged && configContextMap . has ( tailwindConfigHash ) ) {
585589 let context = configContextMap . get ( tailwindConfigHash )
586590 contextSourcesMap . get ( context ) . add ( sourcePath )
587591 contextMap . set ( sourcePath , context )
588592 return context
589593 }
590594
591- // TODO: Update this to no longer associate this path with its old context
592- // and clean up that context if no one else is using it.
595+ // If this source is in the context map, get the old context.
596+ // Remove this source from the context sources for the old context,
597+ // and clean up that context if no one else is using it. This can be
598+ // called by many processes in rapid succession, so we check for presence
599+ // first because the first process to run this code will wipe it out first.
593600 if ( contextMap . has ( sourcePath ) ) {
594601 let oldContext = contextMap . get ( sourcePath )
595- contextSourcesMap . get ( oldContext ) . remove ( sourcePath )
596- if ( contextSourcesMap . get ( oldContext ) . size === 0 ) {
597- cleanupContext ( oldContext )
602+ if ( contextSourcesMap . has ( oldContext ) ) {
603+ contextSourcesMap . get ( oldContext ) . remove ( sourcePath )
604+ if ( contextSourcesMap . get ( oldContext ) . size === 0 ) {
605+ contextSourcesMap . delete ( oldContext )
606+ cleanupContext ( oldContext )
607+ }
598608 }
599609 }
600610
@@ -646,12 +656,6 @@ function setupContext(configOrPath) {
646656 }
647657 }
648658
649- let prev = null
650- for ( let [ key , value ] of contextSourcesMap ) {
651- console . log ( key === prev )
652- prev = key
653- }
654-
655659 rebootWatcher ( context )
656660
657661 let corePluginList = Object . entries ( corePlugins )
0 commit comments