A PostCSS plugin to prefix classes with corresponding filenames
The idea is to isolate styles in framework components
MyComponent.css
.myclass { /* ... */ }
.-mymodifier { /* ... */ }
.Mycapitalclass { /* ... */ }
Output:
.MyComponent-myclass { /* ... */ }
.-mymodifier { /* ... */ }
.Mycapitalclass { /* ... */ }
Ignores:
- filenames that starts from not capital letter
- modifiers (classes that starts from hyphen)
- classes that starts from capital letter
npm install postcss-filename-prefix
var fs = require('fs');
var postcss = require('postcss');
var filenamePrefix = require('postcss-filename-prefix');
var css = fs.readFileSync('css/MyFile.css', 'utf8').toString();
var out = postcss()
.use(filenamePrefix())
.process(css);
var fs = require('fs');
var postcss = require('postcss');
var filenamePrefix = require('postcss-filename-prefix');
var css = fs.readFileSync('css/MyFile.css', 'utf8').toString();
var out = postcss()
.use(filenamePrefix({ ignore: [/ng-/, 'some-class-to-ignore']}))
.process(css);
MIT
- Use it with react-prefix-loader for React components
- Inspired by postcss-class-prefix