Rewrite Image URLs in CSS files using Service Workers

I’m using a separate CDN for delivering images. I’ve rewritten all images in the HTML using HTMLRewriter. However, some images like background images’ URLs are present inside CSS files and I want to rewrite them.

Is there anything like HTMLRewriter for CSS? If not, what is the best performant way to parse and rewrite images in css files?

For now, download the whole content, search and replace using regex seems to be the only solution. Not sure if there any better way.