22
33A [ PostCSS] plugin to use [ CSS Modules] everywhere. Not only at the client side.
44
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
99
1010<a href =" https://evilmartians.com/?utm_source=postcss-modules " >
1111<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.
1414What is this? For example, you have the following CSS:
1515
1616``` css
17-
1817/* styles.css */
1918:global .page {
20- padding : 20px ;
19+ padding : 20px ;
2120}
2221
2322.title {
24- composes : title from " ./mixins.css" ;
25- color : green ;
23+ composes : title from " ./mixins.css" ;
24+ color : green ;
2625}
2726
2827.article {
29- font-size : 16px ;
28+ font-size : 16px ;
3029}
3130
3231/* mixins.css */
3332.title {
34- color : black ;
35- font-size : 40px ;
33+ color : black ;
34+ font-size : 40px ;
3635}
3736
3837.title :hover {
39- color : red ;
38+ color : red ;
4039}
41-
4240```
41+
4342After the transformation it will become like this:
4443
4544``` css
4645._title_116zl_1 {
47- color : black ;
48- font-size : 40px ;
46+ color : black ;
47+ font-size : 40px ;
4948}
5049
5150._title_116zl_1 :hover {
52- color : red ;
51+ color : red ;
5352}
5453
5554.page {
56- padding : 20px ;
55+ padding : 20px ;
5756}
5857
5958._title_xkpkl_5 {
60- color : green ;
59+ color : green ;
6160}
6261
6362._article_xkpkl_10 {
64- font-size : 16px ;
63+ font-size : 16px ;
6564}
6665```
6766
6867And the plugin will give you a JSON object for transformed classes:
68+
6969``` json
7070{
7171 "title" : " _title_xkpkl_5 _title_116zl_1" ,
72- "article" : " _article_xkpkl_10" ,
72+ "article" : " _article_xkpkl_10"
7373}
7474```
7575
76-
7776## Usage
7877
7978### Saving exported classes
@@ -84,11 +83,11 @@ use the `getJSON` callback. For example, save data about classes into a correspo
8483
8584``` js
8685postcss ([
87- require (' postcss-modules' )({
86+ require (" postcss-modules" )({
8887 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" );
9291 fs .writeFileSync (jsonFileName, JSON .stringify (json));
9392 }
9493 })
@@ -104,13 +103,13 @@ this behaviour using the `scopeBehaviour` option:
104103
105104``` js
106105postcss ([
107- require (' postcss-modules' )({
108- scopeBehaviour: ' global' // can be 'global' or 'local',
106+ require (" postcss-modules" )({
107+ scopeBehaviour: " global" // can be 'global' or 'local',
109108 })
110109]);
111110```
112111
113- To define paths for global modules, use the ` globalModulePaths ` option.
112+ To define paths for global modules, use the ` globalModulePaths ` option.
114113It is an array with regular expressions defining the paths:
115114
116115``` js
@@ -125,14 +124,14 @@ To generate custom classes, use the `generateScopedName` callback:
125124
126125``` js
127126postcss ([
128- require (' postcss-modules' )({
127+ require (" postcss-modules" )({
129128 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" );
134133
135- return ' _ ' + file + ' _ ' + line + ' _ ' + name;
134+ return " _ " + file + " _ " + line + " _ " + name;
136135 }
137136 })
138137]);
@@ -143,8 +142,19 @@ Or just pass an interpolated string to the `generateScopedName` option
143142
144143``` js
145144postcss ([
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"
148158 })
149159]);
150160```
@@ -155,13 +165,14 @@ If you need, you can pass a custom loader (see [FileSystemLoader] for example):
155165
156166``` js
157167postcss ([
158- require (' postcss-modules' )({
159- Loader: CustomLoader,
168+ require (" postcss-modules" )({
169+ Loader: CustomLoader
160170 })
161171]);
162172```
163173
164174You can also pass any needed root path:
175+
165176``` js
166177postcss ([
167178 require (' postcss-modules' )({
@@ -175,22 +186,24 @@ postcss([
175186If you need, you can pass the options ` { camelCase: true } ` to transform classes:
176187
177188CSS:
189+
178190``` css
179191.post-title {
180192 color : red ;
181193}
182194```
183195
184196JSON:
197+
185198``` json
186199{
187200 "post-title" : " ._post-title_116zl_1" ,
188201 "postTitle" : " ._post-title_116zl_1"
189202}
190203```
191204
192-
193205## Integration with templates
206+
194207The plugin only transforms CSS classes to CSS modules.
195208But you probably want to integrate these CSS modules with your templates.
196209Here are some examples.
@@ -204,70 +217,84 @@ Assume you've saved project's CSS modules in `cssModules.json`:
204217}
205218```
206219
207-
208220### Pug (ex-Jade)
221+
209222Let's say you have a Pug template ` about.jade ` :
223+
210224``` jade
211225h1(class=css.title) postcss-modules
212226article(class=css.article) A PostCSS plugin to use CSS Modules everywhere
213227```
214228
215229Render it:
230+
216231``` 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 });
220235console .log (html);
221236```
222237
223238And you'll get the following HTML:
239+
224240``` html
225241<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 >
227245```
228246
229-
230247### HTML
248+
231249For HTML transformation we'll use [ PostHTML] ( https://github.com/posthtml/posthtml ) and [ PostHTML CSS Modules] ( https://github.com/maltsev/posthtml-css-modules ) :
250+
232251``` bash
233252npm install --save posthtml posthtml-css-modules
234253```
235254
236255Here is our template ` about.html ` :
256+
237257``` html
238258<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 >
240262```
241263
242264Transform it:
265+
243266``` 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+ });
254277```
255- (for using other build systems please check [ the documentation of PostHTML] ( https://github.com/posthtml/posthtml/blob/master/README.md ) )
256278
279+ (for using other build systems please check [ the documentation of PostHTML] ( https://github.com/posthtml/posthtml/blob/master/README.md ) )
257280
258281And you'll get the following HTML:
282+
259283``` html
260284<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 >
262288```
263289
264-
265290## May I see the plugin in action?
291+
266292Sure! Take a look at the [ example] ( https://github.com/outpunk/postcss-modules-example ) .
267293
268294See [ PostCSS] docs for examples for your environment and don't forget to run
295+
269296```
270297npm install --save-dev postcss-modules
271298```
272299
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