Skip to content

Commit 0e36bfe

Browse files
committed
feat!: tailwindcss-patch 8.x
1 parent bf7e426 commit 0e36bfe

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+2461
-16286
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'tailwindcss-patch': major
3+
---
4+
5+
refactor!: redesign the patcher architecture, CLI surface, and documentation while adding Tailwind CSS v4 extraction support. Legacy APIs are wrapped for compatibility but consumers should migrate to the new entry points.

packages/tailwindcss-patch/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# tailwindcss-patch
22

3+
## Unreleased
4+
5+
- refactor: rebuild around the new `TailwindcssPatcher` API, explicit module boundaries (`api/`, `patching/`, `runtime/`, `options/`, `extraction/`).
6+
- feat: add Tailwind CSS v4 class discovery plus configurable output formats (`json` or newline delimited).
7+
- feat: expose helpers such as `CacheStore`, `normalizeOptions`, and v4 candidate scanners as public exports.
8+
- docs: add `MIGRATION.md` and refresh both README variants to describe the new workflow and CLI flags.
9+
310
## 7.1.6
411

512
### Patch Changes
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# Migration Guide
2+
3+
This document describes the changes introduced by the full refactor of `tailwindcss-patch`. It helps teams upgrading from the 7.x line (or earlier) to the new architecture.
4+
5+
> TL;DR — the public API now revolves around the `TailwindcssPatcher` class, helpers live in explicit folders, and the CLI exposes clearer options. Legacy configuration objects are still accepted, but adopting the new structure unlocks additional features.
6+
7+
## 1. Package layout
8+
9+
| Before | After |
10+
| --- | --- |
11+
| `src/core/**` mix of cache, patch and runtime helpers | Dedicated folders: `api/`, `cache/`, `patching/`, `runtime/`, `options/`, `extraction/` |
12+
| `CacheManager`, `InternalPatchOptions`, ad-hoc exports | `CacheStore`, `normalizeOptions`, typed helpers with explicit imports |
13+
| `processTailwindcss` | `runTailwindBuild` in `runtime/process-tailwindcss.ts` |
14+
15+
Imports such as `@/core/patcher` or `@/core/cache` must be updated. Use the new entry-points:
16+
17+
```ts
18+
import { TailwindcssPatcher } from 'tailwindcss-patch'
19+
import { CacheStore } from 'tailwindcss-patch/cache/store'
20+
import { applyTailwindPatches } from 'tailwindcss-patch/patching/patch-runner'
21+
```
22+
23+
## 2. TailwindcssPatcher options
24+
25+
### Previous shape
26+
27+
```ts
28+
new TailwindcssPatcher({
29+
cache: { dir: '.cache', file: 'classes.json' },
30+
patch: {
31+
overwrite: true,
32+
output: { filename: '.tw-patch/tw-class-list.json', loose: true },
33+
tailwindcss: { version: 3, v3: { cwd, config } },
34+
applyPatches: { extendLengthUnits: true },
35+
},
36+
})
37+
```
38+
39+
### New shape
40+
41+
```ts
42+
new TailwindcssPatcher({
43+
overwrite: true,
44+
cache: {
45+
enabled: true,
46+
dir: '.tw-patch/cache',
47+
strategy: 'merge',
48+
},
49+
output: {
50+
file: '.tw-patch/tw-class-list.json',
51+
format: 'json',
52+
removeUniversalSelector: true,
53+
},
54+
features: {
55+
exposeContext: { refProperty: 'runtimeContexts' },
56+
extendLengthUnits: { units: ['rpx'] },
57+
},
58+
tailwind: {
59+
version: 4,
60+
v4: {
61+
base: './src',
62+
cssEntries: ['dist/tailwind.css'],
63+
},
64+
},
65+
})
66+
```
67+
68+
Both shapes are accepted. When the constructor detects `patch`/`cache` keys it automatically converts them via `fromLegacyOptions()`. This allows step-by-step migrations.
69+
70+
## 3. CLI changes
71+
72+
- `tw-patch install` still applies the runtime patch, but logging and error handling were refreshed.
73+
- `tw-patch extract` gained new flags (`--output`, `--format`, `--no-write`, `--css`).
74+
- Commands resolve configuration from `tailwindcss-patch.config.ts` via `@tailwindcss-mangle/config`. Existing configuration files continue to work without changes.
75+
76+
## 4. Cache handling
77+
78+
`CacheManager` has been replaced by `CacheStore`:
79+
80+
```ts
81+
const cache = new CacheStore(normalized.cache)
82+
await cache.write(new Set(['text-lg']))
83+
const values = await cache.read()
84+
```
85+
86+
This ensures consistent async behaviour and reliable error recovery (invalid JSON files are removed automatically).
87+
88+
## 5. Exported helpers
89+
90+
- Runtime context access now lives in `runtime/context-registry.ts` (`loadRuntimeContexts`).
91+
- Tailwind v4 candidate extraction moved to `extraction/candidate-extractor.ts`.
92+
- Custom unit support is available from `patching/operations/extend-length-units.ts`.
93+
94+
Update imports accordingly when consuming these helpers directly.
95+
96+
## 6. Configuration advice
97+
98+
`defineConfig` from `tailwindcss-patch` (provided by `@tailwindcss-mangle/config`) still emits the legacy `patch` object. All new fields—`output.format`, extended `tailwindcss.v4` options, `applyPatches.extendLengthUnits` objects—are handled transparently. You may migrate gradually by adding the new keys into the existing `patch` block.
99+
100+
If you want the new structure inside application code, prefer creating the patcher manually and pass the modern object as demonstrated above.
101+
102+
## 7. Feature highlights
103+
104+
- Tailwind v4 is supported without monkey patching. Provide CSS entries and content sources to `tailwind.v4` and call `extract()`.
105+
- Custom length units patching (`extendLengthUnits`) now supports Tailwind v3 and v4 with a single option object.
106+
- Filters are composed with the `output.removeUniversalSelector` flag so `'*'` can be kept when desired.
107+
108+
## 8. Removal summary
109+
110+
- The entire `src/core/**` directory was removed. Update imports to their new locations.
111+
- `defaults.ts`, `InternalPatchOptions`, and related helpers were removed.
112+
- Old tests referencing `CacheManager` or `processTailwindcss` no longer apply.
113+
114+
## 9. Checklist for upgrading projects
115+
116+
1. Update the dependency to the latest version of `tailwindcss-patch`.
117+
2. Review custom imports from `tailwindcss-patch/core/*` and switch to the new module paths.
118+
3. If you instantiate the patcher manually, adopt the new options object (or keep legacy options temporarily).
119+
4. Refresh CLI usage in scripts (e.g. add `--output` or `--no-write` where appropriate).
120+
5. For Tailwind v4 projects, configure `tailwind.v4.cssEntries` and `sources` so that `extract()` can discover candidates.
121+
6. Run your extraction workflow and ensure the generated class list matches expectations.
122+
123+
For any regressions or gaps discovered during migration, please open an issue with reproduction details so we can iterate quickly.
Lines changed: 90 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,116 +1,137 @@
1-
# 使用 `tailwindcss-patch@2` 来提取你的类名吧
1+
# tailwindcss-patch
22

