Giter Club home page Giter Club logo

css-modulesify's Introduction

css-modulesify

Build Status

A browserify plugin to load CSS Modules.

Please note that this is still highly experimental.

Why CSS Modules?

Normally you need to use a strict naming convention like BEM to ensure that one component's CSS doesn't collide with another's. CSS Modules are locally scoped, which allows you to use names that are meaningful within the context of the component, without any danger of name collision.

Read Mark Dalgleish's excellent "End of Global CSS" and check out css-modules for more context.

Getting started

First install the package: npm install --save css-modulesify

Then you can use it as a browserify plugin, eg: browserify -p [ css-modulesify -o dist/main.css ] example/index.js

Inside example/index.js you can now load css into your scripts. When you do var box1 = require('./box1.css'), box1 will be an object to lookup the localized classname for one of the selectors in that file.

So to apply a class to an element you can do something like:

var styles = require('./styles.css');
var div = `<div class="${styles.inner}">...</div>`;

The generated css will contain locally-scoped versions of any css you have require'd, and will be written out to the file you specify in the --output or -o option.

API Usage

var b = require('browserify')();

b.add('./main.js');
b.plugin(require('css-modulesify'), {
  rootDir: __dirname,
  output: './path/to/my.css'
});

b.bundle();

Options:

  • rootDir: absolute path to your project's root directory. This is optional but providing it will result in better generated classnames.
  • output: path to write the generated css.
  • jsonOutput: optional path to write a json manifest of classnames.
  • use: optional array of postcss plugins (by default we use the css-modules core plugins).

Using CSS Modules on the backend

If you want to use CSS Modules in server-generated templates there are a couple of options:

  • Option A (nodejs only): register the require-hook so that var styles = require('./foo.css') operates the same way as on the frontend. Make sure that the rootDir option matches to guarantee that the classnames are the same.

  • Option B: configure the jsonOutput option with a file path and css-modulesify will generate a JSON manifest of classnames.

PostCSS Plugins

The following PostCSS plugins are enabled by default:

(i.e. the CSS Modules specification).

You can override the default PostCSS Plugins (and add your own) by passing --use|-u to css-modulesify.

Or if you just want to add some extra plugins to run after the default, add them to the postcssAfter array option (API only at this time).

In addition you may also wish to configure defined PostCSS plugins by passing --plugin.option true.

An example of this would be:

browserify -p [css-modulesify \
  --after autoprefixer --autoprefixer.browsers '> 5%' \
  -o dist/main.css] -o dist/index.js src/index.js

Example

An example implementation can be found here.

Licence

MIT

With thanks

  • Tobias Koppers
  • Mark Dalgleish
  • Glen Maddern

Josh Johnston, 2015.

css-modulesify's People

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.