|
| 1 | +# Use `tailwindcss-patch 2.x` to extract your classNames |
| 2 | + |
| 3 | +`tailwindcss-patch` is an extension to the `tailwindcss` ecosystem. It is also an important part of the [`tailwindcss-mangle`](https://github.com/sonofmagic/tailwindcss-mangle) project. |
| 4 | + |
| 5 | +I recently released `2.x` version of `tailwindcss-patch`, which adds a configuration file reading and tool class name extraction feature. |
| 6 | + |
| 7 | +Let's see how to use it. It's fairly simple! |
| 8 | + |
| 9 | +- [Use `tailwindcss-patch 2.x` to extract your classNames](#use-tailwindcss-patch-2x-to-extract-your-classnames) |
| 10 | + - [Setup](#setup) |
| 11 | + - [Usage](#usage) |
| 12 | + - [Cli](#cli) |
| 13 | + - [Extract all class into a json](#extract-all-class-into-a-json) |
| 14 | + - [Nodejs API](#nodejs-api) |
| 15 | + - [Config](#config) |
| 16 | + - [Init Config File](#init-config-file) |
| 17 | + - [What's next?](#whats-next) |
| 18 | + |
| 19 | +> Nodejs version should >= `16.6.0` |
| 20 | +
|
| 21 | +## Setup |
| 22 | + |
| 23 | +1. Install package |
| 24 | + |
| 25 | +```sh |
| 26 | +<yarn|npm|pnpm> add -D tailwindcss-patch |
| 27 | +``` |
| 28 | + |
| 29 | +2. Patch tailwindcss |
| 30 | + |
| 31 | +```sh |
| 32 | +npx tw-patch install |
| 33 | +``` |
| 34 | + |
| 35 | +3. Add `prepare` script (keeps patch persisted after npm install) |
| 36 | + |
| 37 | +`package.json` |
| 38 | + |
| 39 | +```json |
| 40 | +{ |
| 41 | + /* ... */ |
| 42 | + "scripts": { |
| 43 | + "prepare": "tw-patch install" |
| 44 | + } |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +## Usage |
| 49 | + |
| 50 | +## Cli |
| 51 | + |
| 52 | +### Extract all class into a json |
| 53 | + |
| 54 | +```sh |
| 55 | +npx tw-patch extract |
| 56 | +``` |
| 57 | + |
| 58 | +default there will be a json in `.tw-patch/tw-class-list.json` in your project. |
| 59 | + |
| 60 | +you can custom this behavior by config `tailwindcss-mangle.config.ts` |
| 61 | + |
| 62 | +## Nodejs API |
| 63 | + |
| 64 | +```js |
| 65 | +import { TailwindcssPatcher } from 'tailwindcss-patch' |
| 66 | + |
| 67 | +const twPatcher = new TailwindcssPatcher(/* options */) |
| 68 | +// do patch |
| 69 | +twPatcher.patch() |
| 70 | +// get all contexts at runtime |
| 71 | +twPatcher.getContexts() |
| 72 | +// get all class generated by tailwindcss utilities |
| 73 | +twPatcher.getClassSet() |
| 74 | +``` |
| 75 | + |
| 76 | +## Config |
| 77 | + |
| 78 | +### Init Config File |
| 79 | + |
| 80 | +```sh |
| 81 | +npx tw-patch init |
| 82 | +``` |
| 83 | + |
| 84 | +Then there will be a ts file called `tailwindcss-mangle.config.ts` exist in your `cwd`. |
| 85 | + |
| 86 | +The config as follows: |
| 87 | + |
| 88 | +```ts |
| 89 | +import { defineConfig } from 'tailwindcss-patch' |
| 90 | + |
| 91 | +export default defineConfig({}) |
| 92 | +``` |
| 93 | + |
| 94 | +you can custom `tw-patch` behavior by `patch` option: |
| 95 | + |
| 96 | +```ts |
| 97 | +import { defineConfig } from 'tailwindcss-patch' |
| 98 | + |
| 99 | +export default defineConfig({ |
| 100 | + patch: { |
| 101 | + output: { |
| 102 | + filename: 'xxx.json', |
| 103 | + loose: true, |
| 104 | + removeUniversalSelector: true |
| 105 | + }, |
| 106 | + tailwindcss: { |
| 107 | + config: 'path/to/your-tailwind.config.js', |
| 108 | + cwd: 'project/cwd' |
| 109 | + } |
| 110 | + } |
| 111 | +}) |
| 112 | +``` |
| 113 | + |
| 114 | +## What's next? |
| 115 | + |
| 116 | +At the moment I just extracted all the tool classes to actually get the context of `tailwindcss` to analyze. You can add more functionality to this project by giving me `issue` or `pr`. |
| 117 | + |
| 118 | +Of course, the extracted `JSON` isn't just for you to look at. You can analyze it, and I use it as a data file for my `tailwindcss-mangle`. |
| 119 | + |
| 120 | +The `tailwindcss-mangle` itself is an obfuscation tool to obfuscate the tools generated by `tailwindcss`, see the next article for more details on how to use it. |
0 commit comments