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

How to replace a node? #261

Closed
joelmoss opened this issue Apr 20, 2021 · 1 comment
Closed

How to replace a node? #261

joelmoss opened this issue Apr 20, 2021 · 1 comment

Comments

@joelmoss
Copy link

I have this CSS:

body {
  @mixin container;
  color: red;
}

And want to replace the @mixin container; line with additional CSS. For example:

font-size: 12px;
a {
  color: pink;
}

My output should be

body {
  font-size: 12px;
  color: red;
  a {
    color: pink;
  }
}

So in my middleware, I can check for the @mixin type:

if (element.type === '@mixin') {
  // ??
}

but how to replace the node with the above CSS?

thx

@thysultan
Copy link
Owner

You can do this in multiple ways depending on the use case the simplest case being to just return a string from the middleware, other cases involve observing the parent/sibling nodes and serialising output that you return based on that, others include mutating the AST directly... the default middleware /src/Middleware.js have examples.

Closing the issue: if possible we can continue this discussion on the Discussions section.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants