Skip to content

Commit aad6b28

Browse files
committed
chore: upgrade docs
1 parent 9c05097 commit aad6b28

12 files changed

+400
-115
lines changed

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"@commitlint/config-conventional": "^19.8.0",
3939
"@commitlint/prompt-cli": "^19.8.0",
4040
"@commitlint/types": "^19.8.0",
41-
"@icebreakers/eslint-config": "^1.0.3",
41+
"@icebreakers/eslint-config": "^1.0.4",
4242
"@icebreakers/monorepo": "^0.7.8",
4343
"@icebreakers/stylelint-config": "^1.0.0",
4444
"@rollup/pluginutils": "^5.1.4",
@@ -70,9 +70,11 @@
7070
"dedent": "^1.5.3",
7171
"defu": "^6.1.4",
7272
"del": "^8.0.0",
73+
"dotenv": "^16.4.7",
7374
"eslint": "^9.22.0",
7475
"execa": "^9.5.2",
7576
"fast-glob": "^3.3.3",
77+
"fdir": "^6.4.3",
7678
"fs-extra": "^11.3.0",
7779
"get-value": "^4.0.1",
7880
"html-minifier-terser": "^7.2.0",
@@ -87,7 +89,9 @@
8789
"mini-css-extract-plugin": "^2.9.2",
8890
"normalize-newline": "^4.1.0",
8991
"only-allow": "^1.2.1",
92+
"openai": "^4.87.3",
9093
"pathe": "^2.0.3",
94+
"picomatch": "^4.0.2",
9195
"pkg-types": "^2.1.0",
9296
"postcss": "^8.5.3",
9397
"postcss-loader": "^8.1.1",

pnpm-lock.yaml

