css-modules / icss-utils

Search & replace tokens during the linking stage of ICSS loading
ISC License
23 stars 15 forks source link

Allow specifying replacements as a callback #70

Open joepie91 opened 4 years ago

joepie91 commented 4 years ago

Currently, replaceSymbols only accepts a mapping of values. This is problematic because the same value may need to be represented differently in different contexts (reference, and there's no way to do that right now.

An API that would solve this, would be something like:

icssUtils.replaceSymbols(css, (symbol, context) => {
    let mappedSymbol = someMapping[symbol];

    if (context === "decl") {
        return mappedSymbol;
    } else {
        return mappedSymbol.replace(/ /g, ".");
    }
});
alexander-akait commented 4 years ago

Not sure it is real fix bug, when we replace we should known what it was composing from selector

joepie91 commented 4 years ago

What usecase do you have in mind where that is necessary?

At least for the composed-class-name issue that shouldn't be necessary, as the correct formatting is dependent entirely on where the identifier appears (class names cannot appear in rule declarations, and vice versa).

alexander-akait commented 4 years ago

Not really, when we receive something (It can bee selector/value/media query) from another file, we do not know whether it is, so we should keep context where we get value before, we should change :import('file.css') { test: test from selector } (just idea) it will not fundamentally solve the problem, but rather a hack