postcss-merge-idents 5.0.2 → 5.0.3

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.
Files changed (3) hide show
  1. package/package.json +4 -8
  2. package/src/index.js +118 -0
  3. package/dist/index.js +0 -103
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "postcss-merge-idents",
3
- "version": "5.0.2",
3
+ "version": "5.0.3",
4
4
  "description": "Merge keyframe and counter style identifiers.",
5
- "main": "dist/index.js",
5
+ "main": "src/index.js",
6
6
  "files": [
7
- "dist",
7
+ "src",
8
8
  "LICENSE-MIT"
9
9
  ],
10
10
  "keywords": [
@@ -22,7 +22,7 @@
22
22
  },
23
23
  "repository": "cssnano/cssnano",
24
24
  "dependencies": {
25
- "cssnano-utils": "^3.0.0",
25
+ "cssnano-utils": "^3.0.2",
26
26
  "postcss-value-parser": "^4.2.0"
27
27
  },
28
28
  "bugs": {
@@ -37,9 +37,5 @@
37
37
  "peerDependencies": {
38
38
  "postcss": "^8.2.15"
39
39
  },
40
- "scripts": {
41
- "prebuild": "rimraf dist",
42
- "build": "babel src --config-file ../../babel.config.json --out-dir dist --ignore \"**/__tests__/\""
43
- },
44
40
  "readme": "# [postcss][postcss]-merge-idents\n\n> Merge keyframe and counter style identifiers.\n\n\n## Install\n\nWith [npm](https://npmjs.org/package/postcss-merge-idents) do:\n\n```\nnpm install postcss-merge-idents --save\n```\n\n\n## Example\n\nThis module will merge identifiers such as `@keyframes` and `@counter-style`,\nif their properties are identical. Then, it will update those declarations that\ndepend on the duplicated property.\n\n### Input\n\n```css\n@keyframes rotate {\n from { transform: rotate(0) }\n to { transform: rotate(360deg) }\n}\n\n@keyframes flip {\n from { transform: rotate(0) }\n to { transform: rotate(360deg) }\n}\n\n.rotate {\n animation-name: rotate\n}\n\n.flip {\n animation-name: flip\n}\n```\n\n### Output\n\n```css\n@keyframes flip {\n from { transform: rotate(0) }\n to { transform: rotate(360deg) }\n}\n\n.rotate {\n animation-name: flip\n}\n\n.flip {\n animation-name: flip\n}\n```\n\n\n## Usage\n\nSee the [PostCSS documentation](https://github.com/postcss/postcss#usage) for\nexamples for your environment.\n\n\n## Contributors\n\nSee [CONTRIBUTORS.md](https://github.com/cssnano/cssnano/blob/master/CONTRIBUTORS.md).\n\n\n## License\n\nMIT © [Ben Briggs](http://beneb.info)\n\n\n[postcss]: https://github.com/postcss/postcss\n"
45
41
  }
