Skip to content

Commit ecdf567

Browse files
authored
Export public sorting APIs to /sorter (#438)
1 parent 5229a0a commit ecdf567

10 files changed

Lines changed: 712 additions & 291 deletions

File tree

README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,55 @@ Once added, tag your strings with the function and the plugin will sort them:
192192
const mySortedClasses = tw`bg-white p-4 dark:bg-black`
193193
```
194194

195+
## Public API
196+
197+
If you want to use the Tailwind class sorting logic outside of Prettier, import from the
198+
`sorter` entrypoint:
199+
200+
```js
201+
import { createSorter } from 'prettier-plugin-tailwindcss/sorter'
202+
203+
let sorter = await createSorter({
204+
base: '/path/to/project',
205+
stylesheetPath: './app.css',
206+
})
207+
208+
// Sort HTML class attributes (space-separated strings)
209+
let sorted = sorter.sortClassAttributes([
210+
'sm:bg-tomato bg-red-500',
211+
'p-4 m-2'
212+
])
213+
// Returns: ['bg-red-500 sm:bg-tomato', 'm-2 p-4']
214+
215+
// Sort class lists (arrays of class names)
216+
let sortedLists = sorter.sortClassLists([
217+
['sm:bg-tomato', 'bg-red-500'],
218+
['p-4', 'm-2']
219+
])
220+
// Returns: [['bg-red-500', 'sm:bg-tomato'], ['m-2', 'p-4']]
221+
```
222+
223+
### API Options
224+
225+
The `createSorter` function accepts the following options:
226+
227+
- **`base`** (optional): The directory used to resolve relative file paths. Defaults to the current working directory.
228+
- **`filepath`** (optional): The path to the file being formatted. When provided, Tailwind CSS is resolved relative to this path.
229+
- **`configPath`** (optional): Path to the Tailwind CSS config file (v3). Paths are resolved relative to `base`.
230+
- **`stylesheetPath`** (optional): Path to the CSS stylesheet used by Tailwind CSS (v4+). Paths are resolved relative to `base`.
231+
- **`preserveWhitespace`** (optional): Whether to preserve whitespace around classes. Default: `false`.
232+
- **`preserveDuplicates`** (optional): Whether to preserve duplicate classes. Default: `false`.
233+
234+
### Sorter Methods
235+
236+
The sorter object returned by `createSorter` has two methods:
237+
238+
- **`sortClassAttributes(classes: string[]): string[]`**
239+
Sorts one or more HTML class attributes. Each element should be a space-separated string of class names (like the value of an HTML `class` attribute).
240+
241+
- **`sortClassLists(classes: string[][]): (string | null)[][]`**
242+
Sorts one or more class lists. Each element should be an array of individual class names. When removing duplicates (default behavior), duplicate classes are replaced with `null` in the output.
243+
195244
### Using regex patterns
196245

197246
Like the `tailwindAttributes` option, the `tailwindFunctions` option also supports regular expressions to match multiple function names. Patterns should be enclosed in forward slashes. Note that JS regex literals are not supported with Prettier.

package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@
1010
"files": [
1111
"dist"
1212
],
13+
"exports": {
14+
".": {
15+
"types": "./dist/index.d.mts",
16+
"import": "./dist/index.mjs",
17+
"default": "./dist/index.mjs"
18+
},
19+
"./sorter": {
20+
"types": "./dist/sorter.d.mts",
21+
"import": "./dist/sorter.mjs",
22+
"default": "./dist/sorter.mjs"
23+
}
24+
},
1325
"repository": {
1426
"type": "git",
1527
"url": "https://github.com/tailwindlabs/prettier-plugin-tailwindcss"

0 commit comments

Comments
 (0)