Skip to content

Commit 229b1a6

Browse files
committed
chore: bump version
1 parent 84bf57a commit 229b1a6

File tree

6 files changed

+64
-23
lines changed

6 files changed

+64
-23
lines changed

.changeset/wide-bobcats-know.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tailwindcss-patch": patch
3+
---
4+
5+
feat: add filter option for PatchOptions

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@ export class TailwindcssPatcher {
2222
public cacheManager: CacheManager
2323
public packageInfo: PackageInfo
2424
public majorVersion?: number
25+
public filter?: (className: string) => boolean
2526

2627
constructor(options: TailwindcssPatcherOptions = {}) {
2728
this.rawOptions = options
2829
this.cacheOptions = getCacheOptions(options.cache)
2930
this.patchOptions = getPatchOptions(options.patch)
3031

3132
this.cacheManager = new CacheManager(this.cacheOptions)
32-
33+
this.filter = this.patchOptions.filter
3334
const packageInfo = getPackageInfoSync(
3435
this.patchOptions.packageName ?? 'tailwindcss',
3536
this.patchOptions.resolve,
@@ -132,7 +133,7 @@ export class TailwindcssPatcher {
132133
}),
133134
})
134135
for (const candidate of candidates) {
135-
classSet.add(candidate)
136+
this.filter?.(candidate) && classSet.add(candidate)
136137
}
137138
}
138139
}
@@ -148,7 +149,7 @@ export class TailwindcssPatcher {
148149
}),
149150
})
150151
for (const candidate of candidates) {
151-
classSet.add(candidate)
152+
this.filter?.(candidate) && classSet.add(candidate)
152153
}
153154
}
154155
}
@@ -162,7 +163,7 @@ export class TailwindcssPatcher {
162163
if (output?.removeUniversalSelector && v === '*') {
163164
continue
164165
}
165-
classSet.add(v)
166+
this.filter?.(v) && classSet.add(v)
166167
}
167168
}
168169
}

packages/tailwindcss-patch/src/defaults.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
import type { DeepRequired, InternalPatchOptions, PatchOptions } from './types'
1+
import type { InternalPatchOptions, PatchOptions } from './types'
22
import process from 'node:process'
33
import { defu } from '@tailwindcss-mangle/shared'
44

5-
export function getDefaultPatchOptions(): DeepRequired<PatchOptions> {
5+
export function getDefaultPatchOptions() {
66
return {
77
packageName: 'tailwindcss',
88
applyPatches: {
99
exportContext: true,
1010
extendLengthUnits: false,
1111
},
1212
overwrite: true,
13+
filter: () => true,
1314
}
1415
}
1516

packages/tailwindcss-patch/src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export interface PatchOptions extends PatchUserConfig {
3737
exportContext?: boolean
3838
extendLengthUnits?: boolean | ILengthUnitsPatchOptions
3939
}
40+
filter?: (className: string) => boolean
4041
}
4142

