Skip to content

Commit 0bbda35

Browse files
committed
Merge pull request #61 from css-modules/docs
readme update
2 parents 6792228 + 77609c3 commit 0bbda35

File tree

1 file changed

+54
-44
lines changed

1 file changed

+54
-44
lines changed

demo/readme.md

Lines changed: 54 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,101 @@
11
Universal Usage demo
22
====================
33

4-
Small demo of configuring [css-modules-require-hook](https://github.com/css-modules/css-modules-require-hook/) with [webpack](https://webpack.github.io/) and [react](https://facebook.github.io/react/). See the detailed description below.
4+
It's a small demo to show how to set up [css-modules-require-hook](https://github.com/css-modules/css-modules-require-hook/) with [webpack](https://webpack.github.io/) and [react](https://facebook.github.io/react/). If you are familiar with the technologies you can jump to the quick start. Otherwise, you can find detailed description below.
5+
56

67
## Quick start
78

9+
Make sure that you have [Node.js](https://nodejs.org/en/) and [npm](https://www.npmjs.com/) and run these commands:
10+
811
```bash
912
$ npm install
1013
$ npm run compile
1114
$ npm run start
1215
```
1316

14-
## Description
17+
Open <a href="http://localhost:3000/" target="_blank">http://localhost:3000/</a>.
1518

16-
Hi, I tried to make a simple demo. So if you are familiar with technologies [webpack](https://webpack.github.io/), [react](https://facebook.github.io/react/) and [express](http://expressjs.com/), then it will be easy for to understand that example. Anyways, I'll point on the main parts to save your time.
1719

18-
### Backend
20+
## Detailed description
1921

20-
In this demo I use [express](http://expressjs.com/) to handle user requests and [react](https://facebook.github.io/react/) components to serve html for them:
22+
In short [CSS&nbsp;Modules](https://github.com/css-modules/css-modules) provide modularity with generated class names. Therefore, generated names should be present in CSS styles and in templates which form resulting html. Since, we talk about server rendering in the current example I'll show you how to set require hook to generate the same names in runtime as the CSS styles.
2123

22-
- **app/**
23-
- `view-engine.js`
24-
- `worker.js`
25-
- **components/**
26-
- `Page.js`
2724

28-
#### `worker.js`
25+
### Frontend
26+
27+
The modern frontend is so tough that you have to use particular bundler systems in order to generate a simple CSS file. My favourite one is [webpack](https://webpack.github.io/), so I'll show you how to set it with the require hook.
2928

30-
Is an entry point for the server application. It contains main middlewares and helpers for the server rendering. Here I attach require hook:
29+
To understand [webpack](https://webpack.github.io/) configs you should be familiar with [loaders](https://webpack.github.io/docs/using-loaders.html). In order to use [CSS&nbsp;Modules](https://github.com/css-modules/css-modules) with [webpack](https://webpack.github.io/) you should set [css-loader](https://github.com/webpack/css-loader#css-modules). Also [extract-text-webpack-plugin](https://github.com/webpack/extract-text-webpack-plugin) provides you possibility to create pure CSS file. So eventually, your configuration should look similar to this:
3130

3231
```javascript
33-
require('css-modules-require-hook/preset');
32+
module: {
33+
loaders: [
34+
{
35+
test: /\.css$/i,
36+
loader: ExtractTextPlugin.extract('style',
37+
'css?modules&localIdentName=[name]_[local]__[hash:base64:5]'),
38+
},
39+
],
40+
},
41+
42+
plugins: [
43+
new ExtractTextPlugin('styles.css', {
44+
allChunks: true
45+
}),
46+
],
3447
```
3548

36-
It helps to process calls to the css files in runtime and build necessary class names:
49+
### Backend
3750

38-
```javascript
39-
import styles from './Page.css'; // Page.js
40-
```
51+
I use [express](http://expressjs.com/) to handle user requests and [react](https://facebook.github.io/react/) components to serve html for them. In order to make it independent and good looking I decided to use a custom [template engine](http://expressjs.com/en/advanced/developing-template-engines.html) to isolate all the rendering stuff and to have neat calls in the middlewares. So, here is my structure:
4152

42-
Also, I made a small [template engine](http://expressjs.com/en/advanced/developing-template-engines.html) for express to make render step isolated from the main program. It's connected here:
53+
- *app/*
54+
- `view-engine.js`
55+
- `worker.js`
56+
57+
#### `worker.js`
58+
59+
Is an entry point for the app. It sets react [template engine](http://expressjs.com/en/advanced/developing-template-engines.html):
4360

4461
```javascript
45-
// setting rendering engine
62+
// sets react rendering engine
4663
app.engine('js', viewEngine);
4764
app.set('views', path.join(__dirname, '../components'));
4865
app.set('view engine', 'js');
4966
```
5067

51-
and implemented in the `view-engine.js` file. So, I can use neat calls to build html:
68+
and declares basic middlewares:
5269

5370
```javascript
5471
app.get('/', (req, res) => res.render('Page'));
5572
```
5673

5774
#### `view-engine.js`
5875

59-
Is a [template engine](http://expressjs.com/en/advanced/developing-template-engines.html) implementation. Requires necessary react components and builds html.
60-
61-
#### `Page.js`
62-
63-
Main react component, which describes the page and contains all the necessary dependencies.
76+
Describes the simple [template engine](http://expressjs.com/en/advanced/developing-template-engines.html) and attaches require hook:
6477

6578
```javascript
66-
// get the necessary class names
67-
import styles from './Page.css';
68-
69-
// pass particular generated class name to the component
70-
<section className={ styles.wrapper }>
71-
// ...
72-
</section>
79+
// teaches node.js to load css files
80+
// uses external config cmrh.conf.js
81+
require('css-modules-require-hook/preset');
7382
```
7483

75-
### Frontend
84+
**Note** that to generate the same names in runtime as the CSS styles you should provide the same pattern to [webpack](https://webpack.github.io/) and to the require hook.
7685

77-
The modern frontend is so tough that you have to use particular bundler systems in order to build necessary styles and scripts. My favourite one is [webpack](https://webpack.github.io/), so I'll describe how to configure it. Usually to build necessary styles using [CSS&nbsp;Modules](https://github.com/css-modules/) you have to use a [css-loader](https://github.com/webpack/css-loader):
86+
Use `localIdentName` for [webpack](https://webpack.github.io/):
7887

7988
```javascript
80-
module: {
81-
loaders: [
82-
{
83-
test: /\.css$/i,
84-
loader: ExtractTextPlugin.extract('style',
85-
`css?modules&localIdentName=[name]_[local]__[hash:base64:5]`),
86-
},
87-
],
88-
},
89+
loader: ExtractTextPlugin.extract('style',
90+
'css?modules&localIdentName=[name]_[local]__[hash:base64:5]'),
8991
```
9092

91-
In this example I provide a custom template for the generic class names `[name]_[local]__[hash:base64:5]` which is also used by require hook (see the `cmrh.conf.js` file).
93+
and `generateScopedName` for the require hook. For example, if you use presets then you can put it to the `cmrh.conf.js` file:
94+
95+
```javascript
96+
module.exports = {
97+
// the custom template for the generic classes
98+
generateScopedName: '[name]_[local]__[hash:base64:5]',
99+
};
100+
101+
```

0 commit comments

Comments
 (0)