3-
- [使用 `tailwindcss-patch@2` 来提取你的类名吧](#使用-tailwindcss-patch2-来提取你的类名吧)
4-
- [安装](#安装)
5-
- [使用方式](#使用方式)
6-
- [命令行 Cli](#命令行-cli)
7-
- [开始提取吧](#开始提取吧)
8-
- [Nodejs API 的方式来使用](#nodejs-api-的方式来使用)
9-
- [配置](#配置)
10-
- [初始化](#初始化)
11-
- [What's next?](#whats-next)
3+
重新设计的 Tailwind CSS 补丁工具,用于导出运行时上下文、扫描 v4 的类候选、并在同一个入口管理缓存与输出。新的架构提供更清晰的配置体验,同时完全兼容旧版配置文件。
124

13-
`tailwindcss-patch` 是一个 `tailwindcss` 生态的扩展项目。也是 [`tailwindcss-mangle`](https://github.com/sonofmagic/tailwindcss-mangle) 项目重要的组成部分。
14-
15-
最近我发布了 `tailwindcss-patch``2.x` 版本,主要添加了一个配置文件读取和工具类名提取额功能。
16-
17-
让我们来看看怎么使用它吧。
5+
- 自动为 Tailwind v2/v3 打补丁,暴露运行时上下文,不再手动修改源码。
6+
- 针对 Tailwind v4,通过扫描 CSS 和内容源生成完整类名清单。
7+
- 支持写入 JSON 或纯文本文件,也可以仅在内存中返回结果,方便集成到自定义流程。
8+
- 集中式配置:缓存策略、过滤器、自定义长度单位都可以在一个对象里完成。
189

1910
## 安装
2011

21-
选择你喜欢的包管理器
22-
23-
```sh
24-
<yarn|npm|pnpm> add -D tailwindcss-patch
25-
```
26-
27-
1.`tailwindcss` 打补丁
28-
29-
```sh
30-
npx tw-patch install
12+
```bash
13+
pnpm add -D tailwindcss-patch
14+
pnpm dlx tw-patch install
3115
```
3216

33-
1.`npm``prepare` `hook` 里加入指令
34-
35-
`package.json`
17+
为了在安装依赖时自动保持补丁,可添加 `prepare` 脚本:
3618

3719
```json
3820
{
39-
/* ... */
4021
"scripts": {
4122
"prepare": "tw-patch install"
4223
}
4324
}
4425
```
4526

46-
## 使用方式
27+
## CLI 使用
4728

48-
### 命令行 Cli
29+
在项目根目录通过 `tw-patch`(或 `tailwindcss-patch`)运行命令:
4930

50-
#### 开始提取吧
31+
```bash
32+
# 为当前安装的 Tailwind 应用补丁
33+
pnpm dlx tw-patch install
5134

52-
```sh
53-
npx tw-patch extract
35+
# 生成类名清单到配置的输出文件
36+
pnpm dlx tw-patch extract
5437
```
5538

56-
默认情况下,执行成功后会有一个 `json` 文件 `.tw-patch/tw-class-list.json` 在你的项目中出现。
57-
58-
当然,你可以通过配置文件 `tailwindcss-mangle.config.ts` 来自定义这个行为。
39+
### `extract` 常用参数
5940

60-
### Nodejs API 的方式来使用
41+
| 参数 | 说明 |
42+
| --- | --- |
43+
| `--cwd <dir>` | 指定读取配置的工作目录。 |
44+
| `--output <file>` | 覆盖输出文件路径。 |
45+
| `--format <json|lines>` | 切换 JSON(默认)或换行分隔的纯文本。 |
46+
| `--css <file>` | 使用 Tailwind v4 时指定 CSS 入口文件。 |
47+
| `--no-write` | 仅返回结果,不落盘。 |
6148

62-
```js
63-
import { TailwindcssPatcher } from 'tailwindcss-patch'
49+
CLI 会通过 `@tailwindcss-mangle/config` 加载 `tailwindcss-patch.config.ts`。旧配置仍可使用,详情请参考 [迁移指南](./MIGRATION.md)
6450

65-
const twPatcher = new TailwindcssPatcher(/* options */)
66-
// do patch
67-
twPatcher.patch()
68-
// get all contexts at runtime
69-
twPatcher.getContexts()
70-
// get all class generated by tailwindcss utilities
71-
twPatcher.getClassSet()
72-
```
51+
## 编程接口
7352

74-
## 配置
53+
```ts
54+
import { TailwindcssPatcher } from 'tailwindcss-patch'
7555

76-
### 初始化
56+
const patcher = new TailwindcssPatcher({
57+
overwrite: true,
58+
cache: {
59+
enabled: true,
60+
dir: '.tw-patch/cache',
61+
strategy: 'merge',
62+
},
63+
output: {
64+
file: '.tw-patch/tw-class-list.json',
65+
format: 'json',
66+
},
67+
features: {
68+
exposeContext: { refProperty: 'runtimeContexts' },
69+
extendLengthUnits: {
70+
units: ['rpx'],
71+
},
72+
},
73+
tailwind: {
74+
version: 4,
75+
v4: {
76+
base: './src',
77+
cssEntries: ['dist/tailwind.css'],
78+
},
79+
},
80+
})
7781

78-
```sh
79-
npx tw-patch init
82+
await patcher.patch()
83+
const { classList, filename } = await patcher.extract()
8084
```
8185

82-
这样在你的当前的 `cwd` 中就会出现一个 `tailwindcss-mangle.config.ts` 文件:
86+
构造函数既可以接收上述新版对象,也可以传入旧的 `patch`/`cache` 结构;内部会自动完成兼容转换。
8387

84-
```ts
85-
import { defineConfig } from 'tailwindcss-patch'
88+
### 可复用工具
8689

87-
export default defineConfig({})
88-
```
90+
- `normalizeOptions`:归一化用户输入并应用默认值。
91+
- `CacheStore`:读写类名缓存,支持合并或覆盖策略。
92+
- `extractValidCandidates`:利用 Tailwind Oxide 扫描 v4 CSS 与内容源。
93+
- `runTailwindBuild`:在 v2/v3 项目中运行 Tailwind PostCSS 插件以预热上下文。
8994

90-
你可以通过 `patch` 字段来自定义它的行为:
95+
以上工具均由包入口导出,便于集成到自定义流程。
96+
97+
## 配置示例
9198

9299
```ts
100+
// tailwindcss-patch.config.ts
93101
import { defineConfig } from 'tailwindcss-patch'
94102

95103
export default defineConfig({
96104
patch: {
97105
output: {
98-
filename: 'xxx.json',
99-
loose: true,
100-
removeUniversalSelector: true
106+
filename: '.tw-patch/tw-class-list.json',
107+
removeUniversalSelector: true,
108+
format: 'json',
101109
},
102110
tailwindcss: {
103-
config: 'path/to/your-tailwind.config.js',
104-
cwd: 'project/cwd'
105-
}
106-
}
111+
version: 4,
112+
v4: {
113+
cssEntries: ['dist/tailwind.css'],
114+
sources: [
115+
{ base: 'src', pattern: '**/*.{html,tsx}', negated: false },
116+
],
117+
},
118+
},
119+
applyPatches: {
120+
exportContext: true,
121+
extendLengthUnits: {
122+
units: ['rpx'],
123+
},
124+
},
125+
},
107126
})
108127
```
109128

110-
## What's next?
129+
虽然 `defineConfig` 仍然暴露旧的字段名,但所有新增字段都会被自动识别并归一化。
130+
131+
## 迁移
111132

112-
目前我只是提取了所有的工具类,实际上可以获取 `tailwindcss` 的上下文进行分析。你可以给我提 `issue` 或者 `pr` 的方式,来为这个项目添加更多的功能,
133+
从 7.x 或更早版本升级时,请阅读 [MIGRATION.md](./MIGRATION.md) 获取模块调整和 API 变化说明。
113134

114-
当然,提取之后这个 `json` 当然也不是只是给你看看的。你可以对它进行一些分析,而我是把它作为我 `tailwindcss-mangle` 的数据文件来使用的。
135+
## License
115136

116-
`tailwindcss-mangle` 本身是一个混淆工具,用来混淆 `tailwindcss` 生成的工具类,具体的使用方式就看下篇文章吧。
137+
MIT © ice breaker

0 commit comments

Comments
 (0)