4243
export interface InternalPatchOptions extends PatchOptions {

packages/tailwindcss-patch/test/__snapshots__/defaults.test.ts.snap

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ exports[`defaults > getDefaultPatchOptions 1`] = `
66
"exportContext": true,
77
"extendLengthUnits": false,
88
},
9+
"filter": [Function],
910
"overwrite": true,
1011
"packageName": "tailwindcss",
1112
}

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

+49-17
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,19 @@ import { getCss, getTestCase } from './utils'
55
describe('class', () => {
66
it('default', async () => {
77
// const dir = path.resolve(__dirname, './fixtures/cache')
8-
const twPatcher = new TailwindcssPatcher()
8+
const twPatcher = new TailwindcssPatcher({
9+
patch: {
10+
output: {
11+
removeUniversalSelector: false,
12+
},
13+
},
14+
})
915
expect(twPatcher.cacheOptions.enable).toBe(false)
1016
twPatcher.patch()
1117
await getCss([getTestCase('hello-world.html')])
1218
const ctxs = twPatcher.getContexts()
1319
expect(ctxs.length).toBe(1)
14-
const set = await twPatcher.getClassSet({
15-
output: {
16-
removeUniversalSelector: false,
17-
},
18-
})
20+
const set = await twPatcher.getClassSet()
1921
expect(set.size).toBeGreaterThan(0)
2022
expect(set.size).toBe(4)
2123
})
@@ -48,13 +50,17 @@ describe('class', () => {
4850
})
4951

5052
it('multiple time process sources', async () => {
51-
const twPatcher = new TailwindcssPatcher()
53+
const twPatcher = new TailwindcssPatcher({
54+
patch: {
55+
output: {
56+
removeUniversalSelector: false,
57+
},
58+
},
59+
})
5260
await getCss(['text-[100px]'])
5361
let ctxs = twPatcher.getContexts()
5462
expect(ctxs.length).toBe(1)
55-
let set = await twPatcher.getClassSet({
56-
removeUniversalSelector: false,
57-
})
63+
let set = await twPatcher.getClassSet()
5864
expect(set.size).toBeGreaterThan(0)
5965
expect(set.size).toBe(2)
6066
expect(set.has('text-[100px]')).toBe(true)
@@ -64,9 +70,7 @@ describe('class', () => {
6470
await getCss(['text-[99px]'])
6571
ctxs = twPatcher.getContexts()
6672
expect(ctxs.length).toBe(1)
67-
set = await twPatcher.getClassSet({
68-
removeUniversalSelector: false,
69-
})
73+
set = await twPatcher.getClassSet()
7074
expect(set.size).toBeGreaterThan(0)
7175
expect(set.size).toBe(2)
7276
expect(set.has('text-[99px]')).toBe(true)
@@ -75,21 +79,49 @@ describe('class', () => {
7579
})
7680

7781
it('wxml process sources', async () => {
78-
const twPatcher = new TailwindcssPatcher()
82+
const twPatcher = new TailwindcssPatcher({
83+
patch: {
84+
output: {
85+
removeUniversalSelector: false,
86+
},
87+
},
88+
})
7989
twPatcher.patch()
8090
await getCss([`<view class="bg-[#7d7ac2] text-[100px] text-[#123456] {{true?'h-[30px]':'h-[45px]'}}">111</view>`])
8191
const ctxs = twPatcher.getContexts()
8292
expect(ctxs.length).toBe(1)
83-
const set = await twPatcher.getClassSet({
84-
removeUniversalSelector: false,
85-
})
93+
const set = await twPatcher.getClassSet()
8694
expect(set.size).toBeGreaterThan(0)
8795
expect(set.size).toBe(6)
8896
expect(set.has('text-[100px]')).toBe(true)
8997
expect(set.has('h-[30px]')).toBe(true)
9098
expect(set.has('h-[45px]')).toBe(true)
9199
})
92100

101+
it('wxml process sources filter', async () => {
102+
const twPatcher = new TailwindcssPatcher({
103+
patch: {
104+
output: {
105+
removeUniversalSelector: false,
106+
},
107+
filter(className) {
108+
return className.includes('text-[100px]') || className.includes('h-[')
109+
},
110+
},
111+
})
112+
twPatcher.patch()
113+
await getCss([`<view class="bg-[#7d7ac2] text-[100px] text-[#123456] {{true?'h-[30px]':'h-[45px]'}}">111</view>`])
114+
const ctxs = twPatcher.getContexts()
115+
expect(ctxs.length).toBe(1)
116+
const set = await twPatcher.getClassSet()
117+
expect(set.size).toBeGreaterThan(0)
118+
expect(set.size).toBe(3)
119+
expect(set.has('text-[100px]')).toBe(true)
120+
expect(set.has('h-[30px]')).toBe(true)
121+
expect(set.has('h-[45px]')).toBe(true)
122+
expect(set.has('bg-[#7d7ac2]')).toBe(false)
123+
})
124+
93125
it('extract', async () => {
94126
const twPatcher = new TailwindcssPatcher()
95127
const filename = await twPatcher.extract()

0 commit comments

Comments
 (0)