Skip to content

Commit 8afd953

Browse files
committed
feat: support multiple tailwindcss context
1 parent 477aadd commit 8afd953

File tree

7 files changed

+76
-6
lines changed

7 files changed

+76
-6
lines changed

packages/tailwindcss-patch/src/cache.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ export function getCacheOptions(options: CacheOptions = {}): Required<CacheOptio
2222
cwd,
2323
dir,
2424
file,
25-
filename
25+
filename,
26+
strategy: 'merge'
2627
}
2728
}
2829

packages/tailwindcss-patch/src/class.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { getClassCacheSet, getContexts, getTailwindcssEntry } from './exposeContext'
2-
import type { CacheOptions, InternalCacheOptions, InternalPatchOptions, TailwindcssPatcherOptions } from './type'
2+
import type { CacheOptions, InternalCacheOptions, InternalPatchOptions, TailwindcssPatcherOptions, CacheStrategy } from './type'
33
import { writeCache, readCache } from './cache'
44
import { createPatch, getPatchOptions } from './patcher'
55

@@ -53,9 +53,34 @@ export class TailwindcssPatcher {
5353
// }
5454
}
5555

56-
getClassSet(basedir?: string) {
56+
/**
57+
* @description 在多个 tailwindcss 上下文时,这个方法将被执行多次,所以策略上应该使用 append
58+
* 详见 taro weapp-tailwindcss 独立分包
59+
* @param basedir
60+
* @returns
61+
*/
62+
getClassSet(
63+
options: {
64+
basedir?: string
65+
cacheStrategy?: CacheStrategy
66+
} = {
67+
cacheStrategy: this.cacheOptions.strategy ?? 'merge'
68+
}
69+
) {
70+
const { basedir, cacheStrategy } = options
5771
const set = getClassCacheSet(basedir)
58-
set.size > 0 && this.setCache(set)
72+
if (cacheStrategy === 'overwrite') {
73+
set.size > 0 && this.setCache(set)
74+
} else if (cacheStrategy === 'merge') {
75+
const cacheSet = this.getCache()
76+
if (cacheSet) {
77+
for (const x of cacheSet) {
78+
set.add(x)
79+
}
80+
}
81+
this.setCache(set)
82+
}
83+
5984
return set
6085
}
6186

packages/tailwindcss-patch/src/exposeContext.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export function getClassCacheSet(basedir?: string): Set<string> {
4545
for (const classCacheMap of classCaches) {
4646
const keys = classCacheMap.keys()
4747
for (const key of keys) {
48-
classSet.add(key)
48+
classSet.add(key.toString())
4949
}
5050
}
5151
return classSet

packages/tailwindcss-patch/src/type.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
export type CacheStrategy = 'merge' | 'overwrite'
2+
13
export interface CacheOptions {
24
// enable?: boolean
35
dir?: string
46
cwd?: string
57
file?: string
8+
strategy?: CacheStrategy
69
}
710

811
export type InternalCacheOptions = CacheOptions & { enable?: boolean }

packages/tailwindcss-patch/test/cache.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import { getCacheOptions, mkCacheDirectory, readCache, writeCache } from '../src
22
import path from 'node:path'
33
import { pkgName } from '../src/constants'
44
import fs from 'node:fs'
5+
import { TailwindcssPatcher } from '../src/class'
6+
import { getCss } from './utils'
7+
58
describe('cache', () => {
69
it('getCacheOptions', () => {
710
expect(getCacheOptions).toBeDefined()
@@ -54,4 +57,33 @@ describe('cache', () => {
5457
expect(cache).toBe(undefined)
5558
expect(fs.existsSync(filepath)).toBe(false)
5659
})
60+
61+
it('multiple tw context merge cache', () => {
62+
const dir = path.resolve(__dirname, './fixtures/cache')
63+
const twPatcher = new TailwindcssPatcher({
64+
cache: {
65+
dir,
66+
file: 'merge-multiple-context.json'
67+
}
68+
})
69+
twPatcher.setCache(new Set())
70+
getCss(['text-[100px]'])
71+
let ctxs = twPatcher.getContexts()
72+
expect(ctxs.length).toBe(1)
73+
let set = twPatcher.getClassSet()
74+
expect(set.size).toBeGreaterThan(0)
75+
expect(set.size).toBe(2)
76+
expect(set.has('text-[100px]')).toBe(true)
77+
78+
// 2 times
79+
// 不累加
80+
getCss(['text-[99px]'])
81+
ctxs = twPatcher.getContexts()
82+
expect(ctxs.length).toBe(1)
83+
set = twPatcher.getClassSet()
84+
expect(set.size).toBeGreaterThan(0)
85+
expect(set.size).toBe(3)
86+
expect(set.has('text-[99px]')).toBe(true)
87+
expect(set.has('text-[100px]')).toBe(true)
88+
})
5789
})

packages/tailwindcss-patch/test/class.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ describe('class', () => {
3131
getCss([getTestCase('hello-world.html'), getTestCase('hello-world.js')])
3232
const ctxs = twPatcher.getContexts()
3333
expect(ctxs.length).toBe(1)
34-
const set = twPatcher.getClassSet()
34+
const set = twPatcher.getClassSet({
35+
cacheStrategy: 'overwrite'
36+
})
3537
expect(set.size).toBeGreaterThan(0)
3638
expect(set.size).toBe(5)
3739
})
@@ -55,5 +57,7 @@ describe('class', () => {
5557
expect(set.size).toBeGreaterThan(0)
5658
expect(set.size).toBe(2)
5759
expect(set.has('text-[99px]')).toBe(true)
60+
61+
twPatcher.setCache(new Set())
5862
})
5963
})
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[
2+
"*",
3+
"text-[99px]",
4+
"text-[100px]"
5+
]

0 commit comments

Comments
 (0)