|
1 | 1 | css-modules-require-hook
|
2 | 2 | ========================
|
3 | 3 |
|
4 |
| -Automatically compiles a CSS Module to a low-level interchange format called ICSS or [Interoperable CSS](https://github.com/css-modules/icss). |
| 4 | +The require hook compiles [CSS Modules](https://github.com/css-modules/css-modules) in runtime. This is similar to Babel's [babel/register](https://babeljs.io/docs/usage/require/). |
5 | 5 |
|
6 |
| -One of the ways you can compile [CSS Modules](https://github.com/css-modules/css-modules) to the ICSS format is through the require hook. The require hook will bind itself to node's require and automatically compile files on the fly. This is similar to Babel's [babel/register](https://babeljs.io/docs/usage/require/). |
| 6 | +## What is CSS Modules |
7 | 7 |
|
8 |
| -## Requirements |
| 8 | +A **CSS Module** is a CSS file in which all class names and animation names are scoped locally by default. Learn more in the article [CSS Modules - Welcome to the Future](http://glenmaddern.com/articles/css-modules) by Glen Maddern. |
9 | 9 |
|
10 |
| -To use this tool we require postcss and few plugins to be installed on your project. See the list below: |
| 10 | +## Features |
11 | 11 |
|
12 |
| -- `"postcss": "4.x"` |
13 |
| -- `"postcss-modules-extract-imports": "0.x"` |
14 |
| -- `"postcss-modules-local-by-default": "0.x"` |
15 |
| -- `"postcss-modules-scope": "0.x"` |
16 |
| - |
17 |
| -## Install |
18 |
| - |
19 |
| -```bash |
20 |
| -$ npm i css-modules-require-hook |
21 |
| -``` |
| 12 | +Compiling in runtime, [universal](https://medium.com/@mjackson/universal-javascript-4761051b7ae9) usage. |
22 | 13 |
|
23 | 14 | ## Usage
|
24 | 15 |
|
25 |
| -```javascript |
26 |
| -require('css-modules-require-hook'); |
27 |
| -``` |
| 16 | +### Requirements |
28 | 17 |
|
29 |
| -## Available options |
| 18 | +To use this tool we require [Node.js v0.12.x](https://github.com/nodejs/node) (or higher) and several modules to be installed. |
30 | 19 |
|
31 |
| -Providing additional options allows you to get advanced experience. See the variants below. |
| 20 | +- [postcss](https://github.com/postcss/postcss) version 4 or higher |
| 21 | +- [postcss-modules-extract-imports](https://github.com/css-modules/postcss-modules-extract-imports) |
| 22 | +- [postcss-modules-local-by-default](https://github.com/css-modules/postcss-modules-local-by-default) |
| 23 | +- [postcss-modules-scope](https://github.com/css-modules/postcss-modules-scope) |
32 | 24 |
|
33 |
| -```javascript |
34 |
| -var hook = require('css-modules-require-hook'); |
35 |
| -hook({ /* options */ }); |
36 |
| -``` |
| 25 | +### Installation |
37 | 26 |
|
38 |
| -### `rootDir` option |
| 27 | +```bash |
| 28 | +$ npm i css-modules-require-hook |
| 29 | +``` |
39 | 30 |
|
40 |
| -Aliases are `root`, `d`. |
| 31 | +### Tuning (options) |
41 | 32 |
|
42 |
| -Absolute path to your project's root directory. This is optional but providing it will result in better generated classnames. It can be obligatory, if you run require hook and build tools, like [css-modulesify](https://github.com/css-modules/css-modulesify) from different working directories. |
| 33 | + * `function` **createImportedName** — short alias for the [postcss-modules-extract-imports](https://github.com/css-modules/postcss-modules-extract-imports) plugin's `createImportedName` option. |
| 34 | + * `function` **generateScopedName** — short alias for the [postcss-modules-scope](https://github.com/css-modules/postcss-modules-scope) plugin's option. Helps you to specify the custom way to build generic names for the class selectors. |
| 35 | + * `function` **processCss** — in rare cases you may want to get compiled styles in runtime, so providing this option helps. |
| 36 | + * `string` **rootDir** — absolute path to the project directory. Providing this will result in better generated class names. It can be obligatory, if you run require hook and build tools (like [css-modulesify](https://github.com/css-modules/css-modulesify)) from different working directories. |
| 37 | + * `string` **to** — provides `to` option to the [LazyResult instance](https://github.com/postcss/postcss/blob/master/docs/api.md#processorprocesscss-opts). |
| 38 | + * `array` **use** — custom subset of postcss plugins. |
43 | 39 |
|
44 |
| -### `use` option |
| 40 | +### Examples |
45 | 41 |
|
46 |
| -Alias is `u`. |
| 42 | +Basically you need to require this plugin before other requires for your styles will occur. |
| 43 | +For example: |
47 | 44 |
|
48 |
| -Custom list of plugins. This is optional but helps you to extend list of basic [postcss](https://github.com/postcss/postcss) plugins. Also helps to specify options for particular plugins. |
| 45 | +*icon.css* |
| 46 | +```css |
| 47 | +.icon |
| 48 | +{ |
| 49 | + composes: fa fa-hand-peace-o from 'font-awesome/css/font-awesome.css'; |
| 50 | +} |
| 51 | +``` |
49 | 52 |
|
50 |
| -### `createImportedName` option |
| 53 | +*server.js* |
| 54 | +```javascript |
| 55 | +require('css-modules-require-hook'); |
51 | 56 |
|
52 |
| -Alias for the `createImportedName` option from the [postcss-modules-extract-imports](https://github.com/css-modules/postcss-modules-extract-imports) plugin. This is optional. Won't work if you `use` option. |
| 57 | +var styles = require('./icon.css'); |
| 58 | +var html = '<i class="' + styles.icon + '"></i>'; |
| 59 | +// send it somehow :) |
| 60 | +``` |
53 | 61 |
|
54 |
| -### `generateScopedName` option |
| 62 | +You'll get: |
55 | 63 |
|
56 |
| -Custom function to generate class names. This is optional. Alias for the `generateScopedName` option from the [postcss-modules-scope](https://github.com/css-modules/postcss-modules-scope) plugin. Won't work if you `use` option. |
| 64 | +```html |
| 65 | +<i class="_icon_font-awesome-css-font-awesome__fa _icon_font-awesome-css-font-awesome__fa-hand-peace-o"></i>' |
| 66 | +``` |
57 | 67 |
|
58 |
| -### `mode` option |
| 68 | +In rare cases you may want to tune the require hook for better experience. |
59 | 69 |
|
60 |
| -Alias for the `mode` option from the [postcss-modules-local-by-default](https://github.com/css-modules/postcss-modules-local-by-default) plugin. This is optional. Won't work if you `use` option. |
| 70 | +```javascript |
| 71 | +var hook = require('css-modules-require-hook'); |
| 72 | +var path = require('path'); |
61 | 73 |
|
62 |
| -## Examples |
| 74 | +hook({ |
| 75 | + // setting root to the parent directory |
| 76 | + rootDir: path.join(__dirname, '..') |
| 77 | +}); |
| 78 | +``` |
63 | 79 |
|
64 |
| -If you want to add custom functionality, for example [CSS Next](http://cssnext.io/setup/#as-a-postcss-plugin) plugin, you should provide the `use` option. |
| 80 | +If you want to add any postcss plugins to the pipeline - you should use the `use` option. |
65 | 81 |
|
66 | 82 | ```javascript
|
67 | 83 | var hook = require('css-modules-require-hook');
|
|
0 commit comments