Skip to content

sorting / re ordering rules from a visitor transform #573

@astahmer

Description

@astahmer

Context

I'm currently evaluating the possibility to migrate Panda CSS from postcss to lightningcss again

Issue

among others, we have a postcss plugin that sorts atomic rules to make the ones with modifiers/conditions at the bottom, along with media query etc.

I'm looking for a way to do that with lightningcss custom transforms. I checked the documentation and visitor tests but couldn't find something similar. Given there's no root container like postcss, this makes it a bit hard I guess ?

temporary workaround

  • getting all nodes with location
  • compute their corresponding CSS (something like this, using the location of the previous node with location)
  • re-construct the CSS however I need (= sort / re order rules the way I want)

haven't yet tried but this might just work. not sure if that would be ideal tho since the custom transform would not actually transform anything and would instead just serve as a custom visitor. (something like this).

on a slightly unrelated note, would it be possible to have a lightningcss.parse API just to get a SourceFile (kinda like ts-morph) with a forEachDescendant (or similar) method ? with a bit more infos on each nodes like start/end positions, and it would make the traversal easier. or actually maybe just a forEachDescendant on the transform method, as a TransformOption, along with the visitor

Current postcss plugin

to give you a better idea of the kind of things I'm looking for for:

the postcss plugin currently looks like this:
https://github.com/chakra-ui/panda/blob/1c8bd6f4a763bdd3b2d56ed6536c9555247e0139/packages/core/src/plugins/sort-css.ts#L10

and an example of the desired output:
https://github.com/chakra-ui/panda/blob/1c8bd6f4a763bdd3b2d56ed6536c9555247e0139/packages/core/__tests__/sort-css.test.ts#L25-L36

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions