postcss-nested 2.0.4 → 3.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.
package/CHANGELOG.md CHANGED
@@ -1,6 +1,20 @@
1
1
  # Change Log
2
2
  This project adheres to [Semantic Versioning](http://semver.org/).
3
3
 
4
+ ## 3.0
5
+ * Add `@font-face` to bubbling at-rules (by Valeriy Komlev).
6
+ * Add special logic for `@font-face` bubbling (by Phanindra Pydisetty).
7
+ * Use PostCSS selector parser 3.0.
8
+
9
+ ## 2.1.2
10
+ * Fix replacing multiple `&`.
11
+
12
+ ## 2.1.1
13
+ * Fix `:not(&)` support.
14
+
15
+ ## 2.1
16
+ * Add `preserveEmpty` option (by Federico Zivolo).
17
+
4
18
  ## 2.0.4
5
19
  * Fix finding `&` in some selectors (by Stepan Mikhaylyuk).
6
20
 
package/README.md CHANGED
@@ -74,9 +74,33 @@ See [PostCSS] docs for examples for your environment.
74
74
 
75
75
  ### `bubble`
76
76
 
77
- By default, plugin will unwrap only `@media`, `@supports` and `@document`
77
+ By default, plugin will unwrap only `@media`, `@supports`, `@font-face` and `@document
78
78
  at-rules. You can add your custom at-rules to this list by `bubble` option:
79
79
 
80
80
  ```js
81
- postcss([ require('postcss-nested')({ bubble: ['phone'] }) ]
81
+ postcss([ require('postcss-nested')({ bubble: ['phone'] }) ])
82
82
  ```
83
+
84
+ ### `preserveEmpty`
85
+
86
+ By default, plugin will strip out any empty selector generated by intermediate
87
+ nesting levels. You can set `preserveEmpty` to `true` to preserve them.
88
+
89
+ ```css
90
+ .a {
91
+ .b {
92
+ color: black;
93
+ }
94
+ }
95
+ ```
96
+
97
+ Will be compiled to:
98
+
99
+ ```css
100
+ .a { }
101
+ .a .b {
102
+ color: black;
103
+ }
104
+ ```
105
+
106
+ This is especially useful if you want to export the empty classes with `postcss-modules`.
package/index.js CHANGED
@@ -1,29 +1,43 @@
1
1
  var postcss = require('postcss');
2
2
  var parser = require('postcss-selector-parser');
3
3
 
4
- function concatNested(selector, parent) {
5
- var replaced = false;
6
- var nestingNodes = selector.nodes.filter(node => node.type === 'nesting');
7
- nestingNodes.forEach(ampersand => {
8
- ampersand.replaceWith(parent.clone());
9
- replaced = true;
4
+ function parse(str) {
5
+ var nodes;
6
+ var saver = parser(function (parsed) {
7
+ nodes = parsed;
10
8
  });
9
+ saver.processSync(str);
10
+ return nodes.at(0);
11
+ }
11
12
 
12
- if (!replaced) {
13
- selector.prepend(parser.combinator({ value: ' ' }));
14
- selector.prepend(parent.clone());
15
- }
16
-
17
- return selector;
13
+ function replace(nodes, parent) {
14
+ var replaced = false;
15
+ nodes.each(function (i) {
16
+ if (i.type === 'nesting') {
17
+ i.replaceWith(parent.clone());
18
+ replaced = true;
19
+ } else if (i.nodes) {
20
+ if (replace(i, parent)) {
21
+ replaced = true;
22
+ }
23
+ }
24
+ });
25
+ return replaced;
18
26
  }
19
27
 
20
- function selectors(parent, node) {
28
+ function selectors(parent, child) {
21
29
  var result = [];
22
30
  parent.selectors.forEach(function (i) {
23
- var parsed = parser().process(i).res.first;
31
+ var parentNode = parse(i);
24
32
 
25
- node.selectors.forEach(function (j) {
26
- result.push(concatNested(parser().process(j).res.first, parsed));
33
+ child.selectors.forEach(function (j) {
34
+ var node = parse(j);
35
+ var replaced = replace(node, parentNode);
36
+ if (!replaced) {
37
+ node.prepend(parser.combinator({ value: ' ' }));
38
+ node.prepend(parentNode.clone());
39
+ }
40
+ result.push(node.toString());
27
41
  });
28
42
  });
29
43
  return result;
@@ -51,6 +65,8 @@ function atruleChilds(rule, atrule) {
51
65
  atruleChilds(rule, child);
52
66
  }
53
67
  });
68
+ if ( atrule.name === 'font-face' ) return;
69
+
54
70
  if ( children.length ) {
55
71
  var clone = rule.clone({ nodes: [] });
56
72
  for ( var i = 0; i < children.length; i++ ) {
@@ -60,7 +76,7 @@ function atruleChilds(rule, atrule) {
60
76
  }
61
77
  }
62
78
 
63
- function processRule(rule, bubble) {
79
+ function processRule(rule, bubble, preserveEmpty) {
64
80
  var unwrapped = false;
65
81
  var after = rule;
66
82
  rule.each(function (child) {
@@ -80,24 +96,25 @@ function processRule(rule, bubble) {
80
96
  }
81
97
  }
82
98
  });
83
- if ( unwrapped ) {
99
+ if ( unwrapped && preserveEmpty !== true ) {
84
100
  rule.raws.semicolon = true;
85
101
  if ( rule.nodes.length === 0 ) rule.remove();
86
102
  }
87
103
  }
88
104
 
89
105
  module.exports = postcss.plugin('postcss-nested', function (opts) {
90
- var bubble = ['media', 'supports', 'document'];
106
+ var bubble = ['media', 'supports', 'document', 'font-face'];
91
107
  if ( opts && opts.bubble ) {
92
108
  bubble = bubble.concat(opts.bubble.map(function (i) {
93
109
  return i.replace(/^@/, '');
94
110
  }));
95
111
  }
112
+ var preserveEmpty = opts ? opts.preserveEmpty : false;
96
113
 
97
114
  var process = function (node) {
98
115
  node.each(function (child) {
99
116
  if ( child.type === 'rule' ) {
100
- processRule(child, bubble);
117
+ processRule(child, bubble, preserveEmpty);
101
118
  } else if ( child.type === 'atrule' ) {
102
119
  process(child);
103
120
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "postcss-nested",
3
- "version": "2.0.4",
3
+ "version": "3.0.0",
4
4
  "description": "PostCSS plugin to unwrap nested rules like how Sass does it.",
5
5
  "keywords": [
6
6
  "postcss",
@@ -13,13 +13,14 @@
13
13
  "license": "MIT",
14
14
  "repository": "postcss/postcss-nested",
15
15
  "dependencies": {
16
- "postcss": "^6.0.6",
17
- "postcss-selector-parser": "^2.2.3"
16
+ "postcss": "^6.0.14",
17
+ "postcss-selector-parser": "^3.1.1"
18
18
  },
19
19
  "devDependencies": {
20
- "eslint": "^4.2.0",
20
+ "eslint": "^4.13.0",
21
+ "eslint-ci": "^0.1.1",
21
22
  "eslint-config-postcss": "^2.0.2",
22
- "jest": "^20.0.4"
23
+ "jest": "^21.2.1"
23
24
  },
24
25
  "jest": {
25
26
  "coverageThreshold": {
@@ -29,7 +30,7 @@
29
30
  }
30
31
  },
31
32
  "scripts": {
32
- "test": "jest --coverage && eslint *.js"
33
+ "test": "jest --coverage && eslint-ci *.js"
33
34
  },
34
35
  "eslintConfig": {
35
36
  "extends": "eslint-config-postcss/es5",