Skip to content

Commit 5bd5c06

Browse files
committed
chore: add getClassSet
1 parent 3092d4c commit 5bd5c06

File tree

8 files changed

+90
-121
lines changed

8 files changed

+90
-121
lines changed

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

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { GlobEntry } from '@tailwindcss/oxide'
22
import process from 'node:process'
3-
import { defu } from '@tailwindcss-mangle/shared'
3+
import { defuOverrideArray } from '@tailwindcss-mangle/shared'
44

55
function importNode() {
66
return import('@tailwindcss/node')
@@ -44,31 +44,35 @@ export async function extractRawCandidates(
4444
}
4545

4646
export interface ExtractValidCandidatesOption {
47-
content?: string
48-
base?: string
49-
css?: string
47+
sources: GlobEntry[]
48+
base: string
49+
css: string
5050
}
5151

52-
export async function extractValidCandidates(options: ExtractValidCandidatesOption) {
53-
const { content, base, css } = defu<
52+
export async function extractValidCandidates(options?: Partial<ExtractValidCandidatesOption>) {
53+
const cwd = process.cwd()
54+
const { sources, base, css } = defuOverrideArray<
5455
Required<ExtractValidCandidatesOption>,
5556
Partial<ExtractValidCandidatesOption>[]
56-
>(options, {
57-
css: '@import "tailwindcss";',
58-
base: process.cwd(),
59-
content: '**/*',
60-
})
57+
>(
58+
// @ts-ignore
59+
options,
60+
{
61+
css: '@import "tailwindcss";',
62+
base: cwd,
63+
sources: [
64+
{
65+
base: cwd,
66+
pattern: '**/*',
67+
},
68+
],
69+
},
70+
)
6171

62-
// const { __unstable__loadDesignSystem } = await importTailwindcss()
6372
const { __unstable__loadDesignSystem } = await importNode()
6473
const designSystem = await __unstable__loadDesignSystem(css, { base })
6574

66-
const candidates = await extractRawCandidates([
67-
{
68-
base,
69-
pattern: content,
70-
},
71-
])
75+
const candidates = await extractRawCandidates(sources)
7276
const validCandidates = candidates.filter(
7377
rawCandidate => designSystem.parseCandidate(rawCandidate).length > 0,
7478
)

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

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { UserConfig } from '../config'
22
import type { CacheStrategy, InternalCacheOptions, InternalPatchOptions, PackageInfo, TailwindcssClassCache, TailwindcssPatcherOptions, TailwindcssRuntimeContext } from '../types'
3+
import type { ExtractValidCandidatesOption } from './candidates'
34
import { createRequire } from 'node:module'
45
import fs from 'fs-extra'
56
import { getPackageInfoSync } from 'local-pkg'
@@ -88,28 +89,42 @@ export class TailwindcssPatcher {
8889
return contexts.filter(x => isObject(x)).map(x => x.classCache)
8990
}
9091

91-
getClassCacheSet(options?: { removeUniversalSelector?: boolean }): Set<string> {
92-
const classCaches = this.getClassCaches()
92+
async getClassCacheSet(options?: { removeUniversalSelector?: boolean } & Partial<ExtractValidCandidatesOption>): Promise<Set<string>> {
9393
const classSet = new Set<string>()
94-
for (const classCacheMap of classCaches) {
95-
const keys = classCacheMap.keys()
96-
for (const key of keys) {
97-
const v = key.toString()
98-
if (options?.removeUniversalSelector && v === '*') {
99-
continue
94+
if (this.majorVersion === 4) {
95+
const candidates = await extractValidCandidates({
96+
base: options?.base,
97+
css: options?.css,
98+
sources: options?.sources,
99+
})
100+
for (const candidate of candidates) {
101+
classSet.add(candidate)
102+
}
103+
}
104+
else {
105+
const classCaches = this.getClassCaches()
106+
107+
for (const classCacheMap of classCaches) {
108+
const keys = classCacheMap.keys()
109+
for (const key of keys) {
110+
const v = key.toString()
111+
if (options?.removeUniversalSelector && v === '*') {
112+
continue
113+
}
114+
classSet.add(v)
100115
}
101-
classSet.add(v)
102116
}
103117
}
118+
104119
return classSet
105120
}
106121

107122
/**
108123
* @description 在多个 tailwindcss 上下文时,这个方法将被执行多次,所以策略上应该使用 append
109124
*/
110-
getClassSet(options?: { cacheStrategy?: CacheStrategy, removeUniversalSelector?: boolean }) {
125+
async getClassSet(options?: { cacheStrategy?: CacheStrategy, removeUniversalSelector?: boolean }) {
111126
const { cacheStrategy = this.cacheOptions.strategy ?? 'merge', removeUniversalSelector = true } = options ?? {}
112-
const set = this.getClassCacheSet({
127+
const set = await this.getClassCacheSet({
113128
removeUniversalSelector,
114129
})
115130
if (cacheStrategy === 'overwrite') {
@@ -133,12 +148,14 @@ export class TailwindcssPatcher {
133148
if (output && tailwindcss) {
134149
const { removeUniversalSelector, filename, loose } = output
135150

136-
await processTailwindcss({
137-
...tailwindcss,
138-
majorVersion: this.majorVersion,
139-
})
151+
if (this.majorVersion === 3) {
152+
await processTailwindcss({
153+
...tailwindcss,
154+
majorVersion: this.majorVersion,
155+
})
156+
}
140157

141-
const set = this.getClassSet({
158+
const set = await this.getClassSet({
142159
removeUniversalSelector,
143160
})
144161
if (filename) {

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

Lines changed: 4 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -27,88 +27,15 @@ exports[`v4 > extractRawCandidates case 0 1`] = `
2727

2828
exports[`v4 > tailwindcssPatcher casae 1 1`] = `
2929
[
30-
"![color:red]/50",
31-
"!bg-[#0088cc]",
32-
"!bg-red-500",
33-
"!underline",
34-
"-inset-full",
35-
"[color:red]",
36-
"[color:red]/50",
37-
"[color:red]/50!",
38-
"[color:red]/[0.5]",
39-
"[color:red]/[0.5]!",
40-
"basis-[32rpx]",
41-
"bg-(--spacing-0_5,var(--spacing-1_5,3rem))",
42-
"bg-[#0088cc]!",
43-
"bg-[#0088cc]/50",
44-
"bg-[#0088cc]/[0.5]",
4530
"bg-[#123456]",
46-
"bg-[#2f2f2f]",
47-
"bg-[#7d7ac2]",
48-
"bg-[-1px_-1px]",
49-
"bg-[no-repeat_url(/image_13.png)]",
50-
"bg-[url('https://xxx.webp')]",
51-
"bg-[var(--spacing)-1px]",
52-
"bg-[var(--spacing)_-_1px]",
53-
"bg-[var(--spacing-0_5,_var(--spacing-1_5,_3rem))]",
54-
"bg-blue-500",
55-
"bg-red-500",
56-
"bg-red-500!",
57-
"bg-red-500/50",
58-
"bg-red-500/[0.5]",
59-
"bottom",
60-
"box-border",
31+
"bg-gradient-to-b",
6132
"content",
62-
"content-["hello_world"]",
63-
"content-[____"hello_world"___]",
64-
"end",
65-
"filter",
66-
"flex",
67-
"font-bold",
68-
"font-palette",
69-
"from",
70-
"h-[12rpx]",
33+
"from-[#2f73f1]",
7134
"h-[30px]",
72-
"h-[45px]",
73-
"hover:focus:text-white",
74-
"left",
75-
"list",
76-
"m-[23.43rpx]",
77-
"max-h-[12rpx]",
78-
"max-w-[12rpx]",
79-
"min-h-[12rpx]",
80-
"min-w-[12rpx]",
81-
"object-hash",
82-
"order",
83-
"p",
84-
"p-[0.32rpx]",
85-
"p-[calc((100vw-theme(maxWidth.2xl))/2)]",
86-
"p-[calc((100vw-theme(maxWidth.2xl))_/_2)]",
87-
"p-[round(to-zero,1px)]",
88-
"placeholder",
89-
"pt",
90-
"px",
91-
"right",
92-
"shadow",
93-
"size",
94-
"space-y-[12.0rpx]",
95-
"start",
96-
"static",
9735
"text-(--x)",
98-
"text-3xl",
99-
"text-[#123456]",
100-
"text-[100px]",
101-
"text-[99px]",
102-
"text-[length:32rpx]",
103-
"text-white",
10436
"text-xs",
105-
"to",
106-
"top",
107-
"transform",
108-
"underline",
109-
"underline!",
110-
"w-1/2",
111-
"w-[12rpx]",
37+
"to-[#4bcefd]",
38+
"w-[323px]",
11239
]
11340
`;
11441

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ describe('cache', () => {
8282
await getCss(['text-[100px]'])
8383
let ctxs = twPatcher.getContexts()
8484
expect(ctxs.length).toBe(1)
85-
let set = twPatcher.getClassSet({
85+
let set = await twPatcher.getClassSet({
8686
removeUniversalSelector: false,
8787
})
8888
expect(set.size).toBeGreaterThan(0)
@@ -94,7 +94,7 @@ describe('cache', () => {
9494
await getCss(['text-[99px]'])
9595
ctxs = twPatcher.getContexts()
9696
expect(ctxs.length).toBe(1)
97-
set = twPatcher.getClassSet()
97+
set = await twPatcher.getClassSet()
9898
expect(set.size).toBeGreaterThan(0)
9999
expect(set.size).toBe(3)
100100
expect(set.has('text-[99px]')).toBe(true)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Document</title>
7+
</head>
8+
<body>
9+
<div class="bg-gradient-to-b from-[#2f73f1] to-[#4bcefd] w-[323px] h-[30px]"></div>
10+
</body>
11+
</html>

packages/tailwindcss-patch/test/postcss8-v3.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe('postcss', () => {
1818
expect(res.css).toMatchSnapshot()
1919
const res0 = twPatcher.getContexts()
2020
expect(res0.length).toBe(1)
21-
const set = twPatcher.getClassSet({
21+
const set = await twPatcher.getClassSet({
2222
removeUniversalSelector: false,
2323
})
2424
expect(set.size).toBe(4)

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe('class', () => {
1111
await getCss([getTestCase('hello-world.html')])
1212
const ctxs = twPatcher.getContexts()
1313
expect(ctxs.length).toBe(1)
14-
const set = twPatcher.getClassSet({
14+
const set = await twPatcher.getClassSet({
1515
removeUniversalSelector: false,
1616
})
1717
expect(set.size).toBeGreaterThan(0)
@@ -34,7 +34,7 @@ describe('class', () => {
3434
await getCss([getTestCase('hello-world.html'), getTestCase('hello-world.js')])
3535
const ctxs = twPatcher.getContexts()
3636
expect(ctxs.length).toBe(1)
37-
const set = twPatcher.getClassSet({
37+
const set = await twPatcher.getClassSet({
3838
cacheStrategy: 'overwrite',
3939
removeUniversalSelector: false,
4040
})
@@ -47,7 +47,7 @@ describe('class', () => {
4747
await getCss(['text-[100px]'])
4848
let ctxs = twPatcher.getContexts()
4949
expect(ctxs.length).toBe(1)
50-
let set = twPatcher.getClassSet({
50+
let set = await twPatcher.getClassSet({
5151
removeUniversalSelector: false,
5252
})
5353
expect(set.size).toBeGreaterThan(0)
@@ -59,7 +59,7 @@ describe('class', () => {
5959
await getCss(['text-[99px]'])
6060
ctxs = twPatcher.getContexts()
6161
expect(ctxs.length).toBe(1)
62-
set = twPatcher.getClassSet({
62+
set = await twPatcher.getClassSet({
6363
removeUniversalSelector: false,
6464
})
6565
expect(set.size).toBeGreaterThan(0)
@@ -75,7 +75,7 @@ describe('class', () => {
7575
await getCss([`<view class="bg-[#7d7ac2] text-[100px] text-[#123456] {{true?'h-[30px]':'h-[45px]'}}">111</view>`])
7676
const ctxs = twPatcher.getContexts()
7777
expect(ctxs.length).toBe(1)
78-
const set = twPatcher.getClassSet({
78+
const set = await twPatcher.getClassSet({
7979
removeUniversalSelector: false,
8080
})
8181
expect(set.size).toBeGreaterThan(0)

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ describe('v4', () => {
1111
const candidates = await patcher.extractValidCandidates({
1212
base: import.meta.dirname,
1313
css: await fs.readFile(path.resolve(import.meta.dirname, './fixtures/v4/index.css'), 'utf8'),
14-
content: path.resolve(import.meta.dirname, './fixtures/v4/index.html'),
14+
sources: [
15+
{
16+
base: import.meta.dirname,
17+
pattern: path.resolve(import.meta.dirname, './fixtures/v4/index.html'),
18+
},
19+
],
1520
})
1621
expect(candidates).toMatchSnapshot()
1722
})
@@ -22,7 +27,12 @@ describe('v4', () => {
2227
const candidates = await patcher.extractValidCandidates({
2328
base: import.meta.dirname,
2429
css: await fs.readFile(path.resolve(import.meta.dirname, './fixtures/v4/index.css'), 'utf8'),
25-
30+
sources: [
31+
{
32+
base: import.meta.dirname,
33+
pattern: path.resolve(import.meta.dirname, './fixtures/v4/**/*.html'),
34+
},
35+
],
2636
})
2737
expect(candidates).toMatchSnapshot()
2838
})

0 commit comments

Comments
 (0)