Search code examples
postcss

How to replace `walkDecls` with a regex with postcss8 API `Declaration`


Hello postcss experts!

I try to update a plugin to postcss v8 API.

I don't find how to replace:

root.walkDecls(/^--/, (decl) => {
  //…
});

By the new Declaration: entry.

Where to put this regex?


Solution

  • Unfortunately, there is no API for regexp in the new visitor API. Use:

    Declaration (decl) {
      if (decl.prop.startsWith('--')) {
        …
      }
    }
    

    In walkDecls passing RegExp do not make it faster. It is just syntax sugar. This code with startsWith instead of RegExp will work a little faster than old one.