package/src/index.js ADDED
@@ -0,0 +1,118 @@
1
+ 'use strict';
2
+ const valueParser = require('postcss-value-parser');
3
+ const { sameParent } = require('cssnano-utils');
4
+
5
+ function canonical(obj) {
6
+ // Prevent potential infinite loops
7
+ let stack = 50;
8
+
9
+ return function recurse(key) {
10
+ if (
11
+ Object.prototype.hasOwnProperty.call(obj, key) &&
12
+ obj[key] !== key &&
13
+ stack
14
+ ) {
15
+ stack--;
16
+
17
+ return recurse(obj[key]);
18
+ }
19
+
20
+ stack = 50;
21
+
22
+ return key;
23
+ };
24
+ }
25
+
26
+ function mergeAtRules(css, pairs) {
27
+ pairs.forEach((pair) => {
28
+ pair.cache = [];
29
+ pair.replacements = [];
30
+ pair.decls = [];
31
+ pair.removals = [];
32
+ });
33
+
34
+ let relevant;
35
+
36
+ css.walk((node) => {
37
+ if (node.type === 'atrule') {
38
+ relevant = pairs.filter((pair) =>
39
+ pair.atrule.test(node.name.toLowerCase())
40
+ )[0];
41
+
42
+ if (!relevant) {
43
+ return;
44
+ }
45
+
46
+ if (relevant.cache.length < 1) {
47
+ relevant.cache.push(node);
48
+ return;
49
+ } else {
50
+ let toString = node.nodes.toString();
51
+
52
+ relevant.cache.forEach((cached) => {
53
+ if (
54
+ cached.name.toLowerCase() === node.name.toLowerCase() &&
55
+ sameParent(cached, node) &&
56
+ cached.nodes.toString() === toString
57
+ ) {
58
+ relevant.removals.push(cached);
59
+ relevant.replacements[cached.params] = node.params;
60
+ }
61
+ });
62
+
63
+ relevant.cache.push(node);
64
+
65
+ return;
66
+ }
67
+ }
68
+
69
+ if (node.type === 'decl') {
70
+ relevant = pairs.filter((pair) =>
71
+ pair.decl.test(node.prop.toLowerCase())
72
+ )[0];
73
+
74
+ if (!relevant) {
75
+ return;
76
+ }
77
+
78
+ relevant.decls.push(node);
79
+ }
80
+ });
81
+
82
+ pairs.forEach((pair) => {
83
+ let canon = canonical(pair.replacements);
84
+
85
+ pair.decls.forEach((decl) => {
86
+ decl.value = valueParser(decl.value)
87
+ .walk((node) => {
88
+ if (node.type === 'word') {
89
+ node.value = canon(node.value);
90
+ }
91
+ })
92
+ .toString();
93
+ });
94
+ pair.removals.forEach((cached) => cached.remove());
95
+ });
96
+ }
97
+
98
+ function pluginCreator() {
99
+ return {
100
+ postcssPlugin: 'postcss-merge-idents',
101
+
102
+ OnceExit(css) {
103
+ mergeAtRules(css, [
104
+ {
105
+ atrule: /keyframes/i,
106
+ decl: /animation/i,
107
+ },
108
+ {
109
+ atrule: /counter-style/i,
110
+ decl: /(list-style|system)/i,
111
+ },
112
+ ]);
113
+ },
114
+ };
115
+ }
116
+
117
+ pluginCreator.postcss = true;
118
+ module.exports = pluginCreator;
package/dist/index.js DELETED
@@ -1,103 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
-
8
- var _postcssValueParser = _interopRequireDefault(require("postcss-value-parser"));
9
-
10
- var _cssnanoUtils = require("cssnano-utils");
11
-
12
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
-
14
- function canonical(obj) {
15
- // Prevent potential infinite loops
16
- let stack = 50;
17
- return function recurse(key) {
18
- if (Object.prototype.hasOwnProperty.call(obj, key) && obj[key] !== key && stack) {
19
- stack--;
20
- return recurse(obj[key]);
21
- }
22
-
23
- stack = 50;
24
- return key;
25
- };
26
- }
27
-
28
- function mergeAtRules(css, pairs) {
29
- pairs.forEach(pair => {
30
- pair.cache = [];
31
- pair.replacements = [];
32
- pair.decls = [];
33
- pair.removals = [];
34
- });
35
- let relevant;
36
- css.walk(node => {
37
- if (node.type === 'atrule') {
38
- relevant = pairs.filter(pair => pair.atrule.test(node.name.toLowerCase()))[0];
39
-
40
- if (!relevant) {
41
- return;
42
- }
43
-
44
- if (relevant.cache.length < 1) {
45
- relevant.cache.push(node);
46
- return;
47
- } else {
48
- let toString = node.nodes.toString();
49
- relevant.cache.forEach(cached => {
50
- if (cached.name.toLowerCase() === node.name.toLowerCase() && (0, _cssnanoUtils.sameParent)(cached, node) && cached.nodes.toString() === toString) {
51
- relevant.removals.push(cached);
52
- relevant.replacements[cached.params] = node.params;
53
- }
54
- });
55
- relevant.cache.push(node);
56
- return;
57
- }
58
- }
59
-
60
- if (node.type === 'decl') {
61
- relevant = pairs.filter(pair => pair.decl.test(node.prop.toLowerCase()))[0];
62
-
63
- if (!relevant) {
64
- return;
65
- }
66
-
67
- relevant.decls.push(node);
68
- }
69
- });
70
- pairs.forEach(pair => {
71
- let canon = canonical(pair.replacements);
72
- pair.decls.forEach(decl => {
73
- decl.value = (0, _postcssValueParser.default)(decl.value).walk(node => {
74
- if (node.type === 'word') {
75
- node.value = canon(node.value);
76
- }
77
- }).toString();
78
- });
79
- pair.removals.forEach(cached => cached.remove());
80
- });
81
- }
82
-
83
- function pluginCreator() {
84
- return {
85
- postcssPlugin: 'postcss-merge-idents',
86
-
87
- OnceExit(css) {
88
- mergeAtRules(css, [{
89
- atrule: /keyframes/i,
90
- decl: /animation/i
91
- }, {
92
- atrule: /counter-style/i,
93
- decl: /(list-style|system)/i
94
- }]);
95
- }
96
-
97
- };
98
- }
99
-
100
- pluginCreator.postcss = true;
101
- var _default = pluginCreator;
102
- exports.default = _default;
103
- module.exports = exports.default;