postcss-modules 8.1.0 → 8.3.1

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/CHANGELOG.md CHANGED
@@ -1,5 +1,22 @@
1
1
  # Changelog
2
2
 
3
+ ## 8.3.1
4
+
5
+ ### Fixed
6
+
7
+ - `hashPrefix` is now applied when no custom `generateScopedName` is set. Previously the option was only honoured by `generic-names`, so the built-in default generator silently ignored it. [#160](https://github.com/madyankin/postcss-modules/issues/160)
8
+ - Empty rules left behind after `:global { ... }` unwrapping (e.g. a Sass block containing only a multi-line comment) are now removed instead of emitted as orphaned `{ ... }` blocks. [#136](https://github.com/madyankin/postcss-modules/issues/136)
9
+
10
+ ## 8.3.0
11
+
12
+ ### Added
13
+
14
+ - New `localsConvention` value `'all'` — emit the original class name plus the camelCase and dashes-camelCase variants in one pass.
15
+ - New `localsConvention` value `'none'` — emit only the original class name, with no additional aliases.
16
+ - The function form of `localsConvention` may now return an array of strings. Every entry in the array is added to the locals map and resolves to the same value, which makes it easy to expose a single CSS class under multiple JS-friendly aliases.
17
+
18
+ Adapted from the unmerged work in [#154](https://github.com/madyankin/postcss-modules/pull/154) by @CarbonORM.
19
+
3
20
  ## 8.1.0
4
21
 
5
22
  ### Internal
package/README.md CHANGED
@@ -191,7 +191,7 @@ postcss([
191
191
 
192
192
  ### localsConvention
193
193
 
194
- Type: `String | (originalClassName: string, generatedClassName: string, inputFile: string) => className: string`
194
+ Type: `String | (originalClassName: string, generatedClassName: string, inputFile: string) => string | string[]`
195
195
  Default: `null`
196
196
 
197
197
  Style of exported classnames, the keys in your json.
@@ -202,8 +202,30 @@ Style of exported classnames, the keys in your json.
202
202
  | **`'camelCaseOnly'`** | `{String}` | Class names will be camelized, the original class name will be removed from the locals |
203
203
  | **`'dashes'`** | `{String}` | Only dashes in class names will be camelized |
204
204
  | **`'dashesOnly'`** | `{String}` | Dashes in class names will be camelized, the original class name will be removed from the locals |
205
+ | **`'all'`** | `{String}` | Emit the original class name plus the camelCase and dashes-camelCase variants |
206
+ | **`'none'`** | `{String}` | Emit only the original class name (equivalent to leaving `localsConvention` unset) |
205
207
 
206
- In lieu of a string, a custom function can generate the exported class names.
208
+ For an input class `foo-bar` resolving to scoped name `_a`:
209
+
210
+ | Mode | Emitted locals |
211
+ | --------------- | ------------------------------------------------------------------------------------------- |
212
+ | `camelCase` | `{ "foo-bar": "_a", "fooBar": "_a" }` |
213
+ | `camelCaseOnly` | `{ "fooBar": "_a" }` |
214
+ | `dashes` | `{ "foo-bar": "_a", "fooBar": "_a" }` |
215
+ | `dashesOnly` | `{ "fooBar": "_a" }` |
216
+ | `all` | `{ "foo-bar": "_a", "fooBar": "_a" }` (camelCase + dashesCamelCase collapse for this input) |
217
+ | `none` | `{ "foo-bar": "_a" }` |
218
+
219
+ In lieu of a string, a custom function can generate the exported class names. The function may return either a single string or an array of strings; when an array is returned every entry is added to the locals map and resolves to the same value:
220
+
221
+ ```js
222
+ postcss([
223
+ require("postcss-modules")({
224
+ localsConvention: (original, generated) => [original, generated, original.toUpperCase()],
225
+ }),
226
+ ]);
227
+ // .foo-bar { ... } → { "foo-bar": "_a", "_a": "_a", "FOO-BAR": "_a" }
228
+ ```
207
229
 
208
230
  ### Resolve path alias
209
231
 
@@ -22,10 +22,22 @@ function makeLocalsConventionReducer(localsConvention, inputFile) {
22
22
  return (tokens, [className, value])=>{
23
23
  if (isFunc) {
24
24
  const convention = localsConvention(className, value, inputFile);
25
+ if (Array.isArray(convention)) {
26
+ convention.forEach((name)=>tokens[name] = value);
27
+ return tokens;
28
+ }
25
29
  tokens[convention] = value;
26
30
  return tokens;
27
31
  }
28
32
  switch(localsConvention){
33
+ case "none":
34
+ tokens[className] = value;
35
+ break;
36
+ case "all":
37
+ tokens[className] = value;
38
+ tokens[(0, _lodashcamelcase.default)(className)] = value;
39
+ tokens[dashesCamelCase(className)] = value;
40
+ break;
29
41
  case "camelCase":
30
42
  tokens[className] = value;
31
43
  tokens[(0, _lodashcamelcase.default)(className)] = value;
@@ -77,6 +77,11 @@ function makePlugin(opts) {
77
77
  ]).process(css, {
78
78
  from: inputFile
79
79
  });
80
+ // Remove rules whose selector was reduced to whitespace after
81
+ // `:global { ... }` unwrapping — see #136.
82
+ css.walkRules((rule)=>{
83
+ if (rule.selector.trim() === "") rule.remove();
84
+ });
80
85
  const out = loader.finalSource;
81
86
  if (out) css.prepend(out);
82
87
  if (opts.localsConvention) {
package/build/scoping.js CHANGED
@@ -68,14 +68,16 @@ function isValidBehaviour(behaviour) {
68
68
  function getDefaultScopeBehaviour(scopeBehaviour) {
69
69
  return scopeBehaviour && isValidBehaviour(scopeBehaviour) ? scopeBehaviour : behaviours.LOCAL;
70
70
  }
71
- function generateScopedNameDefault(name, filename, css) {
72
- const i = css.indexOf(`.${name}`);
73
- const lineNumber = css.substr(0, i).split(/[\r\n]/).length;
74
- const hash = (0, _stringhash.default)(css).toString(36).substr(0, 5);
75
- return `_${name}_${hash}_${lineNumber}`;
71
+ function makeDefaultScopedNameGenerator(hashPrefix) {
72
+ return function generateScopedNameDefault(name, filename, css) {
73
+ const i = css.indexOf(`.${name}`);
74
+ const lineNumber = css.substr(0, i).split(/[\r\n]/).length;
75
+ const hash = (0, _stringhash.default)(`${hashPrefix || ""}${css}`).toString(36).substr(0, 5);
76
+ return `_${name}_${hash}_${lineNumber}`;
77
+ };
76
78
  }
77
79
  function getScopedNameGenerator(generateScopedName, hashPrefix) {
78
- const scopedNameGenerator = generateScopedName || generateScopedNameDefault;
80
+ const scopedNameGenerator = generateScopedName || makeDefaultScopedNameGenerator(hashPrefix);
79
81
  if (typeof scopedNameGenerator === "function") {
80
82
  return scopedNameGenerator;
81
83
  }
package/index.d.ts CHANGED
@@ -6,7 +6,7 @@ declare type LocalsConventionFunction = (
6
6
  originalClassName: string,
7
7
  generatedClassName: string,
8
8
  inputFile: string
9
- ) => string;
9
+ ) => string | string[];
10
10
 
11
11
  declare class Loader {
12
12
  constructor(root: string, plugins: Plugin[]);
@@ -24,6 +24,8 @@ declare interface Options {
24
24
  | "camelCaseOnly"
25
25
  | "dashes"
26
26
  | "dashesOnly"
27
+ | "all"
28
+ | "none"
27
29
  | LocalsConventionFunction;
28
30
 
29
31
  scopeBehaviour?: "global" | "local";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "postcss-modules",
3
- "version": "8.1.0",
3
+ "version": "8.3.1",
4
4
  "description": "PostCSS plugin to use CSS Modules everywhere",
5
5
  "main": "build/index.js",
6
6
  "types": "index.d.ts",