Skip to content

Commit 280f1ed

Browse files
committed
feat: add tailwind utility class ordering option
1 parent a44c126 commit 280f1ed

File tree

5 files changed

+77
-7
lines changed

5 files changed

+77
-7
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@egoistdeveloper/twcss-to-sass",
3-
"version": "2.1.17",
3+
"version": "2.1.18",
44
"description": "HTML template to SASS converter for TailwindCSS",
55
"main": "dist/cjs/index.js",
66
"module": "dist/esm/index.js",

src/interfaces/tw-to-sass-options.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export interface ITwToSassOptions {
88
printSassComments: boolean
99
useCommentBlocksAsClassName: boolean
1010
maxClassNameLength: number
11-
classNameOptions: IClassNameOptions,
11+
classNameOptions: IClassNameOptions
1212
preventDuplicateClasses: boolean
13+
orderByTailwindClasses: boolean
1314
}

src/twcss-to-sass.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import beautifyCss, { CSSBeautifyOptions } from 'js-beautify'
44
import Utils from './utils/utils'
55
import HtmlUtils from './utils/html'
66
import SlugUtils from './utils/slug'
7+
import ClassUtils from './utils/class'
78

89
import { ITwToSassOptions } from './interfaces/tw-to-sass-options'
910
import {
@@ -38,6 +39,7 @@ const defaultOptions: ITwToSassOptions = {
3839
printSassComments: true,
3940
formatterOptions: formatterOptions,
4041
preventDuplicateClasses: true,
42+
orderByTailwindClasses: true,
4143
classNameOptions: {
4244
lowercase: true,
4345
replacement: '-',
@@ -436,8 +438,14 @@ function getNodeClassAndStyles(node: IHtmlNode): string {
436438
if (node.filterAttributes) {
437439
// print tailwind class names
438440
if (node.filterAttributes.class) {
441+
let classList = ''
442+
443+
classList = _defaultOptions.orderByTailwindClasses
444+
? ClassUtils.orderClasses(node.filterAttributes.class)
445+
: node.filterAttributes.class
446+
439447
nodeClassAndStyles += node.filterAttributes.class
440-
? `@apply ${node.filterAttributes.class};`
448+
? `@apply ${classList};`
441449
: ''
442450
}
443451

src/utils/class.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const ClassUtils = {
2+
/**
3+
* Order by class names
4+
*
5+
* @param classes string
6+
* @returns string
7+
*/
8+
orderClasses(classes: string): string {
9+
const classList = classes.split(/\s+/g)
10+
11+
const orderedClassList = classList
12+
.sort((a, b) => {
13+
return a > b ? 1 : a < b ? -1 : 0
14+
})
15+
.join(' ')
16+
17+
return orderedClassList
18+
},
19+
}
20+
21+
export default ClassUtils

test/twcss-to-sass.test.ts

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ test('convert to sass', () => {
1010
@apply w-72 h-40 bg-green-400 transform transition-all;
1111
}`
1212

13-
const converterResult = convertToSass(htmlCotnent)
13+
const converterConfigs = <ITwToSassOptions>{
14+
orderByTailwindClasses: false
15+
}
16+
17+
const converterResult = convertToSass(htmlCotnent, converterConfigs)
1418

1519
expect(converterResult?.sass).toBe(sassOutput)
1620
})
@@ -28,7 +32,11 @@ test('convert to sass with inline css', () => {
2832
font-weight: 50px;
2933
}`
3034

31-
const converterResult = convertToSass(htmlCotnent)
35+
const converterConfigs = <ITwToSassOptions>{
36+
orderByTailwindClasses: false
37+
}
38+
39+
const converterResult = convertToSass(htmlCotnent, converterConfigs)
3240

3341
expect(converterResult?.sass).toBe(sassOutput)
3442
})
@@ -57,7 +65,9 @@ test('convert to sass with comments', () => {
5765
const converterConfigs = <ITwToSassOptions>{
5866
useCommentBlocksAsClassName: true,
5967
printSassComments: true,
68+
orderByTailwindClasses: false
6069
}
70+
6171
const converterResult = convertToSass(htmlCotnent, converterConfigs)
6272

6373
expect(converterResult?.sass).toBe(sassOutput)
@@ -87,6 +97,7 @@ test('convert to sass with comments class names', () => {
8797
const converterConfigs = <ITwToSassOptions>{
8898
useCommentBlocksAsClassName: true,
8999
printSassComments: true,
100+
orderByTailwindClasses: false,
90101
classNameOptions: {
91102
lowercase: true,
92103
replacement: '_',
@@ -127,7 +138,11 @@ test('convert to sass with group-modifier', () => {
127138
}
128139
}`
129140

130-
const converterResult = convertToSass(htmlCotnent)
141+
const converterConfigs = <ITwToSassOptions>{
142+
orderByTailwindClasses: false
143+
}
144+
145+
const converterResult = convertToSass(htmlCotnent, converterConfigs)
131146

132147
expect(converterResult?.sass).toBe(sassOutput)
133148
})
@@ -175,8 +190,33 @@ test('convert to sass with non-duplicated classes', () => {
175190
}
176191
}`
177192

178-
const converterResult = convertToSass(htmlCotnent)
193+
const converterConfigs = <ITwToSassOptions>{
194+
orderByTailwindClasses: false
195+
}
196+
197+
const converterResult = convertToSass(htmlCotnent, converterConfigs)
179198

180199
expect(converterResult?.sass).toBe(sassOutput)
181200
})
182201

202+
test('convert to sass with class ordering', () => {
203+
const htmlCotnent = `<!-- My Button -->
204+
<button class="flex items-center justify-center w-full px-4 py-2 space-x-1 font-medium tracking-wider uppercase bg-gray-100 border rounded-md focus:outline-none focus:ring">
205+
test
206+
</button>`
207+
208+
const sassOutput = `/* My Button -> 1 */
209+
.my-button {
210+
@apply bg-gray-100 border flex focus:outline-none focus:ring font-medium items-center justify-center px-4 py-2 rounded-md space-x-1 tracking-wider uppercase w-full;
211+
}`
212+
213+
const converterConfigs = <ITwToSassOptions>{
214+
useCommentBlocksAsClassName: true,
215+
printSassComments: true,
216+
orderByTailwindClasses: true
217+
}
218+
219+
const converterResult = convertToSass(htmlCotnent, converterConfigs)
220+
221+
expect(converterResult?.sass).toBe(sassOutput)
222+
})

0 commit comments

Comments
 (0)