PostCSS Scopeify Everything
This PostCSS plugin will scopeify the following CSS selectors:
- Converts HTML elements into classes
- Classes
- Ids
- Keyframes
- Font-faces
Use as a plugin
const postcss = ;const scopeify = ;const opts = {}; ;
Use in javascript
This will return an object with the following properties:
- css {string, hidden use
getCss
access}: the raw scopeified CSS - elements {object}: map containing all the HTML element scopeified and converted into classes
- classes {object}: map containing all the class selectors scopeified
- ids {object}: map containing all the id selectors scopeified
- keyframes {object}: map containing all the @keyframe animations that were scopeified
- fontFaces {object}: map containing all the @font-face definitions that were scopeified
Async
const pse = ;const scopeify = pse;const getCss = psegetCss; const css = 'div > .bro { display: flex; height: 50px; }'; ;
Sync
const pse = ;const scopeify = pse;const getCss = psegetCss; const css = '#foo, .bro { display: flex; height: 50px; }'; const scopified = ;console;/* { elements: {}, classes: { bro: 'bro_4uC3yI' }, ids: { foo: 'foo_4uC3yI' }, keyframes: {} }*/console;// #foo_4uC3yI, .bro_4uC3yI { display: flex; height: 50px; }
Only scopeify classes
const pse = ;const scopeify = pse;const getCss = psegetCss; const scopeified = ;console;console;
Modify scope hash
const pse = ;const scopeify = pse;const getCss = psegetCss; const scopeified = ;console;console; { return { return name + '_' + Math; }}
Add PostCSS plugins
const autoprefixer = ;const pse = ;const scopeify = pse;const getCss = psegetCss; const css = '.prefix { display: flex; }';const scopeified = ;console;/* { elements: {}, classes: { prefix: 'prefix_gQhnX' }, ids: {}, keyframes: {} }*/console;// .prefix_gQhnX { display: -webkit-box; display: -ms-flexbox; display: flex; }
No scope
const pse = ;const scopeify = pse;const getCss = psegetCss; const css = '.cool { display: flex; } div { font-size: 12px; }';const scopeified = ;console;/* { elements: { div: 'div_el' }, classes: { cool: 'cool' }, ids: {}, keyframes: {} }*/console;// .cool { display: flex; }// .div_el { font-size: 12px; }
Convert an element into a class with a different postfix.
By default, anytime an element is converted into a class, a special postfix is attached to avoid
class name conflicts, we can override this behavior to set a custom postfix using the
scopeifyElFn
option.
const pse = ;const scopeify = pse;const getCss = psegetCss; const css = 'table { width: 100%; } .table { border: 1px solid black; }';const scopeified = ;console;/* { elements: { table: 'table_special_gQhnX' }, classes: { table: 'table_gQhnX' }, ids: {}, keyframes: {} }*/console;// .table_special_gQhnX { width: 100%; }// .table_gQhnX { border: 1px solid black; }
Wildcard selector
The asterisk selector will be converted into a class using a special name __asterisk
.
const css = '* { font-size: 12px; }';const scopeified = ;console;/* { elements: { '*': '__asterisk_gQhnX' }, classes: {}, ids: {}, keyframes: {} }*/console;// .__asterisk_gQhnX { font-size: 12px; }
Use your own wildcard name
const pse = ;const scopeify = pse;const getCss = psegetCss; const css = '* { font-size: 12px; }';const scopeified = ;console;/* { elements: { '*': '__wildcard__gQhnX' }, classes: {}, ids: {}, keyframes: {} }*/console;// .__wildcard__gQhnX { font-size: 12px; }
Options
- plugins (Array): adds PostCSS plugins before the scopeify plugin
- scopeifyFn (Function): the function that hashes the identifier name
- scopeifyElFn (Function): the function that converts an element name to a class name
- asteriskName (Function|String): the string that is used for the wildcard selector
*
- ids (Boolean): determines whether or not to disable scoping
ids
- elements (Boolean): determines whether or not to disable scoping
elements
- classes (Boolean): determines whether or not to disable scoping
classes
- keyframes (Boolean): determines whether or not to disable scoping
keyframes
- fontFaces (Boolean): determines whether or not to disable scoping
fontFaces