postcss-modules 1.3.2 → 2.0.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,8 +1,30 @@
1
1
  # Changelog
2
2
 
3
+ ## 2.0.0
4
+
5
+ ### Added
6
+
7
+ - [`localsConvention` option](https://github.com/css-modules/postcss-modules#localsconvention) by Hamza Mihai Daniel (@allocenx) <https://github.com/css-modules/postcss-modules/pull/103>, <https://github.com/css-modules/postcss-modules/issues/93>
8
+
9
+ ### Breaking changes
10
+
11
+ - `camelCase` camelCase removed, use the [`localsConvention` option](https://github.com/css-modules/postcss-modules#localsconvention) instead.
12
+
13
+ ## 1.5.0
14
+
15
+ - Added `hashPrefix` option by Jesse Thomson (@jessethomson) <https://github.com/css-modules/postcss-modules/pull/98>
16
+
17
+ ## 1.4.1
18
+
19
+ - Rebublished the previous release. Sorry :(
20
+
21
+ ## 1.4.0
22
+
23
+ - 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>
24
+
3
25
  ## 1.3.1
4
26
 
5
- - Move dev tools to devDependecies by Anton Khlynovskiy (ubzey) <https://github.com/css-modules/postcss-modules/pull/85>
27
+ - Move dev tools to devDependecies by Anton Khlynovskiy (@ubzey) <https://github.com/css-modules/postcss-modules/pull/85>
6
28
 
7
29
  ## 1.3.0
8
30
 
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')({
@@ -170,27 +181,22 @@ postcss([
170
181
  ]);
171
182
  ```
172
183
 
173
- ### Camel cased classes
184
+ ### localsConvention
174
185
 
175
- If you need, you can pass the options `{ camelCase: true }` to transform classes:
176
-
177
- CSS:
178
- ```css
179
- .post-title {
180
- color: red;
181
- }
182
- ```
186
+ Type: `String`
187
+ Default: `null`
183
188
 
184
- JSON:
185
- ```json
186
- {
187
- "post-title": "._post-title_116zl_1",
188
- "postTitle": "._post-title_116zl_1"
189
- }
190
- ```
189
+ Style of exported classnames.
191
190
 
191
+ | Name | Type | Description |
192
+ | :-------------------: | :--------: | :----------------------------------------------------------------------------------------------- |
193
+ | **`'camelCase'`** | `{String}` | Class names will be camelized, the original class name will not to be removed from the locals |
194
+ | **`'camelCaseOnly'`** | `{String}` | Class names will be camelized, the original class name will be removed from the locals |
195
+ | **`'dashes'`** | `{String}` | Only dashes in class names will be camelized |
196
+ | **`'dashesOnly'`** | `{String}` | Dashes in class names will be camelized, the original class name will be removed from the locals |
192
197
 
193
198
  ## Integration with templates
199
+
194
200
  The plugin only transforms CSS classes to CSS modules.
195
201
  But you probably want to integrate these CSS modules with your templates.
196
202
  Here are some examples.
@@ -204,70 +210,84 @@ Assume you've saved project's CSS modules in `cssModules.json`:
204
210
  }
205
211
  ```
206
212
 
207
-
208
213
  ### Pug (ex-Jade)
214
+
209
215
  Let's say you have a Pug template `about.jade`:
216
+
210
217
  ```jade
211
218
  h1(class=css.title) postcss-modules
212
219
  article(class=css.article) A PostCSS plugin to use CSS Modules everywhere
213
220
  ```
214
221
 
215
222
  Render it:
223
+
216
224
  ```js
217
- var jade = require('jade');
218
- var cssModules = require('./cssModules.json');
219
- var html = jade.compileFile('about.jade')({css: cssModules});
225
+ var jade = require("jade");
226
+ var cssModules = require("./cssModules.json");
227
+ var html = jade.compileFile("about.jade")({ css: cssModules });
220
228
  console.log(html);
221
229
  ```
222
230
 
223
231
  And you'll get the following HTML:
232
+
224
233
  ```html
225
234
  <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>
235
+ <article class="_article_xkpkl_10">
236
+ A PostCSS plugin to use CSS Modules everywhere
237
+ </article>
227
238
  ```
228
239
 
229
-
230
240
  ### HTML
241
+
231
242
  For HTML transformation we'll use [PostHTML](https://github.com/posthtml/posthtml) and [PostHTML CSS Modules](https://github.com/maltsev/posthtml-css-modules):
243
+
232
244
  ```bash
233
245
  npm install --save posthtml posthtml-css-modules
234
246
  ```
235
247
 
236
248
  Here is our template `about.html`:
249
+
237
250
  ```html
238
251
  <h1 css-module="title">postcss-modules</h1>
239
- <article css-module="article">A PostCSS plugin to use CSS Modules everywhere</article>
252
+ <article css-module="article">
253
+ A PostCSS plugin to use CSS Modules everywhere
254
+ </article>
240
255
  ```
241
256
 
242
257
  Transform it:
258
+
243
259
  ```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
- });
260
+ var fs = require("fs");
261
+ var posthtml = require("posthtml");
262
+ var posthtmlCssModules = require("posthtml-css-modules");
263
+ var template = fs.readFileSync("./about.html", "utf8");
264
+
265
+ posthtml([posthtmlCssModules("./cssModules.json")])
266
+ .process(template)
267
+ .then(function(result) {
268
+ console.log(result.html);
269
+ });
254
270
  ```
255
- (for using other build systems please check [the documentation of PostHTML](https://github.com/posthtml/posthtml/blob/master/README.md))
256
271
 
272
+ (for using other build systems please check [the documentation of PostHTML](https://github.com/posthtml/posthtml/blob/master/README.md))
257
273
 
258
274
  And you'll get the following HTML:
275
+
259
276
  ```html
260
277
  <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>
278
+ <article class="_article_xkpkl_10">
279
+ A PostCSS plugin to use CSS Modules everywhere
280
+ </article>
262
281
  ```
263
282
 
264
-
265
283
  ## May I see the plugin in action?
284
+
266
285
  Sure! Take a look at the [example](https://github.com/outpunk/postcss-modules-example).
267
286
 
268
287
  See [PostCSS] docs for examples for your environment and don't forget to run
288
+
269
289
  ```
270
290
  npm install --save-dev postcss-modules
271
291
  ```
272
292
 
273
- [FileSystemLoader]: https://github.com/css-modules/css-modules-loader-core/blob/master/src/file-system-loader.js
293
+ [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) {
@@ -76,6 +79,10 @@ function isResultPlugin(plugin) {
76
79
  return plugin.postcssPlugin !== PLUGIN_NAME;
77
80
  }
78
81
 
82
+ function dashesCamelCase(string) {
83
+ return string.replace(/-+(\w)/g, (_, firstLetter) => firstLetter.toUpperCase());
84
+ }
85
+
79
86
  module.exports = _postcss2.default.plugin(PLUGIN_NAME, (opts = {}) => {
80
87
  const getJSON = opts.getJSON || _saveJSON2.default;
81
88
 
@@ -95,13 +102,39 @@ module.exports = _postcss2.default.plugin(PLUGIN_NAME, (opts = {}) => {
95
102
  const out = loader.finalSource;
96
103
  if (out) css.prepend(out);
97
104
 
98
- if (opts.camelCase) {
99
- Object.keys(parser.exportTokens).forEach(function (token) {
100
- const camelCaseToken = (0, _lodash2.default)(token);
101
- parser.exportTokens[camelCaseToken] = parser.exportTokens[token];
102
- });
105
+ if (opts.localsConvention) {
106
+ parser.exportTokens = Object.entries(parser.exportTokens).reduce(function (tokens, [className, value]) {
107
+ switch (opts.localsConvention) {
108
+ case "camelCase":
109
+ tokens[className] = value;
110
+ tokens[(0, _lodash2.default)(className)] = value;
111
+
112
+ break;
113
+ case "camelCaseOnly":
114
+ tokens[(0, _lodash2.default)(className)] = value;
115
+
116
+ break;
117
+ case "dashes":
118
+ tokens[className] = value;
119
+ tokens[dashesCamelCase(className)] = value;
120
+
121
+ break;
122
+ case "dashesOnly":
123
+ tokens[dashesCamelCase(className)] = value;
124
+
125
+ break;
126
+ }
127
+
128
+ return tokens;
129
+ }, {});
103
130
  }
104
131
 
132
+ result.messages.push({
133
+ type: "export",
134
+ plugin: "postcss-modules",
135
+ exportTokens: parser.exportTokens
136
+ });
137
+
105
138
  // getJSON may return a promise
106
139
  return getJSON(css.source.input.file, parser.exportTokens, result.opts.to);
107
140
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "postcss-modules",
3
- "version": "1.3.2",
3
+ "version": "2.0.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": "^1.0.3",
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.7.7",
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": "^8.2.6",
31
+ "babel-eslint": "^10.0.3",
33
32
  "babel-preset-env": "^1.7.0",
34
- "eslint": "^5.1.0",
33
+ "eslint": "^6.4.0",
35
34
  "eslint-plugin-import": "^2.13.0",
36
- "husky": "0.14.3",
37
- "lint-staged": "7.2.0",
38
- "prettier": "1.13.7"
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)/babel-node $(npm bin)/ava",
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",