Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure @apply works consistently with or without @layer #6938

Merged
merged 5 commits into from Jan 7, 2022

Commits on Jan 6, 2022

  1. partition nodes as soon as possible

    Time to write another story on `@apply`...
    
    When we write code like this:
    
    ```css
    .a {
      @apply b;
    }
    
    .b {
      @apply uppercase;
      color: red;
    }
    ```
    
    Then we create 2 Nodes in our context to keep track of. One has
    identifier `a`, the other has identifier `b`. However, when we have an
    `@apply` and it contains multiple declarations/atrules, then we have to
    split up the (aka partition) node into multiple nodes so that we can
    guarantee the correct expected sort order.
    
    This means that the above example technically looks like this:
    
    ```css
    .a {
      @apply b;
    }
    
    .b {
      @apply uppercase;
    }
    
    .b {
      color: red;
    }
    ```
    
    If this was your input, then we would still have 1 node for identifier
    'a', but we would have 2 nodes for identifier 'b'.
    
    As mentioned earlier, this is important to guarantee the correct order,
    here is an example:
    
    ```css
    .b {
      @apply md:font-bold xl:font-normal; /* Here we can sort by our
      internal rules. This means that the `md` comes before `xl`. */
    }
    ```
    
    ... however
    
    ```css
    .b {
      @apply xl:font-normal; /* This now exists _before_ the example below */
    }
    
    .b {
      @apply md:font-bold; /* Because we respect the order of the user's css */
    }
    ```
    
    So to guarantee the order when doing this:
    ```css
    .b {
      @apply xl:font-normal;
      @apply lg:font-normal;
    }
    ```
    
    We also split this up into 2 nodes like this:
    ```css
    .b {
      @apply xl:font-normal;
    }
    .b {
      @apply lg:font-normal;
    }
    ```
    
    The tricky part is that now only 1 empty `.b` node exists in our context
    because we partitioned the orginal node into multiple nodes and moved
    the children to the new nodes and because they are new nodes it means
    that they have a different identity.
    
    This partitioning used to happen in the expandApplyAtRules code, but
    this is a bit too late because the context has already been filled at
    this time. Instead, we move the code more to the front, as if you wrote
    those separated blocks yourself. Now the code to inject those nodes into
    the context happens in a single spot instead of multiple places.
    
    Another good part about this is that we have better consistency between
    each layer because it turns out that these two examples generated
    different results...
    
    ```css
    .a {
      @apply b;
    }
    .b {
      @apply uppercase;
      color: red;
    }
    ```
    
    ... is different compared to:
    
    ```css
    @tailwind components;
    @layer components {
      .a {
        @apply b;
      }
      .b {
        @apply uppercase;
        color: red;
      }
    }
    ```
    
    Even if both `a` and `b` are being used in one of your content paths...
    Yeah.. *sigh*
    RobinMalfait committed Jan 6, 2022
    Copy the full SHA
    2951408 View commit details
    Browse the repository at this point in the history
  2. Copy the full SHA
    af5468b View commit details
    Browse the repository at this point in the history
  3. update changelog

    RobinMalfait committed Jan 6, 2022
    Copy the full SHA
    9593d53 View commit details
    Browse the repository at this point in the history

Commits on Jan 7, 2022

  1. Copy the full SHA
    6983b89 View commit details
    Browse the repository at this point in the history
  2. remove leftover todo

    This has been fixed already
    RobinMalfait committed Jan 7, 2022
    Copy the full SHA
    1560529 View commit details
    Browse the repository at this point in the history