• Stars
    star
    406
  • Rank 106,093 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 9 years ago
  • Updated almost 5 years ago

Reviews

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

Repository Details

A browserify plugin to load CSS Modules

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();
// or, get the output as a stream
var b = require('browserify')();
var fs = require('fs');

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

var bundle = b.bundle()
b.on('css stream', function (css) {
  css.pipe(fs.createWriteStream('mycss.css'));
});

Options:

  • rootDir: absolute path to your project's root directory. This is optional but providing it will result in better generated classnames. css-modulesify will try to use the browserify basedir if rootDir is not specified, if both are not specified it will use the location from which the command was executed.
  • output: path to write the generated css. If not provided, you'll need to listen to the 'css stream' event on the bundle to get the output.
  • 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). NOTE: it's safer to use after
  • before: optional array of postcss plugins to run before the required css-modules core plugins are run.
  • after: optional array of postcss plugins to run after the required css-modules core plugins are run.
  • generateScopedName: (API only) a function to override the default behaviour of creating locally scoped classnames.
  • global: optional boolean. Set to true if you want css-modulesify to apply to node_modules as well as local files. You can read more about it in the browserify docs.
  • filePattern: optional regular expression string to specify css file names. (default: \.css$)
  • cache: optional object to persist cache between runs.

Events

  • b.on('css stream', callback) The callback is called with a readable stream containing the compiled CSS. You can write this to a file.

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 the same way, add extra plugins to postcssBefore to run the before the defaults.

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

Building for production

If you set NODE_ENV=production then css-modulesify will generate shorter (though less useful) classnames.

You can also manually switch to short names by setting the generateScopedName option. Eg:

browserify.plugin(cssModulesify, {
  rootDir: __dirname,
  output: './dist/main.css',
  generateScopedName: cssModulesify.generateShortName
})

Example

An example implementation can be found here.

Licence

MIT

With thanks

  • Tobias Koppers
  • Mark Dalgleish
  • Glen Maddern

Josh Johnston, 2015.

More Repositories

1

css-modules

Documentation about css-modules
17,507
star
2

webpack-demo

Working demo of CSS Modules, using Webpack's css-loader in module mode
JavaScript
1,487
star
3

icss

Interoperable CSS — a standard for loadable, linkable CSS
612
star
4

css-modules-require-hook

A require hook to compile CSS Modules in runtime
JavaScript
485
star
5

postcss-icss-values

Pass arbitrary constants between your module files
JavaScript
202
star
6

css-modules-loader-core

A loader-agnostic CSS Modules implementation, based on PostCSS
JavaScript
92
star
7

browserify-demo

A working demo of CSS Modules, using css-modulesify
JavaScript
87
star
8

css-selector-tokenizer

Parses and stringifies CSS selectors.
JavaScript
84
star
9

postcss-icss-composes

A CSS Modules transform to extract export statements from local-scope classes
JavaScript
55
star
10

jspm-demo

CSS Modules & JSPM
JavaScript
52
star
11

postcss-modules-extract-imports

A CSS Modules transform to extract local aliases for inline imports
JavaScript
40
star
12

postcss-modules-values

Pass arbitrary constants between your module files
JavaScript
35
star
13

postcss-modules-scope

A CSS Modules transform to extract export statements from local-scope classes
JavaScript
23
star
14

icss-utils

Search & replace tokens during the linking stage of ICSS loading
JavaScript
22
star
15

postcss-modules-local-by-default

PostCSS plugin for css modules to local-scope classes and ids
JavaScript
20
star
16

cssm-rails

WIP of a Rails plugin for CSS Modules
Ruby
17
star
17

generic-names

Helper for building generic names, similar to webpack
JavaScript
15
star
18

postcss-modules-resolve-imports

Resolves ICSS imports
JavaScript
9
star
19

postcss-modules-lint

PostCSS plugin to verify the scoping of selectors in CSS Modules
JavaScript
8
star
20

logos

Official CSS Modules logos
5
star
21

postcss-icss-keyframes

PostCSS plugin for css-modules to local-scope keyframes
JavaScript
5
star
22

postcss-icss

Postcss plugin to process css modules and extract tokens
JavaScript
4
star
23

postcss-icss-import

PostCSS plugin for css-modules to convert @import statements to ICSS
JavaScript
2
star
24

postcss-icss-url

JavaScript
2
star