UNPKG

5.11 kBMarkdownView Raw
1# css-modulesify
2
3[![Build Status](https://travis-ci.org/css-modules/css-modulesify.svg?branch=master)](https://travis-ci.org/css-modules/css-modulesify)
4
5A browserify plugin to load [CSS Modules].
6
7[CSS Modules]: https://github.com/css-modules/css-modules
8
9**Please note that this is still highly experimental.**
10
11## Why CSS Modules?
12
13Normally 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.
14
15Read Mark Dalgleish's excellent ["End of Global CSS"](https://medium.com/seek-ui-engineering/the-end-of-global-css-90d2a4a06284) and check out [css-modules](https://github.com/css-modules/css-modules) for more context.
16
17## Getting started
18
19First install the package: `npm install --save css-modulesify`
20
21Then you can use it as a browserify plugin, eg: `browserify -p [ css-modulesify -o dist/main.css ] example/index.js`
22
23Inside `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.
24
25So to apply a class to an element you can do something like:
26
27```js
28var styles = require('./styles.css');
29var div = `<div class="${styles.inner}">...</div>`;
30```
31
32The 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.
33
34## API Usage
35
36```js
37var b = require('browserify')();
38
39b.add('./main.js');
40b.plugin(require('css-modulesify'), {
41 rootDir: __dirname,
42 output: './path/to/my.css'
43});
44
45b.bundle();
46```
47
48```js
49// or, get the output as a stream
50var b = require('browserify')();
51var fs = require('fs');
52
53b.add('./main.js');
54b.plugin(require('css-modulesify'), {
55 rootDir: __dirname
56});
57
58var bundle = b.bundle()
59bundle.on('css stream', function (css) {
60 css.pipe(fs.createWriteStream('mycss.css'));
61});
62```
63
64### Options:
65
66- `rootDir`: absolute path to your project's root directory. This is optional but providing it will result in better generated classnames.
67- `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.
68- `jsonOutput`: optional path to write a json manifest of classnames.
69- `use`: optional array of postcss plugins (by default we use the css-modules core plugins).
70- `generateScopedName`: (API only) a function to override the default behaviour of creating locally scoped classnames.
71
72### Events
73- `b.bundle().on('css stream', callback)` The callback is called with a readable stream containing the compiled CSS. You can write this to a file.
74
75## Using CSS Modules on the backend
76
77If you want to use CSS Modules in server-generated templates there are a couple of options:
78
79- Option A (nodejs only): register the [require-hook](https://github.com/css-modules/css-modules-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.
80
81- Option B: configure the `jsonOutput` option with a file path and `css-modulesify` will generate a JSON manifest of classnames.
82
83
84## PostCSS Plugins
85
86The following PostCSS plugins are enabled by default:
87
88 * [postcss-modules-local-by-default]
89 * [postcss-modules-extract-imports]
90 * [postcss-modules-scope]
91 * [postcss-modules-values]
92
93(i.e. the [CSS Modules] specification).
94
95You can override the default PostCSS Plugins (and add your own) by passing `--use|-u` to `css-modulesify`.
96
97Or 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).
98
99In addition you may also wish to configure defined PostCSS plugins by passing `--plugin.option true`.
100
101An example of this would be:
102
103```
104browserify -p [css-modulesify \
105 --after autoprefixer --autoprefixer.browsers '> 5%' \
106 -o dist/main.css] -o dist/index.js src/index.js
107```
108
109[postcss-modules-local-by-default]: https://github.com/css-modules/postcss-modules-local-by-default
110[postcss-modules-extract-imports]: https://github.com/css-modules/postcss-modules-extract-imports
111[postcss-modules-scope]: https://github.com/css-modules/postcss-modules-scope
112[postcss-modules-values]: https://github.com/css-modules/postcss-modules-values
113
114## Building for production
115
116If you set `NODE_ENV=production` then `css-modulesify` will generate shorter (though less useful) classnames.
117
118You can also manually switch to short names by setting the `generateScopedName` option. Eg:
119
120```
121browserify.plugin(cssModulesify, {
122 rootDir: __dirname,
123 output: './dist/main.css',
124 generateScopedName: cssModulesify.generateShortName
125})
126```
127
128## Example
129
130An example implementation can be found [here](https://github.com/css-modules/browserify-demo).
131
132## Licence
133
134MIT
135
136## With thanks
137
138 - Tobias Koppers
139 - Mark Dalgleish
140 - Glen Maddern
141
142----
143Josh Johnston, 2015.