Lines changed: 196 additions & 53 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pnpm-workspace.yaml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
packages:
2-
# - 'apps/*'
3-
- 'packages/*'
4-
- 'scripts/*'
2+
- packages/*
3+
- scripts/*
54
- website
5+
onlyBuiltDependencies:
6+
- esbuild

website/.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
public/_pagefind
1+
public/_pagefind
2+
.env

website/content/en/config.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
# TODO
1+
# 待办事项

website/content/en/index.mdx

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
1-
21
import { Callout } from 'nextra/components'
32

43
# Welcome to Tailwindcss-mangle
54

65
## What is tailwindcss-mangle?
76

8-
This is a project for compressing and obfuscating the class names generated by `tailwindcss`.
7+
This is a project designed to compress and obfuscate class names generated by `tailwindcss`.
98

10-
The purpose of creating this library is to prevent easy copying and plagiarism of pages developed using `tailwindcss`.
9+
The purpose of creating this library is that the pages we develop using `tailwindcss` are too easily copied and plagiarized.
1110

12-
By using `tailwindcss-mangle`, it adds a bit of "cost" for potential copycats.
11+
Using `tailwindcss-mangle` can add a little cost to those who attempt to copy.
1312

1413
## How to use?
1514

16-
Currently, the usage process is mainly divided into 2 steps:
15+
Currently, the usage is mainly divided into `2` steps:
1716

1817
1. `Patch`
1918
2. `Mangle`
2019

2120
## Patch
2221

23-
This step is used to apply a patch to `tailwindcss` that exposes the context, so it can extract the `Token` it uses internally.
22+
This step is used to patch `tailwindcss` to expose its context, thereby extracting the `Token` it generates from its internals.
2423

25-
Eventually, it exports these as a `Json` file.
24+
Finally, it exports this into a `Json` file.
2625

2726
## Mangle
2827

29-
This step reads the `Json` exported from `Patch` and escapes all the string literals and template strings corresponding to the `Token`.
28+
Read the `Json` exported by `Patch`, and escape all string literals and template strings corresponding to the `Token` inside.

website/content/en/mangle.mdx

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,59 @@
11
import { Callout } from 'nextra/components'
22

3-
# 2. Mangle
3+
# 2.Mangle
44

5-
## Feature Overview
5+
6+
## Introduction
67

78
```html
8-
<!-- Before -->
9+
<!-- Before modification -->
910
<div class="z-10 w-full max-w-5xl items-center justify-between font-mono text-sm lg:flex"></div>
10-
<!-- After -->
11+
<!-- After modification -->
1112
<div class="tw-g tw-h tw-i tw-d tw-e tw-j tw-k tw-l"></div>
1213
```
1314

1415
## Usage
1516

16-
### 1. Install the package
17+
### 1. Install Packages
1718

18-
```sh
19+
```sh npm2yarn
1920
npm i -D unplugin-tailwindcss-mangle tailwindcss-patch
2021
```
2122

22-
### 2. Run the installation script
23+
### 2. Run the Installation Script
2324

2425
```sh
2526
npx tw-patch install
2627
```
2728

28-
### 3. Add the `prepare` script in `package.json`
29+
### 3. Add `prepare` Script in `package.json`
2930

3031
```json
3132
"scripts": {
3233
"prepare": "tw-patch install"
3334
},
3435
```
3536

36-
### 4. Run the extraction command
37+
### 4. Run the Extraction Command
3738

38-
Navigate to the same directory as your `package.json` and `tailwind.config.js`, then run:
39+
Navigate to the directory containing `package.json` and `tailwind.config.js`, then run:
3940

4041
```sh
4142
npx tw-patch extract
4243
```
4344

44-
> See more options in [tailwindcss-patch](https://github.com/sonofmagic/tailwindcss-mangle/tree/main/packages/tailwindcss-patch)
45+
> Check out more options in [tailwindcss-patch](https://github.com/sonofmagic/tailwindcss-mangle/tree/main/packages/tailwindcss-patch)
4546
4647
This will generate a JSON file: `.tw-patch/tw-class-list.json`
4748

48-
### 5. Register this plugin
49+
### 5. Register the Plugin
4950

5051
#### vite
5152

5253
```js
5354
import vue from '@vitejs/plugin-vue'
5455
import utwm from 'unplugin-tailwindcss-mangle/vite'
55-
// For example: vue vite project
56+
// For example: a vue vite project
5657
import { defineConfig } from 'vite'
5758
// https://vitejs.dev/config/
5859
export default defineConfig({
@@ -63,23 +64,23 @@ export default defineConfig({
6364
Then run the script:
6465

6566
```sh
66-
# Build the package
67+
# Generate build package
6768
yarn build
6869
# Preview
6970
yarn preview
7071
```
7172

72-
You will see that all class names are renamed to `tw-*`
73+
You will see all class names renamed to `tw-*`
7374

7475
#### webpack
7576

7677
```js
7778
// esm
7879
import { webpackPlugin as utwm } from 'unplugin-tailwindcss-mangle'
79-
// Or cjs
80+
// or cjs
8081
const utwm = require('unplugin-tailwindcss-mangle/webpack')
8182
// Use this webpack plugin
82-
// For example in next.config.js
83+
// For example next.config.js
8384
const { defineConfig } = require('@vue/cli-service')
8485
// vue.config.js
8586
module.exports = defineConfig({
@@ -90,24 +91,25 @@ module.exports = defineConfig({
9091
}
9192
}
9293
})
94+
9395
```
9496

9597
## Notes
9698

97-
This plugin only transforms utility class names that contain `-` or `:`, such as `w-32`, `before:h-[300px]`, `after:dark:via-[#0141ff]/40`. Some class names like `flex`, `relative` will not be transformed.
99+
This plugin will only transform utility classes containing `-` or `:`, such as `w-32`, `before:h-[300px]`, `after:dark:via-[#0141ff]/40`. Some class names like `flex`, `relative` will not be transformed.
98100

99-
The plugin **traverses** all `html class` attributes and `js` string literals to find the utility classes generated by `tailwindcss`.
101+
Because the plugin will **traverse** all `html class` attributes and class names in `js` literals, looking for utility classes generated by `tailwindcss`.
100102

101-
Transforming string literals like the following in `js` is dangerous:
103+
Transforming `js` literals like the following is dangerous:
102104

103105
```js
104106
const innerHTML = 'i\'m flex and relative and grid'
105107
document.body.innerHTML = innerHTML
106108
```
107109

108-
Thus, only strings containing `-` or `:` will be transformed.
110+
Therefore, only strings containing `-` or `:` will be transformed.
109111

110-
Additionally, the plugin provides the `twIgnore` option, which allows some strings to be skipped during the `Mangle`.
112+
Additionally, the plugin provides an option `twIgnore` to skip `Mangle` for certain strings.
111113

112114
For example:
113115

@@ -123,4 +125,4 @@ const className = `${twIgnore`gap-y-4`} bg-zinc-800/30`;
123125
```js
124126
const twIgnore = String.raw;
125127
const className = `${twIgnore`gap-y-4`} tw-a`;
126-
```
128+
```

website/content/en/patch.mdx

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
# 1. Patch
22

3-
This step is to export the context extracted from your `tailwindcss` as a `Json` file.
3+
This step is to export the context extracted from your `tailwindcss` into a `Json` file.
44

55
## Installation
66

7-
Install using your preferred package manager:
7+
Choose your preferred package manager for installation.
88

9-
```sh
9+
```sh npm2yarn
1010
npm i -D tailwindcss-patch
1111
```
1212

13-
### Apply the Patch
13+
### Patching
1414

15-
1. Apply the patch to `tailwindcss`
15+
1. Patch `tailwindcss`
1616

17-
```sh
17+
```sh npm2yarn
1818
npx tw-patch install
1919
```
2020

2121
### Register npm hook
2222

23-
1. Add the following command to the `prepare` hook in your `npm` configuration.
23+
1. Add the command to the `prepare` hook in `npm`
2424

2525
`package.json`
2626

@@ -35,25 +35,25 @@ npx tw-patch install
3535

3636
## Usage
3737

38-
### Command-line Cli
38+
### Command Line Interface (CLI)
3939

40-
#### Start Extraction
40+
#### Start Extracting
4141

42-
```sh
42+
```sh npm2yarn
4343
npx tw-patch extract
4444
```
4545

46-
By default, after a successful execution, a `json` file `.tw-patch/tw-class-list.json` will be generated in your project.
46+
By default, a `json` file `.tw-patch/tw-class-list.json` will appear in your project upon successful execution.
4747

48-
> Of course, you can customize this behavior using the configuration file `tailwindcss-mangle.config.ts`.
48+
> Of course, you can customize this behavior through the configuration file `tailwindcss-mangle.config.ts`.
4949
50-
### Nodejs API
50+
### Node.js API
5151

5252
```js
5353
import { TailwindcssPatcher } from 'tailwindcss-patch'
5454

5555
const twPatcher = new TailwindcssPatcher(/* options */)
56-
// Perform the patch action
56+
// Perform patch action
5757
twPatcher.patch()
5858
// Get all contexts at runtime
5959
twPatcher.getContexts()
@@ -63,25 +63,25 @@ twPatcher.getClassSet()
6363

