gajus / babel-plugin-react-css-modules

Transforms styleName to className using compile time CSS module resolution.
Other
2.05k stars 162 forks source link

Feat/add regexp support for filetype #240

Closed xuxinhang closed 3 years ago

xuxinhang commented 5 years ago

Add supports for using RegExp patterns as key names in filetype

Usage

Now, we can use a RegExp pattern in filetypes to specify a certain file.

Consider the following config.

{
  filetypes: {
    '\\.mod\\.sass$': { syntax: 'sugarss' },
  },
}
// component.js
import './components/another.mod.sass';
import './components/another.sass';

// ... ...

According to the config above, babel-plugin-react-css-modules will match and process another.mod.sass, but live another.sass alone.

Why is this useful?

Sometimes, I want to import some .sass file as global styles (such as styles for the common UI). However, if I just add the key .sass to filetypes, then all imported .sass files would be processed, including those containing global styles. (By the way, I dislike :global.)

The support for RegExp patterns would also be useful when assigning the different plugin lists (or other config items) to the different .sass files.

Compatibility

The all test cases is covered. (yarn test on my computer.)

It is worth discussing further if there are any better implements or API designs.