sortBuckets() — tailwindcss Function Reference
Architecture documentation for the sortBuckets() function in sort-buckets.ts from the tailwindcss codebase.
Entity Profile
Dependency Diagram
graph TD f8da4c29_335c_9511_8ded_97963c8de7d7["sortBuckets()"] cc32470d_4e38_6d9b_e583_6e81bb0dc253["migrate()"] cc32470d_4e38_6d9b_e583_6e81bb0dc253 -->|calls| f8da4c29_335c_9511_8ded_97963c8de7d7 86e5240c_c4b1_4628_44c0_1c2f8a0111de["migrate()"] 86e5240c_c4b1_4628_44c0_1c2f8a0111de -->|calls| f8da4c29_335c_9511_8ded_97963c8de7d7 e693c615_04b0_46f3_b602_3e46d121fcb0["migrate()"] e693c615_04b0_46f3_b602_3e46d121fcb0 -->|calls| f8da4c29_335c_9511_8ded_97963c8de7d7 74a61e0c_fc6d_ca6e_70cb_582ad1c4ad3c["migrate()"] 74a61e0c_fc6d_ca6e_70cb_582ad1c4ad3c -->|calls| f8da4c29_335c_9511_8ded_97963c8de7d7 b242fd0e_6a8e_7d8b_fdb9_e04bb2e47104["migrate()"] b242fd0e_6a8e_7d8b_fdb9_e04bb2e47104 -->|calls| f8da4c29_335c_9511_8ded_97963c8de7d7 b614008a_e4aa_722c_910a_3c58e4d28cef["migrate()"] b614008a_e4aa_722c_910a_3c58e4d28cef -->|calls| f8da4c29_335c_9511_8ded_97963c8de7d7 c5a75dce_403f_ac99_8467_fc78fa640177["migrate()"] c5a75dce_403f_ac99_8467_fc78fa640177 -->|calls| f8da4c29_335c_9511_8ded_97963c8de7d7 74fc48f6_df85_86ac_e006_45cc2fe2ccca["migrate()"] 74fc48f6_df85_86ac_e006_45cc2fe2ccca -->|calls| f8da4c29_335c_9511_8ded_97963c8de7d7 d7062e64_c90e_b85d_7355_db6c443e36d0["migrate()"] d7062e64_c90e_b85d_7355_db6c443e36d0 -->|calls| f8da4c29_335c_9511_8ded_97963c8de7d7 c1db9de2_3654_d42b_b9f2_1c5a0ccbd307["run()"] c1db9de2_3654_d42b_b9f2_1c5a0ccbd307 -->|calls| f8da4c29_335c_9511_8ded_97963c8de7d7 4cd99e59_ac1e_2a1f_0946_33cc1afd2532["get()"] f8da4c29_335c_9511_8ded_97963c8de7d7 -->|calls| 4cd99e59_ac1e_2a1f_0946_33cc1afd2532 07d1c181_5704_5e29_cec9_3c03edb8746c["walk()"] f8da4c29_335c_9511_8ded_97963c8de7d7 -->|calls| 07d1c181_5704_5e29_cec9_3c03edb8746c 7c64ccd9_f19c_c04c_7546_1d83845680a8["distance()"] f8da4c29_335c_9511_8ded_97963c8de7d7 -->|calls| 7c64ccd9_f19c_c04c_7546_1d83845680a8 style f8da4c29_335c_9511_8ded_97963c8de7d7 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
packages/@tailwindcss-upgrade/src/codemods/css/sort-buckets.ts lines 24–150
export function sortBuckets(): Plugin {
async function migrate(root: Root) {
// 1. Move items that are not in a bucket, into a bucket
{
let comments: Comment[] = []
let buckets = new DefaultMap<string, AtRule>((name) => {
let bucket = postcss.atRule({ name: 'tw-bucket', params: name, nodes: [] })
root.append(bucket)
return bucket
})
// Seed the buckets with existing buckets
root.walkAtRules('tw-bucket', (node) => {
buckets.set(node.params, node)
})
let lastLayer = 'user'
function injectInto(name: string, ...nodes: ChildNode[]) {
lastLayer = name
buckets.get(name).nodes?.push(...comments.splice(0), ...nodes)
}
walk(root, (node) => {
// Already in a bucket, skip it
if (node.type === 'atrule' && node.name === 'tw-bucket') {
return WalkAction.Skip
}
// Comments belong to the bucket of the nearest node, which is typically
// in the "next" bucket.
if (node.type === 'comment') {
// We already have comments, which means that we already have nodes
// that belong in the next bucket, so we should move the current
// comment into the next bucket as well.
if (comments.length > 0) {
comments.push(node)
return
}
// Figure out the closest node to the comment
let prevDistance = distance(node.prev(), node) ?? Infinity
let nextDistance = distance(node, node.next()) ?? Infinity
if (prevDistance < nextDistance) {
buckets.get(lastLayer).nodes?.push(node)
} else {
comments.push(node)
}
}
// Known at-rules
else if (
node.type === 'atrule' &&
['config', 'plugin', 'source', 'theme', 'utility', 'custom-variant'].includes(node.name)
) {
injectInto(node.name, node)
}
// Imports bucket, which also contains the `@charset` and body-less `@layer`
else if (
(node.type === 'atrule' && node.name === 'layer' && !node.nodes) || // @layer foo, bar;
(node.type === 'atrule' && node.name === 'import') ||
(node.type === 'atrule' && node.name === 'charset') || // @charset "UTF-8";
(node.type === 'atrule' && node.name === 'tailwind')
) {
injectInto('import', node)
}
// User CSS
else if (node.type === 'rule' || node.type === 'atrule') {
injectInto('user', node)
}
// Fallback
else {
injectInto('user', node)
}
return WalkAction.Skip
})
if (comments.length > 0) {
injectInto(lastLayer)
}
}
// 2. Merge `@tw-bucket` with the same name together
let firstBuckets = new Map<string, AtRule>()
root.walkAtRules('tw-bucket', (node) => {
let firstBucket = firstBuckets.get(node.params)
if (!firstBucket) {
firstBuckets.set(node.params, node)
return
}
if (node.nodes) {
firstBucket.append(...node.nodes)
}
})
// 3. Remove empty `@tw-bucket`
root.walkAtRules('tw-bucket', (node) => {
if (!node.nodes?.length) {
node.remove()
}
})
// 4. Sort the `@tw-bucket` themselves
{
let sorted = Array.from(firstBuckets.values()).sort((a, z) => {
let aIndex = BUCKET_ORDER.indexOf(a.params)
let zIndex = BUCKET_ORDER.indexOf(z.params)
return aIndex - zIndex
})
// Re-inject the sorted buckets
root.removeAll()
root.append(sorted)
}
}
return {
postcssPlugin: '@tailwindcss/upgrade/sort-buckets',
OnceExit: migrate,
}
}
Domain
Subdomains
Calls
Source
Frequently Asked Questions
What does sortBuckets() do?
sortBuckets() is a function in the tailwindcss codebase.
What does sortBuckets() call?
sortBuckets() calls 3 function(s): distance, get, walk.
What calls sortBuckets()?
sortBuckets() is called by 10 function(s): migrate, migrate, migrate, migrate, migrate, migrate, migrate, migrate, and 2 more.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free