Skip to content

Commit 6cd1185

Browse files
committed
docs: update
1 parent e5c98b8 commit 6cd1185

File tree

10 files changed

+807
-356
lines changed

10 files changed

+807
-356
lines changed

website/content/en/config.mdx

Lines changed: 128 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,128 @@
1-
# 待办事项
1+
import { Callout } from 'nextra/components'
2+
3+
# 3.Config
4+
5+
The toolchain reads configuration through `@tailwindcss-mangle/config`. Both `tailwindcss-patch.config.ts` and the historic `tailwindcss-mangle.config.ts` are recognised, so you can migrate gradually without breaking existing projects.
6+
7+
## Generate a starter file
8+
9+
```sh npm2yarn
10+
npx tw-patch init
11+
```
12+
13+
This command creates a minimal config at the project root:
14+
15+
```ts
16+
import { defineConfig } from 'tailwindcss-patch'
17+
18+
export default defineConfig({})
19+
```
20+
21+
<Callout type="info">
22+
`defineConfig` provides typed completion, but the runtime accepts additional fields that map to the modern `TailwindcssPatcher` options. Unknown keys are forwarded during normalisation.
23+
</Callout>
24+
25+
## Example configuration
26+
27+
```ts
28+
import { defineConfig } from 'tailwindcss-patch'
29+
30+
export default defineConfig({
31+
patch: {
32+
output: {
33+
filename: '.tw-patch/tw-class-list.json',
34+
format: 'json',
35+
pretty: 2,
36+
removeUniversalSelector: true,
37+
},
38+
tailwindcss: {
39+
version: 4,
40+
v4: {
41+
cssEntries: ['dist/tailwind.css'],
42+
sources: [
43+
{ base: 'src', pattern: '**/*.{astro,tsx,mdx}', negated: false },
44+
],
45+
},
46+
},
47+
applyPatches: {
48+
exportContext: true,
49+
extendLengthUnits: {
50+
units: ['rpx'],
51+
},
52+
},
53+
},
54+
cache: {
55+
enabled: true,
56+
dir: '.tw-patch/cache',
57+
strategy: 'merge',
58+
},
59+
mangle: {
60+
classListPath: '.tw-patch/tw-class-list.json',
61+
include: [
62+
/\.(html|vue|tsx|jsx|mdx)$/,
63+
],
64+
classGenerator: {
65+
classPrefix: 'tw-',
66+
reserveClassName: [/^theme-/],
67+
},
68+
classMapOutput: {
69+
enable: false,
70+
filename: '.tw-patch/tw-map-list.json',
71+
loose: true,
72+
},
73+
preserveFunction: ['twIgnore'],
74+
},
75+
})
76+
```
77+
78+
## Patch options
79+
80+
The `patch` block keeps the legacy shape but is converted to the new `TailwindcssPatchOptions` internally.
81+
82+
### `output`
83+
84+
- `filename` – destination for the extracted inventory (default `.tw-patch/tw-class-list.json`).
85+
- `format` – either `json` or `lines`; the CLI uses JSON by default.
86+
- `pretty``false`, a boolean, or a number of spaces when writing JSON.
87+
- `removeUniversalSelector` – skip `*` from the generated list when `true`.
88+
89+
### `tailwindcss`
90+
91+
- `version` – optional hint (`2`, `3`, or `4`) when multiple versions are installed.
92+
- `v2` / `v3` – provide `cwd` and `config` when Tailwind lives in a workspace package.
93+
- `v4.cssEntries` – CSS entry files used by the Oxide scanner. Repeat for multi-bundle setups.
94+
- `v4.sources` – custom content sources for v4 projects (defaults to `**/*` under `base`).
95+
96+
### `applyPatches`
97+
98+
- `exportContext` – expose Tailwind contexts at runtime so the API can read them back later.
99+
- `extendLengthUnits` – customise the built-in length-unit patching; accepts booleans or an option object (`units`, `overwrite`, etc.).
100+
101+
### `cache` and `overwrite`
102+
103+
- `cache.enabled` – persist collected classes under `node_modules/.cache/tailwindcss-patch` (enabled by default).
104+
- `cache.dir` / `cache.file` – control where the cache set is stored.
105+
- `cache.strategy``merge` adds to the existing cache, `overwrite` replaces it on each extraction.
106+
- `overwrite` (top-level) – reapply Tailwind patches even if a patched version already exists.
107+
108+
### `filter`
109+
110+
Provide a function to skip classes during extraction. Return `false` to drop a class; all other return values keep it.
111+
112+
## Mangle options
113+
114+
The `mangle` block is read by both the core runtime and `unplugin-tailwindcss-mangle`.
115+
116+
- `classListPath` – absolute or relative path to the extracted class list.
117+
- `include` / `exclude` – control which files the plugin scans; defaults cover HTML, frameworks, CSS, and MDX.
118+
- `mangleClassFilter` – override the class-level filter before the generator runs.
119+
- `classGenerator` – customise minified names via `classPrefix`, `reserveClassName`, `exclude`, or `customGenerate`.
120+
- `classMapOutput` – set to `true` to reuse config defaults, provide an object (`enable`, `filename`, `loose`) to emit JSON, or supply a callback for full control.
121+
- `preserveFunction` – list of helper names (e.g. `twIgnore`) that mark template literals as safe from mangling.
122+
- `disabled` – when `true`, skips rewriting (the default is `process.env.NODE_ENV === 'development'`).
123+
124+
## Using the config
125+
126+
- `tw-patch install` / `tw-patch extract` resolve this file automatically.
127+
- `unplugin-tailwindcss-mangle` mirrors the same configuration, so plugin options and CLI behaviour stay aligned.
128+
- When you need programmatic control, construct a `TailwindcssPatcher` manually and pass the modern options object—the legacy config is converted for you at runtime.

