Skip to content

pseudo element/class transformation? #69

Closed as not planned
Closed as not planned
@unleashit

Description

@unleashit

Hi, think I have a somewhat unusual use case in that I've written a small CLI to convert CSS modules into BEM (so users of a library can have a choice). It's working pretty well, except I still need it to transform some CSS module specific :global(.selector) pseudo elements which aren't legal in regular CSS.

So far the relevant part of the code is like this:

const camelToKebab = (str) =>
  str.replace(/([a-z0-9])([A-Z])/g, '$1-$2').toLowerCase();

const strategy = (selector) => {
  return `${camelToKebab(selector)}`;
};

async function main() {
  return await postcss([
    renamer({
      strategy,
      ids: true,
      prefix: `${prefix}${componentName}__`,
    }),
  ]).process(cssFile, { from: undefined });
}

main().then((o) => {
  fs.writeFileSync(newPath, o.css);
});

This works very well, unless the file contains any :global. Initially, I tried to add a regex in the strategy function, but noticed it doesn't receive the relevant pieces.

const strategy = (selector) => {
  const convertedCase =`${camelToKebab(selector)}`;

  // transform :global(.selector) to .selector
 return convertedCase.replace(/:global\((.+)\)/gm, ' $1');
};

Probably will just handle it manually if it can't be done and isn't worth adding as a feature.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions