Skip to content

Commit 8e364e4

Browse files
committed
chore: bump version
1 parent 889d846 commit 8e364e4

File tree

10 files changed

+62
-68
lines changed

10 files changed

+62
-68
lines changed

.changeset/blue-walls-smoke.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+
feat!: remove getClassSet params and set default patch options

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,23 @@ export class CacheManager {
4949
}
5050
}
5151

52-
write(data: Set<string>) {
52+
async write(data: Set<string>) {
5353
try {
54-
const { dir, filename } = this.options
55-
fs.ensureDirSync(dir)
56-
fs.outputFileSync(filename, JSON.stringify([...data], undefined, 2), 'utf8')
54+
const { filename } = this.options
55+
await fs.outputJSON(filename, [...data])
5756
return filename
5857
}
5958
catch {
6059
logger.error('write cache file fail!')
6160
}
6261
}
6362

64-
read() {
63+
async read() {
6564
const { filename } = this.options
6665
try {
67-
if (fs.existsSync(filename)) {
68-
const data = fs.readFileSync(filename, 'utf8')
69-
return new Set<string>(JSON.parse(data))
66+
if (await fs.exists(filename)) {
67+
const data = await fs.readJSON(filename)
68+
return new Set<string>(data)
7069
}
7170
}
7271
catch {

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

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ export class TailwindcssPatcher {
8989
return contexts.filter(x => isObject(x)).map(x => x.classCache)
9090
}
9191

92-
async getClassCacheSet(options?: PatchUserConfig): Promise<Set<string>> {
92+
async getClassCacheSet(): Promise<Set<string>> {
9393
const classSet = new Set<string>()
94-
const { output, tailwindcss } = options ?? {}
94+
const { output, tailwindcss } = this.patchOptions
9595
if (this.majorVersion === 4) {
9696
const { v4 } = tailwindcss ?? {}
9797
if (Array.isArray(v4?.cssEntries)) {
@@ -161,18 +161,14 @@ export class TailwindcssPatcher {
161161
/**
162162
* @description 在多个 tailwindcss 上下文时,这个方法将被执行多次,所以策略上应该使用 append
163163
*/
164-
async getClassSet(options?: PatchUserConfig) {
165-
const { output, tailwindcss } = options ?? {}
164+
async getClassSet() {
166165
const cacheStrategy = this.cacheOptions.strategy ?? 'merge'
167-
const set = await this.getClassCacheSet({
168-
output,
169-
tailwindcss,
170-
})
166+
const set = await this.getClassCacheSet()
171167
if (cacheStrategy === 'overwrite') {
172168
set.size > 0 && this.setCache(set)
173169
}
174170
else if (cacheStrategy === 'merge') {
175-
const cacheSet = this.getCache()
171+
const cacheSet = await this.getCache()
176172
if (cacheSet) {
177173
for (const x of cacheSet) {
178174
set.add(x)
@@ -196,10 +192,7 @@ export class TailwindcssPatcher {
196192
})
197193
}
198194

199-
const set = await this.getClassSet({
200-
output,
201-
tailwindcss,
202-
})
195+
const set = await this.getClassSet()
203196
if (filename) {
204197
const classList = [...set]
205198
await fs.outputJSON(filename, classList, {

packages/tailwindcss-patch/src/types.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
/* eslint-disable ts/no-unsafe-function-type */
2+
import type { PatchUserConfig } from '@tailwindcss-mangle/config'
3+
import type { PackageJson } from 'pkg-types'
24
import type { Node, Rule } from 'postcss'
35
import type { Config } from 'tailwindcss'
46

@@ -9,6 +11,7 @@ export interface PackageInfo {
911
version: string | undefined
1012
rootPath: string
1113
packageJsonPath: string
14+
packageJson: PackageJson
1215
}
1316

1417
export interface CacheOptions {
@@ -22,7 +25,7 @@ export interface InternalCacheOptions extends CacheOptions {
2225
enable?: boolean
2326
}
2427

25-
export interface PatchOptions {
28+
export interface PatchOptions extends PatchUserConfig {
2629
overwrite?: boolean
2730
paths?: string[]
2831
basedir?: string

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,25 @@ describe('cache', () => {
3030
expect(fs.existsSync(dir)).toBe(false)
3131
})
3232

33-
it('write and read cache default option', () => {
33+
it('write and read cache default option', async () => {
3434
// const opt = getCacheOptions()
3535
const opt = {
3636
dir: path.resolve(__dirname, 'fixtures/cache'),
3737
file: 'raw-method.json',
3838
}
3939
cm = new CacheManager(opt)
4040
let cache: Set<string> | undefined
41-
cache = cm.read()
41+
cache = await cm.read()
4242
// expect(cache).toBe(undefined)
43-
cm.write(new Set(['a', 'b', 'c']))
44-
cache = cm.read()
43+
await cm.write(new Set(['a', 'b', 'c']))
44+
cache = await cm.read()
4545
expect(cache).toBeDefined()
4646
if (cache) {
4747
expect(cache.size).toBe(3)
4848
}
4949
})
5050

51-
it('read broken cache', () => {
51+
it('read broken cache', async () => {
5252
// const opt = getCacheOptions()
5353

5454
const dir = path.resolve(__dirname, './fixtures', `${pkgName}-broken`)
@@ -65,7 +65,7 @@ describe('cache', () => {
6565
dir,
6666
})
6767
expect(fs.existsSync(filepath)).toBe(true)
68-
const cache = cm.read()
68+
const cache = await cm.read()
6969
expect(cache).toBe(undefined)
7070
expect(fs.existsSync(filepath)).toBe(false)
7171
})
@@ -78,12 +78,14 @@ describe('cache', () => {
7878
file: 'merge-multiple-context.json',
7979
},
8080
})
81-
twPatcher.setCache(new Set())
81+
await twPatcher.setCache(new Set())
8282
await getCss(['text-[100px]'])
8383
let ctxs = twPatcher.getContexts()
8484
expect(ctxs.length).toBe(1)
8585
let set = await twPatcher.getClassSet({
86-
removeUniversalSelector: false,
86+
output: {
87+
removeUniversalSelector: false,
88+
},
8789
})
8890
expect(set.size).toBeGreaterThan(0)
8991
expect(set.size).toBe(2)
Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1 @@
1-
[
2-
"*",
3-
"bg-[#123456]",
4-
"font-bold",
5-
"text-3xl",
6-
"underline"
7-
]
1+
["*","bg-[#123456]","font-bold","text-3xl","underline"]
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
[
2-
"*",
3-
"text-[99px]",
4-
"text-[100px]"
5-
]
1+
["*","text-[99px]","text-[100px]"]
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
[
2-
"a",
3-
"b",
4-
"c"
5-
]
1+
["a","b","c"]

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ describe('class', () => {
1212
const ctxs = twPatcher.getContexts()
1313
expect(ctxs.length).toBe(1)
1414
const set = await twPatcher.getClassSet({
15-
removeUniversalSelector: false,
15+
output: {
16+
removeUniversalSelector: false,
17+
},
1618
})
1719
expect(set.size).toBeGreaterThan(0)
1820
expect(set.size).toBe(4)
@@ -23,21 +25,24 @@ describe('class', () => {
2325
const twPatcher = new TailwindcssPatcher({
2426
cache: {
2527
dir,
28+
strategy: 'overwrite',
29+
},
30+
patch: {
31+
output: {
32+
removeUniversalSelector: false,
33+
},
2634
},
2735
})
28-
const res = twPatcher.getCache()
36+
const res = await twPatcher.getCache()
2937
expect(res instanceof Set).toBe(true)
3038
expect(res?.size).toBe(5)
31-
const p = twPatcher.setCache(new Set(['*', 'bg-[#123456]', 'font-bold', 'text-3xl', 'underline']))
39+
const p = await twPatcher.setCache(new Set(['*', 'bg-[#123456]', 'font-bold', 'text-3xl', 'underline']))
3240
expect(p).toBe(path.resolve(dir, 'index.json'))
3341
twPatcher.patch()
3442
await getCss([getTestCase('hello-world.html'), getTestCase('hello-world.js')])
3543
const ctxs = twPatcher.getContexts()
3644
expect(ctxs.length).toBe(1)
37-
const set = await twPatcher.getClassSet({
38-
cacheStrategy: 'overwrite',
39-
removeUniversalSelector: false,
40-
})
45+
const set = await twPatcher.getClassSet()
4146
expect(set.size).toBeGreaterThan(0)
4247
expect(set.size).toBe(5)
4348
})

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

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,24 @@ describe('v4', () => {
3838
})
3939

4040
it('tailwindcssPatcher getClassSet case 2', async () => {
41-
const patcher = new TailwindcssPatcher()
42-
patcher.majorVersion = 4
43-
const candidates = await patcher.getClassSet({
44-
tailwindcss: {
45-
v4: {
46-
base: import.meta.dirname,
47-
css: await fs.readFile(path.resolve(import.meta.dirname, './fixtures/v4/index.css'), 'utf8'),
48-
sources: [
49-
{
50-
base: import.meta.dirname,
51-
pattern: path.resolve(import.meta.dirname, './fixtures/v4/**/*.html'),
52-
},
53-
],
41+
const patcher = new TailwindcssPatcher({
42+
patch: {
43+
tailwindcss: {
44+
v4: {
45+
base: import.meta.dirname,
46+
css: await fs.readFile(path.resolve(import.meta.dirname, './fixtures/v4/index.css'), 'utf8'),
47+
sources: [
48+
{
49+
base: import.meta.dirname,
50+
pattern: path.resolve(import.meta.dirname, './fixtures/v4/**/*.html'),
51+
},
52+
],
53+
},
5454
},
5555
},
56-
5756
})
57+
patcher.majorVersion = 4
58+
const candidates = await patcher.getClassSet()
5859
expect(candidates).toMatchSnapshot()
5960
})
6061

0 commit comments

Comments
 (0)