@herb-tools/tailwind-class-sorter 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.txt +22 -0
- package/README.md +253 -0
- package/dist/tailwind-class-sorter.cjs +37113 -0
- package/dist/tailwind-class-sorter.cjs.map +1 -0
- package/dist/tailwind-class-sorter.esm.js +37086 -0
- package/dist/tailwind-class-sorter.esm.js.map +1 -0
- package/dist/types/config.d.ts +2 -0
- package/dist/types/expiring-map.d.ts +6 -0
- package/dist/types/index.d.ts +27 -0
- package/dist/types/resolve.d.ts +4 -0
- package/dist/types/sorter.d.ts +53 -0
- package/dist/types/sorting.d.ts +19 -0
- package/dist/types/types.d.ts +40 -0
- package/package.json +61 -0
- package/src/config.ts +344 -0
- package/src/expiring-map.ts +31 -0
- package/src/index.ts +64 -0
- package/src/resolve.ts +76 -0
- package/src/sorter.ts +106 -0
- package/src/sorting.ts +192 -0
- package/src/types.ts +55 -0
package/LICENSE.txt
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Original work Copyright (c) Tailwind Labs Inc.
|
|
4
|
+
Modified work Copyright (c) Marco Roth
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
# Standalone Tailwind CSS Class Sorter
|
|
2
|
+
|
|
3
|
+
**Package**: [`@herb-tools/tailwind-class-sorter`](https://www.npmjs.com/package/@herb-tools/tailwind-class-sorter)
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
A standalone Tailwind CSS class sorter that automatically sorts classes based on the [recommended class order](https://tailwindcss.com/blog/automatic-class-sorting-with-prettier#how-classes-are-sorted).
|
|
8
|
+
|
|
9
|
+
## Attribution
|
|
10
|
+
|
|
11
|
+
This package is a fork of the original [`prettier-plugin-tailwindcss`](https://github.com/tailwindlabs/prettier-plugin-tailwindcss) by [Tailwind Labs](https://tailwindlabs.com), modified to provide standalone class sorting functionality for [`@herb-tools/formatter`](@herb-tools/formatter) and other use cases. The core sorting logic and algorithms are from the original Tailwind Labs implementation.
|
|
12
|
+
|
|
13
|
+
## Key Features
|
|
14
|
+
|
|
15
|
+
* **Standalone class sorting** - Use the sorting algorithm independently from Prettier
|
|
16
|
+
* **Compatible with `@herb-tools/formatter`** - Designed to work seamlessly with the Herb ecosystem
|
|
17
|
+
* **Same sorting logic** - Uses the same proven algorithm as the original Prettier plugin
|
|
18
|
+
* **Full Tailwind config support** - Works with custom configs, v3 JS configs, and v4 CSS stylesheets
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
```sh
|
|
23
|
+
npm install -D @herb-tools/tailwind-class-sorter
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## API Reference
|
|
27
|
+
|
|
28
|
+
### Main Functions
|
|
29
|
+
|
|
30
|
+
#### `sortTailwindClasses(classStr, options?)`
|
|
31
|
+
|
|
32
|
+
Sorts a string of space-separated Tailwind CSS classes.
|
|
33
|
+
|
|
34
|
+
```js
|
|
35
|
+
import { sortTailwindClasses } from '@herb-tools/tailwind-class-sorter'
|
|
36
|
+
|
|
37
|
+
const result = await sortTailwindClasses('px-4 bg-blue-500 text-white rounded py-2')
|
|
38
|
+
// Result: 'rounded bg-blue-500 px-4 py-2 text-white'
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
**Parameters:**
|
|
42
|
+
- `classStr` (string): Space-separated CSS classes
|
|
43
|
+
- `options?` (SortTailwindClassesOptions): Configuration options
|
|
44
|
+
|
|
45
|
+
**Returns:** `Promise<string>` - Sorted class string
|
|
46
|
+
|
|
47
|
+
#### `sortTailwindClassList(classList, options?)`
|
|
48
|
+
|
|
49
|
+
Sorts an array of Tailwind CSS classes.
|
|
50
|
+
|
|
51
|
+
```js
|
|
52
|
+
import { sortTailwindClassList } from '@herb-tools/tailwind-class-sorter'
|
|
53
|
+
|
|
54
|
+
const result = await sortTailwindClassList(['px-4', 'bg-blue-500', 'text-white'])
|
|
55
|
+
// Result: { classList: ['bg-blue-500', 'px-4', 'text-white'], removedIndices: Set() }
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
**Parameters:**
|
|
59
|
+
- `classList` (string[]): Array of CSS classes
|
|
60
|
+
- `options?` (SortTailwindClassesOptions): Configuration options
|
|
61
|
+
|
|
62
|
+
**Returns:** `Promise<{ classList: string[], removedIndices: Set<number> }>` - Sorted classes and removed duplicate indices
|
|
63
|
+
|
|
64
|
+
#### `TailwindClassSorter` class
|
|
65
|
+
|
|
66
|
+
A reusable class sorter that holds a context for efficient, synchronous sorting. Ideal when you need to sort classes multiple times with the same configuration.
|
|
67
|
+
|
|
68
|
+
```js
|
|
69
|
+
import { TailwindClassSorter } from '@herb-tools/tailwind-class-sorter'
|
|
70
|
+
|
|
71
|
+
const sorter = await TailwindClassSorter.fromConfig({
|
|
72
|
+
tailwindConfig: './tailwind.config.js'
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
const result1 = sorter.sortClasses('px-4 bg-blue-500 text-white')
|
|
76
|
+
const result2 = sorter.sortClasses('py-2 bg-red-500 text-black')
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
**Static Methods:**
|
|
80
|
+
- `TailwindClassSorter.fromConfig(options?)`: Creates sorter from config file (async)
|
|
81
|
+
|
|
82
|
+
**Constructor:**
|
|
83
|
+
- `new TailwindClassSorter(contextContainer, options?)`: Creates sorter from pre-loaded context (sync)
|
|
84
|
+
|
|
85
|
+
**Instance Methods:**
|
|
86
|
+
- `sortClasses(classStr, overrideOptions?)`: Sort classes synchronously
|
|
87
|
+
- `sortClassList(classList, overrideOptions?)`: Sort class array synchronously
|
|
88
|
+
- `getContext()`: Get the underlying context container
|
|
89
|
+
- `getOptions()`: Get current configuration options
|
|
90
|
+
|
|
91
|
+
### Advanced Functions
|
|
92
|
+
|
|
93
|
+
#### `sortClasses(classStr, { env, ...options })`
|
|
94
|
+
|
|
95
|
+
Low-level function for sorting with a pre-configured environment.
|
|
96
|
+
|
|
97
|
+
#### `sortClassList(classList, { env, removeDuplicates })`
|
|
98
|
+
|
|
99
|
+
Low-level function for sorting class arrays with a pre-configured environment.
|
|
100
|
+
|
|
101
|
+
#### `getTailwindConfig(options)`
|
|
102
|
+
|
|
103
|
+
Loads and configures Tailwind CSS context.
|
|
104
|
+
|
|
105
|
+
### Configuration Options
|
|
106
|
+
|
|
107
|
+
The `SortTailwindClassesOptions` interface supports the following options:
|
|
108
|
+
|
|
109
|
+
```typescript
|
|
110
|
+
interface SortTailwindClassesOptions {
|
|
111
|
+
/** Path to the Tailwind config file */
|
|
112
|
+
tailwindConfig?: string
|
|
113
|
+
|
|
114
|
+
/** Path to the CSS stylesheet used by Tailwind CSS (v4+) */
|
|
115
|
+
tailwindStylesheet?: string
|
|
116
|
+
|
|
117
|
+
/** Preserve whitespace around Tailwind classes when sorting */
|
|
118
|
+
tailwindPreserveWhitespace?: boolean
|
|
119
|
+
|
|
120
|
+
/** Preserve duplicate classes inside a class list when sorting */
|
|
121
|
+
tailwindPreserveDuplicates?: boolean
|
|
122
|
+
|
|
123
|
+
/** Base directory for resolving config files (defaults to process.cwd()) */
|
|
124
|
+
baseDir?: string
|
|
125
|
+
}
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
## Usage Examples
|
|
129
|
+
|
|
130
|
+
### Basic Usage
|
|
131
|
+
|
|
132
|
+
```js
|
|
133
|
+
import { sortTailwindClasses } from '@herb-tools/tailwind-class-sorter'
|
|
134
|
+
|
|
135
|
+
const sorted = await sortTailwindClasses('px-4 bg-blue-500 text-white rounded py-2')
|
|
136
|
+
// Result: 'rounded bg-blue-500 px-4 py-2 text-white'
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
### With Custom Tailwind Config (v3)
|
|
140
|
+
|
|
141
|
+
```js
|
|
142
|
+
const sorted = await sortTailwindClasses(
|
|
143
|
+
'px-4 bg-custom-primary text-white py-2',
|
|
144
|
+
{
|
|
145
|
+
tailwindConfig: './tailwind.config.js'
|
|
146
|
+
}
|
|
147
|
+
)
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
### With Tailwind v4 Stylesheet
|
|
151
|
+
|
|
152
|
+
```js
|
|
153
|
+
const sorted = await sortTailwindClasses(
|
|
154
|
+
'px-4 bg-neon-pink text-brand-primary py-2',
|
|
155
|
+
{
|
|
156
|
+
tailwindStylesheet: './theme.css'
|
|
157
|
+
}
|
|
158
|
+
)
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
### Preserving Duplicates and Whitespace
|
|
162
|
+
|
|
163
|
+
```js
|
|
164
|
+
const sorted = await sortTailwindClasses(
|
|
165
|
+
'px-4 bg-blue-500 px-4 text-white',
|
|
166
|
+
{
|
|
167
|
+
tailwindPreserveDuplicates: true,
|
|
168
|
+
tailwindPreserveWhitespace: true
|
|
169
|
+
}
|
|
170
|
+
)
|
|
171
|
+
// Result: 'px-4 bg-blue-500 px-4 text-white'
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
### Sorting Class Arrays
|
|
175
|
+
|
|
176
|
+
```js
|
|
177
|
+
import { sortTailwindClassList } from '@herb-tools/tailwind-class-sorter'
|
|
178
|
+
|
|
179
|
+
const { classList, removedIndices } = await sortTailwindClassList(
|
|
180
|
+
['px-4', 'bg-blue-500', 'px-4', 'text-white'],
|
|
181
|
+
{ tailwindConfig: './tailwind.config.js' }
|
|
182
|
+
)
|
|
183
|
+
// classList: ['bg-blue-500', 'px-4', 'text-white']
|
|
184
|
+
// removedIndices: Set([2]) - duplicate px-4 at index 2 was removed
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
### Efficient Repeated Sorting with TailwindClassSorter
|
|
188
|
+
|
|
189
|
+
For scenarios where you need to sort many class strings with the same configuration, use the `TailwindClassSorter` class for better performance:
|
|
190
|
+
|
|
191
|
+
```js
|
|
192
|
+
import { TailwindClassSorter } from '@herb-tools/tailwind-class-sorter'
|
|
193
|
+
|
|
194
|
+
const sorter = await TailwindClassSorter.fromConfig({
|
|
195
|
+
tailwindConfig: './tailwind.config.js',
|
|
196
|
+
tailwindPreserveDuplicates: false
|
|
197
|
+
})
|
|
198
|
+
|
|
199
|
+
const components = [
|
|
200
|
+
'px-4 bg-blue-500 text-white py-2',
|
|
201
|
+
'mx-auto flex items-center justify-center',
|
|
202
|
+
'border-2 border-gray-300 rounded-lg shadow-sm'
|
|
203
|
+
]
|
|
204
|
+
|
|
205
|
+
const sortedComponents = components.map(classes => sorter.sortClasses(classes))
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
### Using Pre-loaded Context
|
|
209
|
+
|
|
210
|
+
You can also create a sorter from a pre-loaded context:
|
|
211
|
+
|
|
212
|
+
```js
|
|
213
|
+
import { TailwindClassSorter, getTailwindConfig } from '@herb-tools/tailwind-class-sorter'
|
|
214
|
+
|
|
215
|
+
const contextContainer = await getTailwindConfig({
|
|
216
|
+
tailwindConfig: './tailwind.config.js'
|
|
217
|
+
})
|
|
218
|
+
|
|
219
|
+
const sorter = new TailwindClassSorter(contextContainer, {
|
|
220
|
+
tailwindPreserveDuplicates: false
|
|
221
|
+
})
|
|
222
|
+
|
|
223
|
+
const result = sorter.sortClasses('px-4 px-4 bg-blue-500 text-white')
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
### Per-Sort Option Overrides
|
|
227
|
+
|
|
228
|
+
You can override options on a per-sort basis without creating new instances:
|
|
229
|
+
|
|
230
|
+
```js
|
|
231
|
+
const sorter = await TailwindClassSorter.fromConfig({
|
|
232
|
+
tailwindPreserveDuplicates: false
|
|
233
|
+
})
|
|
234
|
+
|
|
235
|
+
const normal = sorter.sortClasses('px-4 px-4 bg-blue-500')
|
|
236
|
+
|
|
237
|
+
const preserved = sorter.sortClasses('px-4 px-4 bg-blue-500', {
|
|
238
|
+
tailwindPreserveDuplicates: true
|
|
239
|
+
})
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
## About This Fork
|
|
243
|
+
|
|
244
|
+
The original `prettier-plugin-tailwindcss` is designed as a Prettier plugin. This package extracts the sorting functionality for use in other contexts like `@herb-tools/formatter`, build tools, or any JavaScript application that needs Tailwind class sorting.
|
|
245
|
+
|
|
246
|
+
For the full Prettier plugin experience, use the [original package](https://www.npmjs.com/package/prettier-plugin-tailwindcss).
|
|
247
|
+
|
|
248
|
+
## License
|
|
249
|
+
|
|
250
|
+
MIT License - see [LICENSE](./LICENSE.txt) for details.
|
|
251
|
+
|
|
252
|
+
Original work Copyright (c) Tailwind Labs Inc.
|
|
253
|
+
Modified work Copyright (c) Marco Roth
|