Skip to content

Commit 6de7f04

Browse files
committed
If CSS file has no @tailwind rules, don't consider it a context dependency
1 parent c2551f7 commit 6de7f04

File tree

3 files changed

+33
-41
lines changed

3 files changed

+33
-41
lines changed

src/index.js

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,7 @@ module.exports = (configOrPath = {}) => {
3131
})
3232
}
3333

34-
// TODO: Maybe we only set up a context + run context dependent plugins
35-
// on files that contain @tailwind rules? I don't know.
36-
let foundTailwind = false
37-
root.walkAtRules('tailwind', (rule) => {
38-
foundTailwind = true
39-
})
40-
41-
let context = null
42-
43-
if (foundTailwind) {
44-
context = setupContext(configOrPath)(result, root)
45-
}
34+
let context = setupContext(configOrPath)(result, root)
4635

4736
if (context.configPath !== null) {
4837
registerDependency(context.configPath)

src/lib/expandTailwindAtRules.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,7 @@ function expandTailwindAtRules(context, registerDependency) {
206206
if (env.DEBUG) {
207207
console.log('Changed files: ', context.changedFiles.size)
208208
console.log('Potential classes: ', candidates.size)
209-
console.log('Active contexts: ', sharedState.contextMap.size)
210-
// console.log('Context source map: ', sharedState.contextSourcesMap)
209+
console.log('Active contexts: ', sharedState.contextSourcesMap.size)
211210
console.log('Content match entries', contentMatchCache.size)
212211
}
213212

src/lib/setupContext.js

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -544,20 +544,33 @@ function cleanupContext(context) {
544544
// plugins) then return it
545545
function 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

Comments
 (0)