postcss-modules 1.3.0 → 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.
@@ -0,0 +1,3 @@
1
+ ie 9
2
+ firefox 4
3
+ safari 8
package/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.5.0
4
+
5
+ - Added `hashPrefix` option by Jesse Thomson (@jessethomson) <https://github.com/css-modules/postcss-modules/pull/98>
6
+
7
+ ## 1.4.1
8
+
9
+ - Rebublished the previous release. Sorry :(
10
+
11
+ ## 1.4.0
12
+
13
+ - Added export for other plugins by Evilebot Tnawi (@evilebottnawi) <https://github.com/css-modules/postcss-modules/pull/88>, <https://github.com/css-modules/postcss-modules/issues/29>
14
+
15
+ ## 1.3.1
16
+
17
+ - Move dev tools to devDependecies by Anton Khlynovskiy (@ubzey) <https://github.com/css-modules/postcss-modules/pull/85>
18
+
3
19
  ## 1.3.0
4
20
 
5
21
  - Updated dependecies
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
- [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,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('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
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, { context: process.cwd() });
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) {
@@ -102,6 +105,12 @@ module.exports = _postcss2.default.plugin(PLUGIN_NAME, (opts = {}) => {
102
105
  });
103
106
  }
104
107
 
108
+ result.messages.push({
109
+ type: "export",
110
+ plugin: "postcss-modules",
111
+ exportTokens: parser.exportTokens
112
+ });
113
+
105
114
  // getJSON may return a promise
106
115
  return getJSON(css.source.input.file, parser.exportTokens, result.opts.to);
107
116
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "postcss-modules",
3
- "version": "1.3.0",
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,28 +19,28 @@
19
19
  },
20
20
  "dependencies": {
21
21
  "css-modules-loader-core": "^1.1.0",
22
- "generic-names": "^1.0.3",
23
- "husky": "^0.14.3",
24
- "lint-staged": "^7.2.0",
22
+ "generic-names": "^2.0.1",
25
23
  "lodash.camelcase": "^4.3.0",
26
24
  "postcss": "^7.0.1",
27
- "prettier": "^1.13.7",
28
25
  "string-hash": "^1.1.1"
29
26
  },
30
27
  "devDependencies": {
31
- "autoprefixer": "6.7.7",
32
- "ava": "^0.25.0",
33
- "babel": "^6.5.2",
28
+ "autoprefixer": "^9.6.1",
34
29
  "babel-cli": "^6.26.0",
35
30
  "babel-core": "^6.26.3",
36
- "babel-eslint": "^8.2.6",
31
+ "babel-eslint": "^10.0.3",
37
32
  "babel-preset-env": "^1.7.0",
38
- "eslint": "^5.1.0",
39
- "eslint-plugin-import": "^2.13.0"
33
+ "eslint": "^6.4.0",
34
+ "eslint-plugin-import": "^2.13.0",
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"
40
40
  },
41
41
  "scripts": {
42
42
  "pretest": "$(npm bin)/eslint src test",
43
- "test": "$(npm bin)/babel-node $(npm bin)/ava",
43
+ "test": "$(npm bin)/jest",
44
44
  "transpile": "$(npm bin)/babel src -d build",
45
45
  "prepublish": "npm run transpile",
46
46
  "postpublish": "rm -rf build && git push --follow-tags",