Skip to content

Commit 6542453

Browse files
committed
fix(tailwindcss-patch): getClassSetSync() returning an empty set
1 parent 0b484c1 commit 6542453

File tree

3 files changed

+52
-16
lines changed

3 files changed

+52
-16
lines changed

.changeset/reliable-otters-ring.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'tailwindcss-patch': patch
3+
---
4+
5+
Fix `getClassSetSync()` returning an empty set before Tailwind v3 contexts are ready so runtime collectors fall back to the async extraction path instead of skipping class discovery.

packages/tailwindcss-patch/src/api/tailwindcss-patcher.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,6 @@ export class TailwindcssPatcher {
150150
return collectClassesFromContexts(contexts, this.options.filter)
151151
}
152152

153-
private collectClassSetSync(): Set<string> {
154-
if (this.majorVersion === 4) {
155-
throw new Error('getClassSetSync is not supported for Tailwind CSS v4 projects. Use getClassSet instead.')
156-
}
157-
158-
const contexts = this.getContexts()
159-
return collectClassesFromContexts(contexts, this.options.filter)
160-
}
161-
162153
private async mergeWithCache(set: Set<string>) {
163154
if (!this.options.cache.enabled) {
164155
return set
@@ -213,9 +204,18 @@ export class TailwindcssPatcher {
213204
return this.mergeWithCache(set)
214205
}
215206

216-
getClassSetSync() {
217-
const set = this.collectClassSetSync()
218-
return this.mergeWithCacheSync(set)
207+
getClassSetSync(): Set<string> | undefined {
208+
if (this.majorVersion === 4) {
209+
throw new Error('getClassSetSync is not supported for Tailwind CSS v4 projects. Use getClassSet instead.')
210+
}
211+
212+
const contexts = this.getContexts()
213+
const set = collectClassesFromContexts(contexts, this.options.filter)
214+
const merged = this.mergeWithCacheSync(set)
215+
if (contexts.length === 0 && merged.size === 0) {
216+
return undefined
217+
}
218+
return merged
219219
}
220220

221221
async extract(options?: { write?: boolean }): Promise<ExtractResult> {

packages/tailwindcss-patch/test/api.tailwindcss-patcher.test.ts

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,9 @@ describe('TailwindcssPatcher', () => {
128128

129129
const result = patcher.getClassSetSync()
130130

131-
expect(result.has('foo')).toBe(true)
132-
expect(result.has('bar')).toBe(true)
131+
expect(result).toBeDefined()
132+
expect(result?.has('foo')).toBe(true)
133+
expect(result?.has('bar')).toBe(true)
133134
expect(fs.pathExistsSync(cacheFile)).toBe(true)
134135
const cacheContent = fs.readJSONSync(cacheFile)
135136
expect(cacheContent).toEqual(expect.arrayContaining(['foo', 'bar']))
@@ -161,7 +162,37 @@ describe('TailwindcssPatcher', () => {
161162

162163
const result = patcher.getClassSetSync()
163164

164-
expect(result.size).toBe(1)
165-
expect(result.has('cached-class')).toBe(true)
165+
expect(result).toBeDefined()
166+
expect(result?.size).toBe(1)
167+
expect(result?.has('cached-class')).toBe(true)
168+
})
169+
170+
it('defers synchronous class access until runtime contexts are populated', async () => {
171+
const tooltipClass = 'text-[#123456]'
172+
const contexts: any[] = []
173+
174+
const patcher = new TailwindcssPatcher({
175+
overwrite: false,
176+
cache: false,
177+
tailwind: {
178+
packageName: 'tailwindcss-3',
179+
version: 3,
180+
},
181+
})
182+
183+
vi.spyOn(patcher, 'getContexts').mockImplementation(() => contexts)
184+
vi.spyOn(patcher as any, 'runTailwindBuildIfNeeded').mockImplementation(async () => {
185+
contexts.push({
186+
classCache: new Map([[tooltipClass, []]]),
187+
})
188+
})
189+
190+
expect(patcher.getClassSetSync()).toBeUndefined()
191+
192+
const { classSet } = await patcher.extract({ write: false })
193+
expect(classSet?.has(tooltipClass)).toBe(true)
194+
195+
const syncSet = patcher.getClassSetSync()
196+
expect(syncSet?.has(tooltipClass)).toBe(true)
166197
})
167198
})

0 commit comments

Comments
 (0)