0.3.8 • Published 9 years ago
postcss-filename-prefixes v0.3.8
postcss-filename-prefix
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
Installation
npm install postcss-filename-prefixesUsage
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);Using the delimeter option
By default delimeter between prefix and class is dash character. You can setup any string like:
filenamePrefix({delimeter: '__'})Using the lowerCase option
Force using of lowecased prefix:
filenamePrefix({lowerCase: true})Using the ignoreRoot option
Omit .root class
Using the ignoreLowerCasedFileName option
Allow to use lowercase name for css file
filenamePrefix({ignoreRoot: true})Using the ignore option
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);License
MIT
Recommendation
- Use it with react-prefix-loader for React components
Acknowledgements
- Inspired by postcss-class-prefix