sheetify
Modular CSS bundler for browserify. Works with npm modules like browserify does.
Features
- clarity: namespace CSS, no more need for naming schemes
- modularity: import and reuse CSS packages from npm
- extensibility: transform CSS using existing transforms, or write your own
- transparency: inline CSS in your views
- simplicity: tiny API surface and a minimal code base
Example
Given some inline CSS:
const css = const html = const prefix = css` :host > h1 { text-align: center; }` const tree = html` My beautiful, centered title ` documentbody
Compile with browserify using -t sheetify
:
$ browserify -t sheetify index.js > bundle.js
CSS classes are namespaced based on the content hash:
And the rendered HTML includes the namespace:
My beautiful, centered title
Styling host elements
The element that gets a prefix applied can be styled using the :host
pseudoselector:
const css = const html = const prefix = css` :host { background-color: blue; } :host > h1 { text-decoration: underline; }` const tree = html` My beautiful, centered title ` documentbody
By using :host
we are able to provide styles for the parent element:
My beautiful, centered title
Dynamic vs static
Sheetify is very good for namespacing static css assets in your javaScript code. Currently there is no support for dynamic variables within sheetify, however you could achieve this by setting the inline style property of an element.
const css = const html = const sectionWidth = '100px';const prefix = css` :host { background-color: blue; } :host > h1 { text-decoration: underline; }` const tree = html` My beautiful, centered title ` documentbody
Should you want to, you could even set dynamic variables in an object and do a rather complicated JSON.stringify with a replace on that object to turn it into a style for an element.
const dynamicStyles = width: globalwindowinnerWidth height: globalwindowinnerHeight let dynamicStyleString = JSON const tree = html` My beautiful, window width `
External files
To include an external CSS file you can pass a path to sheetify as
sheetify('./my-file.css')
:
const css = const html = const prefix = const tree = html` My beautiful, centered title ` documentbody
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
To write the compiled CSS to a separate file, rather than keep it in the bundle use css-extract:
$ browserify index.js \ -t [ sheetify ] \ -p [ css-extract -o bundle.css ] index.js \ -o bundle.js
css-extract can also write to a stream from the JS api, look at the documentation to see how.
Transforms
Sheetify uses transforms that take CSS and apply a transform. For example include sheetify-cssnext to support autoprefixing, variables and more:
const css = const html = const prefix = css` h1 { transform: translate(0, 0); }` const tree = html` My beautiful, centered title ` documentbody
Compile with browserify using -t [ sheetify -t sheetify-cssnext ]
:
$ browserify -t [ sheetify -t sheetify-cssnext ] index.js > bundle.js
Transforms the CSS into:
API
Browserify transforms accept either flags from the command line using subargs:
$ browserify -t [ sheetify -t sheetify-cssnext ] index.js > bundle.js
Or the equivalent options by passing in a configuration object in the JavaScript API:
const browserify = const b = bb
The following options are available:
Options: -t, --transform Consume a sheetify transform
Installation
$ npm install sheetify
See Also
- browserify - browser-side require() the node.js way
- glslify - module system for GLSL shaders
- hyperx - transform inline HTML to JS
- bankai - DIY server middleware for JS, CSS and HTML
- css-extract: extract CSS from a browserify bundle