• Stars
    star
    447
  • Rank 97,700 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 11 years ago
  • Updated about 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

✨ Modular CSS bundler for browserify

sheetify

NPM version build status Test coverage Downloads js-standard-style

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 = require('sheetify')
const html = require('nanohtml')

const prefix = css`
  :host > h1 {
    text-align: center;
  }
`

const tree = html`
  <section class=${prefix}>
    <h1>My beautiful, centered title</h1>
  </section>
`

document.body.appendChild(tree)

Compile with browserify using -t sheetify:

$ browserify -t sheetify index.js > bundle.js

CSS classes are namespaced based on the content hash:

._60ed23ec9f > h1 {
  text-align: center;
}

And the rendered HTML includes the namespace:

<section class="_60ed23ec9f">
  <h1>My beautiful, centered title</h1>
</section>

Styling host elements

The element that gets a prefix applied can be styled using the :host pseudoselector:

const css = require('sheetify')
const html = require('nanohtml')

const prefix = css`
  :host {
    background-color: blue;
  }

  :host > h1 {
    text-decoration: underline;
  }
`

const tree = html`
  <section class=${prefix}>
    <h1>My beautiful, centered title</h1>
  </section>
`

document.body.appendChild(tree)

By using :host we are able to provide styles for the parent element:

._60ed23ec9f {
  background-color: blue;
}

._60ed23ec9f > h1 {
  text-decoration: underline;
}
<section class="_60ed23ec9f">
  <h1>My beautiful, centered title</h1>
</style>

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 = require('sheetify')
const html = require('nanohtml')

const sectionWidth = '100px';
const prefix = css`
  :host {
    background-color: blue;
  }

  :host > h1 {
    text-decoration: underline;
  }
`

const tree = html`
  <section class=${prefix} style="width:${sectionWidth}">
    <h1>My beautiful, centered title</h1>
  </section>
`

document.body.appendChild(tree)

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: global.window.innerWidth,
  height: global.window.innerHeight,
}

let dynamicStyleString = JSON.stringify(dynamicStyles)
    .replace(/\,/g,';')
    .replace(/\"/g,'')
    .replace(/\{|\}/g,'')

const tree = html`
  <div style="${dynamicStyleString}">
    <h1>My beautiful, window width</h1>
  </div>
`

External files

To include an external CSS file you can pass a path to sheetify as sheetify('./my-file.css'):

const css = require('sheetify')
const html = require('nanohtml')

const prefix = css('./my-styles.css')

const tree = html`
  <section class=${prefix}>
    <h1>My beautiful, centered title</h1>
  </section>
`

document.body.appendChild(tree)

Use npm packages

To consume a package from npm that has .css file in its main or style field:

const css = require('sheetify')

css('normalize.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 = require('sheetify')
const html = require('nanohtml')

const prefix = css`
  h1 {
    transform: translate(0, 0);
  }
`

const tree = html`
  <section class=${prefix}>
    <h1>My beautiful, centered title</h1>
  </section>
`

document.body.appendChild(tree)

Compile with browserify using -t [ sheetify -t sheetify-cssnext ]:

$ browserify -t [ sheetify -t sheetify-cssnext ] index.js > bundle.js

Transforms the CSS into:

h1 {
  -webkit-transform: translate(0, 0);
          transform: translate(0, 0);
}

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 = require('browserify')

const b = browserify(path.join(__dirname, 'transform/source.js'))
b.transform('sheetify', { transform: [ 'sheetify-cssnext' ] })
b.bundle().pipe(process.stdout)

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

License

MIT

More Repositories

1

css-extract

Extract CSS from a browserify bundle
JavaScript
46
star
2

css-wipe

Reset the browser's styles
CSS
42
star
3

inline-critical-css

Inline critical CSS in HTML
JavaScript
28
star
4

ergonomic-breakpoint

Ergonomic CSS media queries
CSS
28
star
5

sheetify-cssnext

cssnext transform for sheetify. Use tomorrow's CSS syntax, today
JavaScript
13
star
6

css-electron-reset

Reset Electron stylesheets so it behaves like a native application
CSS
13
star
7

sheetify-postcss

postcss transform for sheetify, use all the plugins!
JavaScript
12
star
8

sheetify-nested

Sheetify plugin to unwrap nested rules
JavaScript
11
star
9

css-type-base

Scalar CSS typography
CSS
9
star
10

style-resolve

A wrapper for the "resolve" module that targets CSS instead of JavaScript
JavaScript
7
star
11

text-fit

Apply text styles based on the shape of the text
JavaScript
7
star
12

sheetify-sass

SASS plugin for Sheetify
JavaScript
6
star
13

css-scale

Scalar CSS variables
CSS
5
star
14

sheetify-inline

a plugin transforms img to base64 for sheetify
JavaScript
4
star
15

postcss-prefix

Prefix CSS selectors
JavaScript
4
star
16

style-deps

Traverse the dependency graph of a CSS project using npm-style import statements
JavaScript
3
star
17

sheetify-variables

Spec compliant CSS variables syntax for sheetify
JavaScript
2
star
18

logo

sheetify logo
2
star
19

sheetify-sibling

A sheetify plugin add sibling class selector to any first level class selector.
JavaScript
1
star
20

sheetify-custom-media

Spec compliant CSS custom media query syntax for sheetify
JavaScript
1
star
21

sheetify-check

check css syntax with sheetify
JavaScript
1
star
22

sheetify-calc

Spec compliant CSS calc syntax for sheetify
JavaScript
1
star