RadValentin / postcss-prefix-selector

Prefix all CSS rules with a selector
MIT License
165 stars 16 forks source link

Request: access to root in transform callback #108

Closed luttje closed 2 years ago

luttje commented 2 years ago

Hi there, thanks for this useful plugin!

From your other posts you seem very busy atm, so please do ignore this request until you have time. I could help by making a PR for this if you like the idea.

Request

Right now the transform callback only receives strings as parameters. It would be useful if it at least also received the postcss root node to support more complex filtering.

Example use-case

I would use this for adding a prefix, but only on the condition that the root has no comment /* !allGlobal */ as it's first node. This is already possible, but requires having to parse the file again.

How it could work:

// [...]
transform: function (prefix, selector, prefixedSelector, file, root) {
    const rootNode = root.first;

    // etc.:
    if (rootNode.type === 'comment' && rootNode.text.trim().toLowerCase() === '!allglobal') {
        return selector;
    } else {
        return prefixedSelector;
    }
}

How I make this work now:

const fs = require('fs');
// [...]
transform: function (prefix, selector, prefixedSelector, file) {
    const rootNode = postcss.parse(fs.readFileSync(file)).first;

    // etc...
}

Thanks for your time. Good luck to you!

RadValentin commented 2 years ago

@luttje If you can make a PR implementing this functionality, I can find time to review it