scopedify
Scoped modular CSS bundler for browserify. (folked from sheetify)
Works with npm modules like browserify does.
Example
Given some inline CSS:
const css = const html = const scope = css` .base h1 { text-align: center; }` const tree = documentbody
Compile with browserify using -t scopedify/transform
:
$ browserify -t scopedify/transform index.js > bundle.js
CSS selectors are namespaced based on the content hash:
And the rendered HTML includes the namespace:
My beautiful, centered title
Nested namespaces
CSS cascading will not be occured outer the scope. Components will no longer be polluted with ancestors' styles.
Note: CSS inheritance (color, text-align, ...) will be occured normally.
Use initial
to reset inherited properties.
const css = const html = const componentScope = css` .base h1 { background-color: #aaf; }` const component = const treeScope = css` .base h1 { border: solid 4px #faa; }` const tree = documentbody
Rendered html:
Red Bordered Blue background, no border
Multiple namespaces
If the given html is already scoped, the new scope will be added to nodes which has same scope as root in given html.
const css = const html = const componentScope = css` .base h1 { background-color: #aaf; }` const component = const Scope1 = css` .base h1 { border: solid 4px #faa; }` const Scope2 = css` .base h1 { transform: scale(0.5); }` const tree = documentbody
Rendered html:
<section class="base" _scope_b29bc9f1 _scope_136a89ad>
<h1 _scope_b29bc9f1 _scope_136a89ad>Red Bordered</h1>
<p class="base" _scope_43e26b4d>
<h1 _scope_43e26b4d>Blue background, no border</h1>
</p>
</section>
External files
To include an external CSS file you can pass a path to scopedify as
scopedify('./my-file.css')
:
const css = const html = const scope = const tree = documentbody
Disable namespaces
To disable namespaces for throughout a file:
Selectors are not namespaced under :root
pseudo selector:
Use npm packages
To consume a package from npm that has .css
file in its main
or style
field:
const css =
Packages from npm will not be namespaced by default.
Write to separate file on disk
See sheetify#write-to-separate-file-on-disk
Plugins
See sheetify#plugins
It is compatible with sheetify plugins.
API
See sheetify#api
FAQ
See sheetify#faq
Installation
$ npm install scopedify