@@ -12,60 +12,60 @@ For example, you have the following CSS:
1212``` css
1313/* styles.css */
1414:global .page {
15- padding : 20px ;
15+ padding : 20px ;
1616}
1717
1818.title {
19- composes : title from " ./mixins.css" ;
20- color : green ;
19+ composes : title from " ./mixins.css" ;
20+ color : green ;
2121}
2222
2323.article {
24- font-size : 16px ;
24+ font-size : 16px ;
2525}
2626
2727/* mixins.css */
2828.title {
29- color : black ;
30- font-size : 40px ;
29+ color : black ;
30+ font-size : 40px ;
3131}
3232
3333.title :hover {
34- color : red ;
34+ color : red ;
3535}
3636```
3737
3838After the transformation it will become like this:
3939
4040``` css
4141._title_116zl_1 {
42- color : black ;
43- font-size : 40px ;
42+ color : black ;
43+ font-size : 40px ;
4444}
4545
4646._title_116zl_1 :hover {
47- color : red ;
47+ color : red ;
4848}
4949
5050.page {
51- padding : 20px ;
51+ padding : 20px ;
5252}
5353
5454._title_xkpkl_5 {
55- color : green ;
55+ color : green ;
5656}
5757
5858._article_xkpkl_10 {
59- font-size : 16px ;
59+ font-size : 16px ;
6060}
6161```
6262
6363And the plugin will give you a JSON object for transformed classes:
6464
6565``` json
6666{
67- "title" : " _title_xkpkl_5 _title_116zl_1" ,
68- "article" : " _article_xkpkl_10"
67+ "title" : " _title_xkpkl_5 _title_116zl_1" ,
68+ "article" : " _article_xkpkl_10"
6969}
7070```
7171
@@ -79,14 +79,14 @@ use the `getJSON` callback. For example, save data about classes into a correspo
7979
8080``` js
8181postcss ([
82- require (" postcss-modules" )({
83- getJSON : function (cssFileName , json , outputFileName ) {
84- var path = require (" path" );
85- var cssName = path .basename (cssFileName, " .css" );
86- var jsonFileName = path .resolve (" ./build/" + cssName + " .json" );
87- fs .writeFileSync (jsonFileName, JSON .stringify (json));
88- },
89- }),
82+ require (" postcss-modules" )({
83+ getJSON : function (cssFileName , json , outputFileName ) {
84+ var path = require (" path" );
85+ var cssName = path .basename (cssFileName, " .css" );
86+ var jsonFileName = path .resolve (" ./build/" + cssName + " .json" );
87+ fs .writeFileSync (jsonFileName, JSON .stringify (json));
88+ },
89+ }),
9090]);
9191```
9292
@@ -99,9 +99,9 @@ this behaviour using the `scopeBehaviour` option:
9999
100100``` js
101101postcss ([
102- require (" postcss-modules" )({
103- scopeBehaviour: " global" , // can be 'global' or 'local',
104- }),
102+ require (" postcss-modules" )({
103+ scopeBehaviour: " global" , // can be 'global' or 'local',
104+ }),
105105]);
106106```
107107
@@ -120,16 +120,16 @@ To generate custom classes, use the `generateScopedName` callback:
120120
121121``` js
122122postcss ([
123- require (" postcss-modules" )({
124- generateScopedName : function (name , filename , css ) {
125- var path = require (" path" );
126- var i = css .indexOf (" ." + name);
127- var line = css .substr (0 , i).split (/ [\r\n ] / ).length ;
128- var file = path .basename (filename, " .css" );
129-
130- return " _" + file + " _" + line + " _" + name;
131- },
132- }),
123+ require (" postcss-modules" )({
124+ generateScopedName : function (name , filename , css ) {
125+ var path = require (" path" );
126+ var i = css .indexOf (" ." + name);
127+ var line = css .substr (0 , i).split (/ [\r\n ] / ).length ;
128+ var file = path .basename (filename, " .css" );
129+
130+ return " _" + file + " _" + line + " _" + name;
131+ },
132+ }),
133133]);
134134```
135135
@@ -138,20 +138,20 @@ Or just pass an interpolated string to the `generateScopedName` option
138138
139139``` js
140140postcss ([
141- require (" postcss-modules" )({
142- generateScopedName: " [name]__[local]___[hash:base64:5]" ,
143- }),
141+ require (" postcss-modules" )({
142+ generateScopedName: " [name]__[local]___[hash:base64:5]" ,
143+ }),
144144]);
145145```
146146
147147It'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 ) ):
148148
149149``` js
150150postcss ([
151- require (" postcss-modules" )({
152- generateScopedName: " [name]__[local]___[hash:base64:5]" ,
153- hashPrefix: " prefix" ,
154- }),
151+ require (" postcss-modules" )({
152+ generateScopedName: " [name]__[local]___[hash:base64:5]" ,
153+ hashPrefix: " prefix" ,
154+ }),
155155]);
156156```
157157
@@ -161,9 +161,9 @@ If you need to export global names via the JSON object along with the local ones
161161
162162``` js
163163postcss ([
164- require (" postcss-modules" )({
165- exportGlobals: true ,
166- }),
164+ require (" postcss-modules" )({
165+ exportGlobals: true ,
166+ }),
167167]);
168168```
169169
@@ -173,9 +173,9 @@ If you need, you can pass a custom loader (see [FileSystemLoader] for example):
173173
174174``` js
175175postcss ([
176- require (" postcss-modules" )({
177- Loader: CustomLoader,
178- }),
176+ require (" postcss-modules" )({
177+ Loader: CustomLoader,
178+ }),
179179]);
180180```
181181
@@ -207,22 +207,28 @@ In lieu of a string, a custom function can generate the exported class names.
207207
208208### Resolve path alias
209209
210- You can rewrite paths for ` composes/from ` by using ` resolve ` options .
210+ You can rewrite paths for ` composes/from ` by using the ` resolve ` option .
211211It's useful when you need to resolve custom path alias.
212212
213+ Parameters:
214+ - ` file ` — a module we want to resolve
215+ - ` importer ` — the file that imports the module we want to resolve
216+
217+ Return value: ` string | null | Promise<string | null> `
218+
213219``` js
214220postcss ([
215- require (" postcss-modules" )({
216- resolve : function (file ) {
217- return file .replace (/ ^ @/ , process .cwd ());
218- },
219- }),
221+ require (" postcss-modules" )({
222+ resolve : function (file , importer ) {
223+ return path .resolve (
224+ path .dirname (importer),
225+ file .replace (/ ^ @/ , process .cwd ()
226+ );
227+ },
228+ }),
220229]);
221230` ` `
222231
223- ` resolve ` may also return a ` Promise<string> ` .
224-
225-
226232## Integration with templates
227233
228234The plugin only transforms CSS classes to CSS modules.
@@ -233,8 +239,8 @@ Assume you've saved project's CSS modules in `cssModules.json`:
233239
234240` ` ` json
235241{
236- "title" : " _title_xkpkl_5 _title_116zl_1" ,
237- "article" : " _article_xkpkl_10"
242+ " title" : " _title_xkpkl_5 _title_116zl_1" ,
243+ " article" : " _article_xkpkl_10"
238244}
239245` ` `
240246
@@ -261,7 +267,7 @@ And you'll get the following HTML:
261267` ` ` html
262268< h1 class = " _title_xkpkl_5 _title_116zl_1" > postcss- modules< / h1>
263269< article class = " _article_xkpkl_10" >
264- A PostCSS plugin to use CSS Modules everywhere
270+ A PostCSS plugin to use CSS Modules everywhere
265271< / article>
266272` ` `
267273
@@ -278,7 +284,7 @@ Here is our template `about.html`:
278284` ` ` html
279285< h1 css- module = " title" > postcss- modules< / h1>
280286< article css- module = " article" >
281- A PostCSS plugin to use CSS Modules everywhere
287+ A PostCSS plugin to use CSS Modules everywhere
282288< / article>
283289` ` `
284290
@@ -291,10 +297,10 @@ var posthtmlCssModules = require("posthtml-css-modules");
291297var template = fs .readFileSync (" ./about.html" , " utf8" );
292298
293299posthtml ([posthtmlCssModules (" ./cssModules.json" )])
294- .process (template)
295- .then (function (result ) {
296- console .log (result .html );
297- });
300+ .process (template)
301+ .then (function (result ) {
302+ console .log (result .html );
303+ });
298304` ` `
299305
300306(for using other build systems please check [the documentation of PostHTML](https://github.com/posthtml/posthtml/blob/master/readme.md))
@@ -304,7 +310,7 @@ And you'll get the following HTML:
304310` ` ` html
305311< h1 class = " _title_xkpkl_5 _title_116zl_1" > postcss- modules< / h1>
306312< article class = " _article_xkpkl_10" >
307- A PostCSS plugin to use CSS Modules everywhere
313+ A PostCSS plugin to use CSS Modules everywhere
308314< / article>
309315` ` `
310316
@@ -320,6 +326,6 @@ npm install --save-dev postcss postcss-modules
320326
321327[filesystemloader]: https://github.com/css-modules/css-modules-loader-core/blob/master/src/file-system-loader.js
322328
323-
324329## Sponsors
325- - Dmitry Olyenyov
330+
331+ - Dmitry Olyenyov
0 commit comments