2
2
3
3
A [ PostCSS] plugin to use [ CSS Modules] everywhere. Not only at the client side.
4
4
5
- [ PostCSS ] : https://github.com/postcss/postcss
6
- [ ci-img ] : https://travis-ci.org/css-modules/postcss-modules.svg
7
- [ ci ] : https://travis-ci.org/css-modules/postcss-modules
8
- [ CSS Modules ] : https://github.com/css-modules/css-modules
5
+ [ postcss ] : https://github.com/postcss/postcss
6
+ [ ci-img ] : https://travis-ci.org/css-modules/postcss-modules.svg
7
+ [ ci ] : https://travis-ci.org/css-modules/postcss-modules
8
+ [ css modules ] : https://github.com/css-modules/css-modules
9
9
10
10
<a href =" https://evilmartians.com/?utm_source=postcss-modules " >
11
11
<img src =" https://evilmartians.com/badges/sponsored-by-evil-martians.svg " alt =" Sponsored by Evil Martians " width =" 236 " height =" 54 " >
@@ -14,66 +14,65 @@ A [PostCSS] plugin to use [CSS Modules] everywhere. Not only at the client side.
14
14
What is this? For example, you have the following CSS:
15
15
16
16
``` css
17
-
18
17
/* styles.css */
19
18
:global .page {
20
- padding : 20px ;
19
+ padding : 20px ;
21
20
}
22
21
23
22
.title {
24
- composes : title from " ./mixins.css" ;
25
- color : green ;
23
+ composes : title from " ./mixins.css" ;
24
+ color : green ;
26
25
}
27
26
28
27
.article {
29
- font-size : 16px ;
28
+ font-size : 16px ;
30
29
}
31
30
32
31
/* mixins.css */
33
32
.title {
34
- color : black ;
35
- font-size : 40px ;
33
+ color : black ;
34
+ font-size : 40px ;
36
35
}
37
36
38
37
.title :hover {
39
- color : red ;
38
+ color : red ;
40
39
}
41
-
42
40
```
41
+
43
42
After the transformation it will become like this:
44
43
45
44
``` css
46
45
._title_116zl_1 {
47
- color : black ;
48
- font-size : 40px ;
46
+ color : black ;
47
+ font-size : 40px ;
49
48
}
50
49
51
50
._title_116zl_1 :hover {
52
- color : red ;
51
+ color : red ;
53
52
}
54
53
55
54
.page {
56
- padding : 20px ;
55
+ padding : 20px ;
57
56
}
58
57
59
58
._title_xkpkl_5 {
60
- color : green ;
59
+ color : green ;
61
60
}
62
61
63
62
._article_xkpkl_10 {
64
- font-size : 16px ;
63
+ font-size : 16px ;
65
64
}
66
65
```
67
66
68
67
And the plugin will give you a JSON object for transformed classes:
68
+
69
69
``` json
70
70
{
71
71
"title" : " _title_xkpkl_5 _title_116zl_1" ,
72
- "article" : " _article_xkpkl_10" ,
72
+ "article" : " _article_xkpkl_10"
73
73
}
74
74
```
75
75
76
-
77
76
## Usage
78
77
79
78
### Saving exported classes
@@ -84,11 +83,11 @@ use the `getJSON` callback. For example, save data about classes into a correspo
84
83
85
84
``` js
86
85
postcss ([
87
- require (' postcss-modules' )({
86
+ require (" postcss-modules" )({
88
87
getJSON : function (cssFileName , json , outputFileName ) {
89
- var path = require (' path' );
90
- var cssName = path .basename (cssFileName, ' .css' );
91
- var jsonFileName = path .resolve (' ./build/' + cssName + ' .json' );
88
+ var path = require (" path" );
89
+ var cssName = path .basename (cssFileName, " .css" );
90
+ var jsonFileName = path .resolve (" ./build/" + cssName + " .json" );
92
91
fs .writeFileSync (jsonFileName, JSON .stringify (json));
93
92
}
94
93
})
@@ -104,13 +103,13 @@ this behaviour using the `scopeBehaviour` option:
104
103
105
104
``` js
106
105
postcss ([
107
- require (' postcss-modules' )({
108
- scopeBehaviour: ' global' // can be 'global' or 'local',
106
+ require (" postcss-modules" )({
107
+ scopeBehaviour: " global" // can be 'global' or 'local',
109
108
})
110
109
]);
111
110
```
112
111
113
- To define paths for global modules, use the ` globalModulePaths ` option.
112
+ To define paths for global modules, use the ` globalModulePaths ` option.
114
113
It is an array with regular expressions defining the paths:
115
114
116
115
``` js
@@ -125,14 +124,14 @@ To generate custom classes, use the `generateScopedName` callback:
125
124
126
125
``` js
127
126
postcss ([
128
- require (' postcss-modules' )({
127
+ require (" postcss-modules" )({
129
128
generateScopedName : function (name , filename , css ) {
130
- var path = require (' path' );
131
- var i = css .indexOf (' . ' + name);
132
- var line = css .substr (0 , i).split (/ [\r\n ] / ).length ;
133
- var file = path .basename (filename, ' .css' );
129
+ var path = require (" path" );
130
+ var i = css .indexOf (" . " + name);
131
+ var line = css .substr (0 , i).split (/ [\r\n ] / ).length ;
132
+ var file = path .basename (filename, " .css" );
134
133
135
- return ' _ ' + file + ' _ ' + line + ' _ ' + name;
134
+ return " _ " + file + " _ " + line + " _ " + name;
136
135
}
137
136
})
138
137
]);
@@ -143,8 +142,19 @@ Or just pass an interpolated string to the `generateScopedName` option
143
142
144
143
``` js
145
144
postcss ([
146
- require (' postcss-modules' )({
147
- generateScopedName: ' [name]__[local]___[hash:base64:5]' ,
145
+ require (" postcss-modules" )({
146
+ generateScopedName: " [name]__[local]___[hash:base64:5]"
147
+ })
148
+ ]);
149
+ ```
150
+
151
+ It's possible to add custom hash to generate more unique classes using the ` hashPrefix ` option (like in [ css-loader] ( https://webpack.js.org/loaders/css-loader/#hashprefix ) ):
152
+
153
+ ``` js
154
+ postcss ([
155
+ require (" postcss-modules" )({
156
+ generateScopedName: " [name]__[local]___[hash:base64:5]" ,
157
+ hashPrefix: " prefix"
148
158
})
149
159
]);
150
160
```
@@ -155,13 +165,14 @@ If you need, you can pass a custom loader (see [FileSystemLoader] for example):
155
165
156
166
``` js
157
167
postcss ([
158
- require (' postcss-modules' )({
159
- Loader: CustomLoader,
168
+ require (" postcss-modules" )({
169
+ Loader: CustomLoader
160
170
})
161
171
]);
162
172
```
163
173
164
174
You can also pass any needed root path:
175
+
165
176
``` js
166
177
postcss ([
167
178
require (' postcss-modules' )({
@@ -175,22 +186,24 @@ postcss([
175
186
If you need, you can pass the options ` { camelCase: true } ` to transform classes:
176
187
177
188
CSS:
189
+
178
190
``` css
179
191
.post-title {
180
192
color : red ;
181
193
}
182
194
```
183
195
184
196
JSON:
197
+
185
198
``` json
186
199
{
187
200
"post-title" : " ._post-title_116zl_1" ,
188
201
"postTitle" : " ._post-title_116zl_1"
189
202
}
190
203
```
191
204
192
-
193
205
## Integration with templates
206
+
194
207
The plugin only transforms CSS classes to CSS modules.
195
208
But you probably want to integrate these CSS modules with your templates.
196
209
Here are some examples.
@@ -204,70 +217,84 @@ Assume you've saved project's CSS modules in `cssModules.json`:
204
217
}
205
218
```
206
219
207
-
208
220
### Pug (ex-Jade)
221
+
209
222
Let's say you have a Pug template ` about.jade ` :
223
+
210
224
``` jade
211
225
h1(class=css.title) postcss-modules
212
226
article(class=css.article) A PostCSS plugin to use CSS Modules everywhere
213
227
```
214
228
215
229
Render it:
230
+
216
231
``` js
217
- var jade = require (' jade' );
218
- var cssModules = require (' ./cssModules.json' );
219
- var html = jade .compileFile (' about.jade' )({css: cssModules});
232
+ var jade = require (" jade" );
233
+ var cssModules = require (" ./cssModules.json" );
234
+ var html = jade .compileFile (" about.jade" )({ css: cssModules });
220
235
console .log (html);
221
236
```
222
237
223
238
And you'll get the following HTML:
239
+
224
240
``` html
225
241
<h1 class =" _title_xkpkl_5 _title_116zl_1" >postcss-modules</h1 >
226
- <article class =" _article_xkpkl_10" >A PostCSS plugin to use CSS Modules everywhere</article >
242
+ <article class =" _article_xkpkl_10" >
243
+ A PostCSS plugin to use CSS Modules everywhere
244
+ </article >
227
245
```
228
246
229
-
230
247
### HTML
248
+
231
249
For HTML transformation we'll use [ PostHTML] ( https://github.com/posthtml/posthtml ) and [ PostHTML CSS Modules] ( https://github.com/maltsev/posthtml-css-modules ) :
250
+
232
251
``` bash
233
252
npm install --save posthtml posthtml-css-modules
234
253
```
235
254
236
255
Here is our template ` about.html ` :
256
+
237
257
``` html
238
258
<h1 css-module =" title" >postcss-modules</h1 >
239
- <article css-module =" article" >A PostCSS plugin to use CSS Modules everywhere</article >
259
+ <article css-module =" article" >
260
+ A PostCSS plugin to use CSS Modules everywhere
261
+ </article >
240
262
```
241
263
242
264
Transform it:
265
+
243
266
``` js
244
- var fs = require (' fs ' );
245
- var posthtml = require (' posthtml' );
246
- var posthtmlCssModules = require (' posthtml-css-modules' );
247
- var template = fs .readFileSync (' ./about.html' , ' utf8' );
248
-
249
- posthtml ([posthtmlCssModules (' ./cssModules.json' )])
250
- .process (template)
251
- .then (function (result ) {
252
- console .log (result .html );
253
- });
267
+ var fs = require (" fs " );
268
+ var posthtml = require (" posthtml" );
269
+ var posthtmlCssModules = require (" posthtml-css-modules" );
270
+ var template = fs .readFileSync (" ./about.html" , " utf8" );
271
+
272
+ posthtml ([posthtmlCssModules (" ./cssModules.json" )])
273
+ .process (template)
274
+ .then (function (result ) {
275
+ console .log (result .html );
276
+ });
254
277
```
255
- (for using other build systems please check [ the documentation of PostHTML] ( https://github.com/posthtml/posthtml/blob/master/README.md ) )
256
278
279
+ (for using other build systems please check [ the documentation of PostHTML] ( https://github.com/posthtml/posthtml/blob/master/README.md ) )
257
280
258
281
And you'll get the following HTML:
282
+
259
283
``` html
260
284
<h1 class =" _title_xkpkl_5 _title_116zl_1" >postcss-modules</h1 >
261
- <article class =" _article_xkpkl_10" >A PostCSS plugin to use CSS Modules everywhere</article >
285
+ <article class =" _article_xkpkl_10" >
286
+ A PostCSS plugin to use CSS Modules everywhere
287
+ </article >
262
288
```
263
289
264
-
265
290
## May I see the plugin in action?
291
+
266
292
Sure! Take a look at the [ example] ( https://github.com/outpunk/postcss-modules-example ) .
267
293
268
294
See [ PostCSS] docs for examples for your environment and don't forget to run
295
+
269
296
```
270
297
npm install --save-dev postcss-modules
271
298
```
272
299
273
- [ FileSystemLoader ] : https://github.com/css-modules/css-modules-loader-core/blob/master/src/file-system-loader.js
300
+ [ filesystemloader ] : https://github.com/css-modules/css-modules-loader-core/blob/master/src/file-system-loader.js
0 commit comments