postcss-modules 1.4.1 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.browserslistrc +3 -0
- package/CHANGELOG.md +4 -0
- package/README.md +87 -60
- package/build/index.js +4 -1
- package/package.json +11 -10
package/.browserslistrc
ADDED
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
A [PostCSS] plugin to use [CSS Modules] everywhere. Not only at the client side.
|
|
4
4
|
|
|
5
|
-
[
|
|
6
|
-
[ci-img]:
|
|
7
|
-
[ci]:
|
|
8
|
-
[
|
|
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
|
-
|
|
19
|
+
padding: 20px;
|
|
21
20
|
}
|
|
22
21
|
|
|
23
22
|
.title {
|
|
24
|
-
|
|
25
|
-
|
|
23
|
+
composes: title from "./mixins.css";
|
|
24
|
+
color: green;
|
|
26
25
|
}
|
|
27
26
|
|
|
28
27
|
.article {
|
|
29
|
-
|
|
28
|
+
font-size: 16px;
|
|
30
29
|
}
|
|
31
30
|
|
|
32
31
|
/* mixins.css */
|
|
33
32
|
.title {
|
|
34
|
-
|
|
35
|
-
|
|
33
|
+
color: black;
|
|
34
|
+
font-size: 40px;
|
|
36
35
|
}
|
|
37
36
|
|
|
38
37
|
.title:hover {
|
|
39
|
-
|
|
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
|
-
|
|
48
|
-
|
|
46
|
+
color: black;
|
|
47
|
+
font-size: 40px;
|
|
49
48
|
}
|
|
50
49
|
|
|
51
50
|
._title_116zl_1:hover {
|
|
52
|
-
|
|
51
|
+
color: red;
|
|
53
52
|
}
|
|
54
53
|
|
|
55
54
|
.page {
|
|
56
|
-
|
|
55
|
+
padding: 20px;
|
|
57
56
|
}
|
|
58
57
|
|
|
59
58
|
._title_xkpkl_5 {
|
|
60
|
-
|
|
59
|
+
color: green;
|
|
61
60
|
}
|
|
62
61
|
|
|
63
62
|
._article_xkpkl_10 {
|
|
64
|
-
|
|
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(
|
|
86
|
+
require("postcss-modules")({
|
|
88
87
|
getJSON: function(cssFileName, json, outputFileName) {
|
|
89
|
-
var path
|
|
90
|
-
var cssName
|
|
91
|
-
var jsonFileName
|
|
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(
|
|
108
|
-
scopeBehaviour:
|
|
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(
|
|
127
|
+
require("postcss-modules")({
|
|
129
128
|
generateScopedName: function(name, filename, css) {
|
|
130
|
-
var path
|
|
131
|
-
var i
|
|
132
|
-
var line
|
|
133
|
-
var file
|
|
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
|
|
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(
|
|
147
|
-
generateScopedName:
|
|
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(
|
|
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,6 +186,7 @@ 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;
|
|
@@ -182,6 +194,7 @@ CSS:
|
|
|
182
194
|
```
|
|
183
195
|
|
|
184
196
|
JSON:
|
|
197
|
+
|
|
185
198
|
```json
|
|
186
199
|
{
|
|
187
200
|
"post-title": "._post-title_116zl_1",
|
|
@@ -189,8 +202,8 @@ JSON:
|
|
|
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(
|
|
218
|
-
var cssModules = require(
|
|
219
|
-
var html = jade.compileFile(
|
|
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">
|
|
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">
|
|
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(
|
|
245
|
-
var posthtml = require(
|
|
246
|
-
var posthtmlCssModules = require(
|
|
247
|
-
var template = fs.readFileSync(
|
|
248
|
-
|
|
249
|
-
posthtml([posthtmlCssModules(
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
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">
|
|
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
|
-
[
|
|
300
|
+
[filesystemloader]: https://github.com/css-modules/css-modules-loader-core/blob/master/src/file-system-loader.js
|
package/build/index.js
CHANGED
|
@@ -48,7 +48,10 @@ function getScopedNameGenerator(opts) {
|
|
|
48
48
|
const scopedNameGenerator = opts.generateScopedName || _generateScopedName2.default;
|
|
49
49
|
|
|
50
50
|
if (typeof scopedNameGenerator === "function") return scopedNameGenerator;
|
|
51
|
-
return (0, _genericNames2.default)(scopedNameGenerator, {
|
|
51
|
+
return (0, _genericNames2.default)(scopedNameGenerator, {
|
|
52
|
+
context: process.cwd(),
|
|
53
|
+
hashPrefix: opts.hashPrefix
|
|
54
|
+
});
|
|
52
55
|
}
|
|
53
56
|
|
|
54
57
|
function getLoader(opts, plugins) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "postcss-modules",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0",
|
|
4
4
|
"description": "PostCSS plugin to use CSS Modules everywhere",
|
|
5
5
|
"main": "build/index.js",
|
|
6
6
|
"keywords": [
|
|
@@ -19,27 +19,28 @@
|
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
21
|
"css-modules-loader-core": "^1.1.0",
|
|
22
|
-
"generic-names": "^
|
|
22
|
+
"generic-names": "^2.0.1",
|
|
23
23
|
"lodash.camelcase": "^4.3.0",
|
|
24
24
|
"postcss": "^7.0.1",
|
|
25
25
|
"string-hash": "^1.1.1"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
|
-
"autoprefixer": "6.
|
|
29
|
-
"ava": "^0.25.0",
|
|
28
|
+
"autoprefixer": "^9.6.1",
|
|
30
29
|
"babel-cli": "^6.26.0",
|
|
31
30
|
"babel-core": "^6.26.3",
|
|
32
|
-
"babel-eslint": "^
|
|
31
|
+
"babel-eslint": "^10.0.3",
|
|
33
32
|
"babel-preset-env": "^1.7.0",
|
|
34
|
-
"eslint": "^
|
|
33
|
+
"eslint": "^6.4.0",
|
|
35
34
|
"eslint-plugin-import": "^2.13.0",
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
"
|
|
35
|
+
"eslint-plugin-jest": "^22.17.0",
|
|
36
|
+
"husky": "^3.0.5",
|
|
37
|
+
"jest": "^24.9.0",
|
|
38
|
+
"lint-staged": "^9.2.5",
|
|
39
|
+
"prettier": "^1.18.2"
|
|
39
40
|
},
|
|
40
41
|
"scripts": {
|
|
41
42
|
"pretest": "$(npm bin)/eslint src test",
|
|
42
|
-
"test": "$(npm bin)/
|
|
43
|
+
"test": "$(npm bin)/jest",
|
|
43
44
|
"transpile": "$(npm bin)/babel src -d build",
|
|
44
45
|
"prepublish": "npm run transpile",
|
|
45
46
|
"postpublish": "rm -rf build && git push --follow-tags",
|