Skip to content

Commit 8cfd80c

Browse files
authored
Describe how to use pug with CSS Modules (#95)
* Describe how to use pug with CSS Modules * Remove unwanted spaces from README
1 parent 4cf541f commit 8cfd80c

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

README.md

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,58 @@ Install [eslint-plugin-react-pug](https://github.com/ezhlobo/eslint-plugin-react
131131

132132
### CSS Modules
133133

134-
It's not supported well yet.
134+
Whether you use babel plugin to turn on CSS Modules specifically for JSX (e.g. [babel-plugin-react-css-modules](https://github.com/gajus/babel-plugin-react-css-modules)) or use webpack loader for that to transform styles into key-value object, it's possible to use it with pug.
135+
136+
* With [babel-plugin-react-css-modules](https://github.com/gajus/babel-plugin-react-css-modules) you need to set [`classAttribute`](#classattribute) option to `styleName` value and that's it.
137+
138+
```rc
139+
{
140+
"plugins": [
141+
["transform-react-pug", {
142+
"classAttribute": "styleName"
143+
}]
144+
]
145+
}
146+
```
147+
148+
```jsx
149+
import './styles.css' // .hello{color:red}
150+
151+
const withCorrectStyles = pug`
152+
div.hello I am a red text
153+
`
154+
```
155+
156+
* With webpack loader or other approaches which transform styles into object
157+
158+
```jsx
159+
import classes from './styles.css' // .hello{color:green}
160+
161+
const withCorrectStyles = pug`
162+
div(className=classes.hello) I am a green text
163+
`
164+
```
165+
166+
The developer experience can be improved here by setting [`classAttribute`](#classattribute) option to `styleName` value and adding [babel-plugin-transform-jsx-css-modules](https://github.com/ezhlobo/babel-plugin-transform-jsx-css-modules)
167+
168+
```rc
169+
{
170+
"plugins": [
171+
["transform-react-pug", {
172+
"classAttribute": "styleName"
173+
}],
174+
"transform-jsx-css-modules"
175+
]
176+
}
177+
```
178+
179+
```jsx
180+
import './styles.css' // .hello{color:green}
181+
182+
const withCorrectStyles = pug`
183+
div.hello I am a green text
184+
`
185+
```
135186

136187
## Install
137188

0 commit comments

Comments
 (0)