Home / Function/ sortBuckets() — tailwindcss Function Reference

sortBuckets() — tailwindcss Function Reference

Architecture documentation for the sortBuckets() function in sort-buckets.ts from the tailwindcss codebase.

Entity Profile

Dependency Diagram

graph TD
  92a12a9f_b36d_4c66_d885_e04224081f21["sortBuckets()"]
  ecf9faa2_4ccf_f16c_bb10_222be63faed0["sort-buckets.ts"]
  92a12a9f_b36d_4c66_d885_e04224081f21 -->|defined in| ecf9faa2_4ccf_f16c_bb10_222be63faed0
  b2462a9a_300b_e7ab_7d56_790bc72f6c28["migrate()"]
  b2462a9a_300b_e7ab_7d56_790bc72f6c28 -->|calls| 92a12a9f_b36d_4c66_d885_e04224081f21
  daaeb558_f493_21e4_086c_d337c75d4e05["migrate()"]
  daaeb558_f493_21e4_086c_d337c75d4e05 -->|calls| 92a12a9f_b36d_4c66_d885_e04224081f21
  6681164e_d8eb_ea9a_ca1c_5d3d077cabd5["migrate()"]
  6681164e_d8eb_ea9a_ca1c_5d3d077cabd5 -->|calls| 92a12a9f_b36d_4c66_d885_e04224081f21
  357a27af_0d02_5377_1d2a_20de9a82dba1["migrate()"]
  357a27af_0d02_5377_1d2a_20de9a82dba1 -->|calls| 92a12a9f_b36d_4c66_d885_e04224081f21
  26298c28_699e_61f2_4a12_de01f329a5d8["migrate()"]
  26298c28_699e_61f2_4a12_de01f329a5d8 -->|calls| 92a12a9f_b36d_4c66_d885_e04224081f21
  afdd8826_c4ba_f675_924c_bce1adab8603["migrate()"]
  afdd8826_c4ba_f675_924c_bce1adab8603 -->|calls| 92a12a9f_b36d_4c66_d885_e04224081f21
  345ce337_4027_ef1a_0d01_935066809de4["migrate()"]
  345ce337_4027_ef1a_0d01_935066809de4 -->|calls| 92a12a9f_b36d_4c66_d885_e04224081f21
  17550040_432e_8df4_09fe_ecb183594100["migrate()"]
  17550040_432e_8df4_09fe_ecb183594100 -->|calls| 92a12a9f_b36d_4c66_d885_e04224081f21
  e2606484_06bf_1985_5729_1134a15ad1e6["migrate()"]
  e2606484_06bf_1985_5729_1134a15ad1e6 -->|calls| 92a12a9f_b36d_4c66_d885_e04224081f21
  6066f111_c660_b87d_6993_07d8cc779b5c["run()"]
  6066f111_c660_b87d_6993_07d8cc779b5c -->|calls| 92a12a9f_b36d_4c66_d885_e04224081f21
  5bcf4886_1230_a8ff_7302_a26cc5a9a525["get()"]
  92a12a9f_b36d_4c66_d885_e04224081f21 -->|calls| 5bcf4886_1230_a8ff_7302_a26cc5a9a525
  fb009bc2_68d9_5c0f_7fa7_fa364488e50a["walk()"]
  92a12a9f_b36d_4c66_d885_e04224081f21 -->|calls| fb009bc2_68d9_5c0f_7fa7_fa364488e50a
  d110683b_9feb_ade8_7566_cf09fdf4f164["distance()"]
  92a12a9f_b36d_4c66_d885_e04224081f21 -->|calls| d110683b_9feb_ade8_7566_cf09fdf4f164
  style 92a12a9f_b36d_4c66_d885_e04224081f21 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
      })

Subdomains

Frequently Asked Questions

What does sortBuckets() do?
sortBuckets() is a function in the tailwindcss codebase, defined in packages/@tailwindcss-upgrade/src/codemods/css/sort-buckets.ts.
Where is sortBuckets() defined?
sortBuckets() is defined in packages/@tailwindcss-upgrade/src/codemods/css/sort-buckets.ts at line 24.
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