website/content/en/index.mdx

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,47 @@ import { Callout } from 'nextra/components'
22

33
# Welcome to Tailwindcss-mangle
44

5-
## What is tailwindcss-mangle?
5+
## Why tailwindcss-mangle?
66

7-
This is a project designed to compress and obfuscate class names generated by `tailwindcss`.
7+
Tailwind CSS makes it easy to build interfaces, but its utility classes are just as easy to copy. The tailwindcss-mangle toolchain patches Tailwind, extracts every generated class, and rewrites them into compact tokens so production bundles are harder to reverse engineer.
88

9-
The purpose of creating this library is that the pages we develop using `tailwindcss` are too easily copied and plagiarized.
9+
## What's new in 8.0?
1010

11-
Using `tailwindcss-mangle` can add a little cost to those who attempt to copy.
11+
- `[email protected]` ships a redesigned architecture with typed entry points, cache controls, and Tailwind v4 support.
12+
- The CLI exposes richer flags (`--output`, `--format`, `--css`, `--no-write`) so you can automate extraction workflows.
13+
- `@tailwindcss-mangle/config` keeps legacy configs working while enabling the new unified options object.
14+
- `unplugin-tailwindcss-mangle` reads the refreshed class list and supports Vite, Webpack, Rollup, Esbuild, and Nuxt 3.
1215

13-
## How to use?
16+
<Callout type="info">
17+
Upgrading from v7.x? Follow the <a href="https://github.com/icebreaker/tailwindcss-mangle/blob/main/packages/tailwindcss-patch/MIGRATION.md" target="_blank" rel="noreferrer">migration guide</a> for a painless transition.
18+
</Callout>
1419

15-
Currently, the usage is mainly divided into `2` steps:
20+
## Two-step workflow
1621

1722
1. `Patch`
1823
2. `Mangle`
1924

2025
## Patch
2126

22-
This step is used to patch `tailwindcss` to expose its context, thereby extracting the `Token` it generates from its internals.
23-
24-
Finally, it exports this into a `Json` file.
27+
Apply runtime patches to your local Tailwind installation, collect build contexts, and write a `.tw-patch/tw-class-list.json` (or newline list) that enumerates every utility class. See [Patch](./patch) for the full CLI and API reference.
2528

2629
## Mangle
2730

28-
Read the `Json` exported by `Patch`, and escape all string literals and template strings corresponding to the `Token` inside.
31+
Feed the extracted class list to `unplugin-tailwindcss-mangle`, then let the bundler rewrite matching class names during builds. The plugin keeps templates, JSX, MDX, and inlined strings in sync. Explore [Mangle](./mangle) for integration details and configuration tips.
32+
33+
## Quick start
34+
35+
```sh npm2yarn
36+
npm i -D tailwindcss-patch unplugin-tailwindcss-mangle
37+
npx tw-patch install
38+
npx tw-patch extract
39+
```
40+
41+
Then register the plugin for your bundler (e.g. `unplugin-tailwindcss-mangle/vite`) and point it at the generated `.tw-patch/tw-class-list.json`.
42+
43+
## Packages in the toolbox
44+
45+
- `tailwindcss-patch` – patches Tailwind runtimes, extracts class inventories, and exposes contexts for tooling.
46+
- `unplugin-tailwindcss-mangle` – rewrites class usage across bundlers by consuming the generated inventory.
47+
- `@tailwindcss-mangle/config` – shared configuration loader that normalises both legacy and modern option shapes.
48+
- `@tailwindcss-mangle/shared` – helpers for class generation, filtering, and shared regex utilities.

0 commit comments

Comments
 (0)