6464
The core of this phase is to obtain the `.tw-patch/tw-class-list.json` file.
6565

66-
Once it is generated, you can start the `Mangle` process.
66+
Once it is generated, we can proceed with our `Mangle` process.
6767

6868
## Configuration
6969

7070
### Initialization
7171

72-
```sh
72+
```sh npm2yarn
7373
npx tw-patch init
7474
```
7575

76-
This will create a `tailwindcss-mangle.config.ts` file in your current `cwd`:
76+
This will create a `tailwindcss-mangle.config.ts` file in your current working directory (`cwd`):
7777

7878
```ts
7979
import { defineConfig } from 'tailwindcss-patch'
8080

8181
export default defineConfig({})
8282
```
8383

84-
You can customize its behavior using the `patch` field:
84+
You can customize its behavior through the `patch` field:
8585

8686
```ts
8787
import { defineConfig } from 'tailwindcss-patch'

website/content/en/recommend.mdx

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Need to Extract More Metadata When Using Tailwindcss
2+
3+
## Need More Information
4+
5+
Currently, when `tailwindcss` extracts `Candidate` from files/content, it only extracts some string information and does not record the location.
6+
7+
```html
8+
<div class="flex bg-slate-500/25" />
9+
↑ ↑ ↑ ↑
10+
token1 token2
11+
```
12+
13+
However, this makes it difficult for us to perform a project-level overall analysis.
14+
15+
We cannot know how many tokens are extracted from a file, nor do we know in which files the extracted tokens exist (the relationship between files and tokens is usually many-to-many).
16+
17+
Moreover, if there is a need to modify the generated product from the original source code later, we need to know the original position where the token was extracted.
18+
19+
For example, transforming the extracted token string into an object like the one below:
20+
21+
```ts
22+
interface Candidate {
23+
value: string
24+
file: string
25+
start: number
26+
end: number
27+
}
28+
```
29+
30+
Here, `value` is the original string value, `file` is the file from which it was extracted, and `start` and `end` represent the position of this `token` in the file.
31+
32+
This way, we can make precise modifications, such as:
33+
34+
```diff filename="diff"
35+
// before
36+
- const className = `gap-y-4 bg-zinc-800/30`;
37+
// after
38+
+ const className = `a b`;
39+
```
40+
41+
Otherwise, currently, to make modifications, we have to apply the entire `Candidates` (`Set<string>`) to all files for replacement, which often leads to many unintended changes.
42+
43+
With this, we only need to find the corresponding file and modify the nodes corresponding to its `start` and `end` to achieve the goal.
44+
45+
In fact, this functionality ultimately resembles the `compile-class` feature of `unocss` [compile-class](https://unocss.dev/transformers/compile-class).
46+
47+
## Tailwindcss v4
48+
49+
https://github.com/tailwindlabs/tailwindcss/blob/437579d3f010a98ae4b824a7d5694486119fbdcd/crates/oxide/src/lib.rs#L108
50+
51+
```rs
52+
pub fn scan(&mut self) -> Vec<String> {
53+
init_tracing();
54+
self.prepare();
55+
self.check_for_new_files();
56+
self.compute_candidates();
57+
58+
let mut candidates: Vec<String> = self.candidates.clone().into_iter().collect();
59+
60+
candidates.sort();
61+
62+
candidates
63+
}
64+
```

0 commit comments

Comments
 (0)