Skip to content

Commit

Permalink
prefer Tailwind's context.sortClassList() if it exists
Browse files Browse the repository at this point in the history
We have to keep the existing code for now, since the plugin will prefer
the user's tailwind version. Hopefully we can remove this in future
versions.
  • Loading branch information
RobinMalfait committed Feb 10, 2022
1 parent 2f48911 commit a398cd2
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions src/index.js
Expand Up @@ -59,25 +59,29 @@ function sortClasses(
suffix = `${whitespace.pop() ?? ''}${classes.pop() ?? ''}`
}

let classNamesWithOrder = []
for (let className of classes) {
let order =
env
.generateRules(new Set([className]), env.context)
.sort(([a], [z]) => bigSign(z - a))[0]?.[0] ?? null
classNamesWithOrder.push([className, order])
}
if (env.context.sortClassList) {
classes = env.context.sortClassList(classes)
} else {
let classNamesWithOrder = []
for (let className of classes) {
let order =
env
.generateRules(new Set([className]), env.context)
.sort(([a], [z]) => bigSign(z - a))[0]?.[0] ?? null
classNamesWithOrder.push([className, order])
}

classes = classNamesWithOrder
.sort(([, a], [, z]) => {
if (a === z) return 0
// if (a === null) return options.unknownClassPosition === 'start' ? -1 : 1
// if (z === null) return options.unknownClassPosition === 'start' ? 1 : -1
if (a === null) return -1
if (z === null) return 1
return bigSign(a - z)
})
.map(([className]) => className)
classes = classNamesWithOrder
.sort(([, a], [, z]) => {
if (a === z) return 0
// if (a === null) return options.unknownClassPosition === 'start' ? -1 : 1
// if (z === null) return options.unknownClassPosition === 'start' ? 1 : -1
if (a === null) return -1
if (z === null) return 1
return bigSign(a - z)
})
.map(([className]) => className)
}

for (let i = 0; i < classes.length; i++) {
result += `${classes[i]}${whitespace[i] ?? ''}`
Expand Down

0 comments on commit a398cd2

Please sign in to comment.