postcss-reduce-transforms 5.0.0 → 5.0.4

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/package.json CHANGED
@@ -1,16 +1,11 @@
1
1
  {
2
2
  "name": "postcss-reduce-transforms",
3
- "version": "5.0.0",
3
+ "version": "5.0.4",
4
4
  "description": "Reduce transform functions with PostCSS.",
5
- "main": "dist/index.js",
6
- "scripts": {
7
- "prebuild": "del-cli dist",
8
- "build": "cross-env BABEL_ENV=publish babel src --config-file ../../babel.config.js --out-dir dist --ignore \"**/__tests__/\"",
9
- "prepublish": "yarn build"
10
- },
5
+ "main": "src/index.js",
11
6
  "files": [
12
7
  "LICENSE-MIT",
13
- "dist"
8
+ "src"
14
9
  ],
15
10
  "license": "MIT",
16
11
  "homepage": "https://github.com/cssnano/cssnano",
@@ -21,8 +16,7 @@
21
16
  },
22
17
  "repository": "cssnano/cssnano",
23
18
  "dependencies": {
24
- "cssnano-utils": "^2.0.0",
25
- "postcss-value-parser": "^4.1.0"
19
+ "postcss-value-parser": "^4.2.0"
26
20
  },
27
21
  "bugs": {
28
22
  "url": "https://github.com/cssnano/cssnano/issues"
@@ -31,10 +25,10 @@
31
25
  "node": "^10 || ^12 || >=14.0"
32
26
  },
33
27
  "devDependencies": {
34
- "postcss": "^8.2.1"
28
+ "postcss": "^8.2.15"
35
29
  },
36
30
  "peerDependencies": {
37
- "postcss": "^8.2.1"
31
+ "postcss": "^8.2.15"
38
32
  },
39
- "gitHead": "0e2c3bf5835bafcdc8783bef66f730a24194c8f3"
40
- }
33
+ "readme": "# [postcss][postcss]-reduce-transforms\n\n> Reduce transform functions with PostCSS.\n\n## Install\n\nWith [npm](https://npmjs.org/package/postcss-reduce-transforms) do:\n\n```\nnpm install postcss-reduce-transforms --save\n```\n\n## Example\n\nThis module will reduce transform functions where possible. For more examples,\nsee the [tests](src/__tests__/index.js).\n\n### Input\n\n```css\nh1 {\n transform: rotate3d(0, 0, 1, 20deg);\n}\n```\n\n### Output\n\n```css\nh1 {\n transform: rotate(20deg);\n}\n```\n\n## Usage\n\nSee the [PostCSS documentation](https://github.com/postcss/postcss#usage) for\nexamples for your environment.\n\n## Contributors\n\nSee [CONTRIBUTORS.md](https://github.com/cssnano/cssnano/blob/master/CONTRIBUTORS.md).\n\n## License\n\nMIT © [Ben Briggs](http://beneb.info)\n\n[postcss]: https://github.com/postcss/postcss\n"
34
+ }
package/src/index.js ADDED
@@ -0,0 +1,256 @@
1
+ 'use strict';
2
+ const valueParser = require('postcss-value-parser');
3
+
4
+ function getValues(list, node, index) {
5
+ if (index % 2 === 0) {
6
+ let value = NaN;
7
+
8
+ if (
9
+ node.type === 'function' &&
10
+ (node.value === 'var' || node.value === 'env') &&
11
+ node.nodes.length === 1
12
+ ) {
13
+ value = valueParser.stringify(node.nodes);
14
+ } else if (node.type === 'word') {
15
+ value = parseFloat(node.value);
16
+ }
17
+
18
+ return [...list, value];
19
+ }
20
+
21
+ return list;
22
+ }
23
+
24
+ function matrix3d(node, values) {
25
+ if (values.length !== 16) {
26
+ return;
27
+ }
28
+
29
+ // matrix3d(a, b, 0, 0, c, d, 0, 0, 0, 0, 1, 0, tx, ty, 0, 1) => matrix(a, b, c, d, tx, ty)
30
+ if (
31
+ values[15] &&
32
+ values[2] === 0 &&
33
+ values[3] === 0 &&
34
+ values[6] === 0 &&
35
+ values[7] === 0 &&
36
+ values[8] === 0 &&
37
+ values[9] === 0 &&
38
+ values[10] === 1 &&
39
+ values[11] === 0 &&
40
+ values[14] === 0 &&
41
+ values[15] === 1
42
+ ) {
43
+ const { nodes } = node;
44
+
45
+ node.value = 'matrix';
46
+ node.nodes = [
47
+ nodes[0], // a
48
+ nodes[1], // ,
49
+ nodes[2], // b
50
+ nodes[3], // ,
51
+ nodes[8], // c
52
+ nodes[9], // ,
53
+ nodes[10], // d
54
+ nodes[11], // ,
55
+ nodes[24], // tx
56
+ nodes[25], // ,
57
+ nodes[26], // ty
58
+ ];
59
+ }
60
+ }
61
+
62
+ const rotate3dMappings = new Map([
63
+ [[1, 0, 0].toString(), 'rotateX'], // rotate3d(1, 0, 0, a) => rotateX(a)
64
+ [[0, 1, 0].toString(), 'rotateY'], // rotate3d(0, 1, 0, a) => rotateY(a)
65
+ [[0, 0, 1].toString(), 'rotate'], // rotate3d(0, 0, 1, a) => rotate(a)
66
+ ]);
67
+
68
+ function rotate3d(node, values) {
69
+ if (values.length !== 4) {
70
+ return;
71
+ }
72
+
73
+ const { nodes } = node;
74
+ const match = rotate3dMappings.get(values.slice(0, 3).toString());
75
+
76
+ if (match) {
77
+ node.value = match;
78
+ node.nodes = [nodes[6]];
79
+ }
80
+ }
81
+
82
+ function rotateZ(node, values) {
83
+ if (values.length !== 1) {
84
+ return;
85
+ }
86
+
87
+ // rotateZ(rz) => rotate(rz)
88
+ node.value = 'rotate';
89
+ }
90
+
91
+ function scale(node, values) {
92
+ if (values.length !== 2) {
93
+ return;
94
+ }
95
+
96
+ const { nodes } = node;
97
+ const [first, second] = values;
98
+
99
+ // scale(sx, sy) => scale(sx)
100
+ if (first === second) {
101
+ node.nodes = [nodes[0]];
102
+
103
+ return;
104
+ }
105
+
106
+ // scale(sx, 1) => scaleX(sx)
107
+ if (second === 1) {
108
+ node.value = 'scaleX';
109
+ node.nodes = [nodes[0]];
110
+
111
+ return;
112
+ }
113
+
114
+ // scale(1, sy) => scaleY(sy)
115
+ if (first === 1) {
116
+ node.value = 'scaleY';
117
+ node.nodes = [nodes[2]];
118
+
119
+ return;
120
+ }
121
+ }
122
+
123
+ function scale3d(node, values) {
124
+ if (values.length !== 3) {
125
+ return;
126
+ }
127
+
128
+ const { nodes } = node;
129
+ const [first, second, third] = values;
130
+
131
+ // scale3d(sx, 1, 1) => scaleX(sx)
132
+ if (second === 1 && third === 1) {
133
+ node.value = 'scaleX';
134
+ node.nodes = [nodes[0]];
135
+
136
+ return;
137
+ }
138
+
139
+ // scale3d(1, sy, 1) => scaleY(sy)
140
+ if (first === 1 && third === 1) {
141
+ node.value = 'scaleY';
142
+ node.nodes = [nodes[2]];
143
+
144
+ return;
145
+ }
146
+
147
+ // scale3d(1, 1, sz) => scaleZ(sz)
148
+ if (first === 1 && second === 1) {
149
+ node.value = 'scaleZ';
150
+ node.nodes = [nodes[4]];
151
+
152
+ return;
153
+ }
154
+ }
155
+
156
+ function translate(node, values) {
157
+ if (values.length !== 2) {
158
+ return;
159
+ }
160
+
161
+ const { nodes } = node;
162
+
163
+ // translate(tx, 0) => translate(tx)
164
+ if (values[1] === 0) {
165
+ node.nodes = [nodes[0]];
166
+
167
+ return;
168
+ }
169
+
170
+ // translate(0, ty) => translateY(ty)
171
+ if (values[0] === 0) {
172
+ node.value = 'translateY';
173
+ node.nodes = [nodes[2]];
174
+
175
+ return;
176
+ }
177
+ }
178
+
179
+ function translate3d(node, values) {
180
+ if (values.length !== 3) {
181
+ return;
182
+ }
183
+
184
+ const { nodes } = node;
185
+
186
+ // translate3d(0, 0, tz) => translateZ(tz)
187
+ if (values[0] === 0 && values[1] === 0) {
188
+ node.value = 'translateZ';
189
+ node.nodes = [nodes[4]];
190
+ }
191
+ }
192
+
193
+ const reducers = new Map([
194
+ ['matrix3d', matrix3d],
195
+ ['rotate3d', rotate3d],
196
+ ['rotateZ', rotateZ],
197
+ ['scale', scale],
198
+ ['scale3d', scale3d],
199
+ ['translate', translate],
200
+ ['translate3d', translate3d],
201
+ ]);
202
+
203
+ function normalizeReducerName(name) {
204
+ const lowerCasedName = name.toLowerCase();
205
+
206
+ if (lowerCasedName === 'rotatez') {
207
+ return 'rotateZ';
208
+ }
209
+
210
+ return lowerCasedName;
211
+ }
212
+
213
+ function reduce(node) {
214
+ if (node.type === 'function') {
215
+ const normalizedReducerName = normalizeReducerName(node.value);
216
+ const reducer = reducers.get(normalizedReducerName);
217
+ if (reducer !== undefined) {
218
+ reducer(node, node.nodes.reduce(getValues, []));
219
+ }
220
+ }
221
+ return false;
222
+ }
223
+
224
+ function pluginCreator() {
225
+ return {
226
+ postcssPlugin: 'postcss-reduce-transforms',
227
+ prepare() {
228
+ const cache = new Map();
229
+ return {
230
+ OnceExit(css) {
231
+ css.walkDecls(/transform$/i, (decl) => {
232
+ const value = decl.value;
233
+
234
+ if (!value) {
235
+ return;
236
+ }
237
+
238
+ if (cache.has(value)) {
239
+ decl.value = cache.get(value);
240
+
241
+ return;
242
+ }
243
+
244
+ const result = valueParser(value).walk(reduce).toString();
245
+
246
+ decl.value = result;
247
+ cache.set(value, result);
248
+ });
249
+ },
250
+ };
251
+ },
252
+ };
253
+ }
254
+
255
+ pluginCreator.postcss = true;
256
+ module.exports = pluginCreator;
package/CHANGELOG.md DELETED
@@ -1,70 +0,0 @@
1
- # Change Log
2
-
3
- All notable changes to this project will be documented in this file.
4
- See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
-
6
- # [5.0.0](https://github.com/cssnano/cssnano/compare/postcss-reduce-transforms@5.0.0-rc.2...postcss-reduce-transforms@5.0.0) (2021-04-06)
7
-
8
- **Note:** Version bump only for package postcss-reduce-transforms
9
-
10
-
11
-
12
-
13
-
14
- # [5.0.0-rc.2](https://github.com/cssnano/cssnano/compare/postcss-reduce-transforms@5.0.0-rc.1...postcss-reduce-transforms@5.0.0-rc.2) (2021-03-15)
15
-
16
- **Note:** Version bump only for package postcss-reduce-transforms
17
-
18
-
19
-
20
-
21
-
22
- # [5.0.0-rc.1](https://github.com/cssnano/cssnano/compare/postcss-reduce-transforms@5.0.0-rc.0...postcss-reduce-transforms@5.0.0-rc.1) (2021-03-04)
23
-
24
- **Note:** Version bump only for package postcss-reduce-transforms
25
-
26
-
27
-
28
-
29
-
30
- # 5.0.0-rc.0 (2021-02-19)
31
-
32
-
33
- ### chore
34
-
35
- * minimum require version of node is 10.13 ([#871](https://github.com/cssnano/cssnano/issues/871)) ([28bda24](https://github.com/cssnano/cssnano/commit/28bda243e32ce3ba89b3c358a5f78727b3732f11))
36
-
37
-
38
- ### Features
39
-
40
- * migarete to PostCSS 8 ([#975](https://github.com/cssnano/cssnano/issues/975)) ([40b82dc](https://github.com/cssnano/cssnano/commit/40b82dca7f53ac02cd4fe62846dec79b898ccb49))
41
- * **postcss-reduce-transforms:** improve optimizations ([#745](https://github.com/cssnano/cssnano/issues/745)) ([b0f0d89](https://github.com/cssnano/cssnano/commit/b0f0d892316d7b77e8033a6dc8d67745043a5072))
42
-
43
-
44
- ### BREAKING CHANGES
45
-
46
- * minimum supported `postcss` version is `8.2.1`
47
- * minimum require version of node is 10.13
48
-
49
-
50
-
51
- ## 4.1.9 (2019-02-12)
52
-
53
-
54
- ### Bug Fixes
55
-
56
- * **fix-postcss-reduce-transforms:** cache ([#691](https://github.com/cssnano/cssnano/issues/691)) ([e07309f](https://github.com/cssnano/cssnano/commit/e07309ff0c07fb36ef2340bbed6b3aee3dad18be))
57
-
58
-
59
- ### Performance Improvements
60
-
61
- * **postcss-reduce-transforms:** increase perf ([#688](https://github.com/cssnano/cssnano/issues/688)) ([84ccba0](https://github.com/cssnano/cssnano/commit/84ccba00cead30e80510525cbcd27ba259c26065))
62
-
63
-
64
-
65
- ## 4.1.1 (2018-09-24)
66
-
67
-
68
- ### Bug Fixes
69
-
70
- * **postcss-merge-longhand:** not mangle border output ([#555](https://github.com/cssnano/cssnano/issues/555)) ([9a70605](https://github.com/cssnano/cssnano/commit/9a706050b621e7795a9bf74eb7110b5c81804ffe)), closes [#553](https://github.com/cssnano/cssnano/issues/553) [#554](https://github.com/cssnano/cssnano/issues/554)
package/dist/index.js DELETED
@@ -1,257 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
-
8
- var _postcssValueParser = _interopRequireWildcard(require("postcss-value-parser"));
9
-
10
- var _cssnanoUtils = require("cssnano-utils");
11
-
12
- function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
13
-
14
- function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
15
-
16
- function getValues(list, node, index) {
17
- if (index % 2 === 0) {
18
- let value = NaN;
19
-
20
- if (node.type === 'function' && (node.value === 'var' || node.value === 'env') && node.nodes.length === 1) {
21
- value = (0, _postcssValueParser.stringify)(node.nodes);
22
- } else if (node.type === 'word') {
23
- value = parseFloat(node.value);
24
- }
25
-
26
- return [...list, value];
27
- }
28
-
29
- return list;
30
- }
31
-
32
- function matrix3d(node, values) {
33
- if (values.length !== 16) {
34
- return;
35
- } // matrix3d(a, b, 0, 0, c, d, 0, 0, 0, 0, 1, 0, tx, ty, 0, 1) => matrix(a, b, c, d, tx, ty)
36
-
37
-
38
- if (values[15] && values[2] === 0 && values[3] === 0 && values[6] === 0 && values[7] === 0 && values[8] === 0 && values[9] === 0 && values[10] === 1 && values[11] === 0 && values[14] === 0 && values[15] === 1) {
39
- const {
40
- nodes
41
- } = node;
42
- node.value = 'matrix';
43
- node.nodes = [nodes[0], // a
44
- nodes[1], // ,
45
- nodes[2], // b
46
- nodes[3], // ,
47
- nodes[8], // c
48
- nodes[9], // ,
49
- nodes[10], // d
50
- nodes[11], // ,
51
- nodes[24], // tx
52
- nodes[25], // ,
53
- nodes[26] // ty
54
- ];
55
- }
56
- }
57
-
58
- const rotate3dMappings = [['rotateX', [1, 0, 0]], // rotate3d(1, 0, 0, a) => rotateX(a)
59
- ['rotateY', [0, 1, 0]], // rotate3d(0, 1, 0, a) => rotateY(a)
60
- ['rotate', [0, 0, 1]] // rotate3d(0, 0, 1, a) => rotate(a)
61
- ];
62
- const rotate3dMatch = (0, _cssnanoUtils.getMatch)(rotate3dMappings);
63
-
64
- function rotate3d(node, values) {
65
- if (values.length !== 4) {
66
- return;
67
- }
68
-
69
- const {
70
- nodes
71
- } = node;
72
- const match = rotate3dMatch(values.slice(0, 3));
73
-
74
- if (match.length) {
75
- node.value = match;
76
- node.nodes = [nodes[6]];
77
- }
78
- }
79
-
80
- function rotateZ(node, values) {
81
- if (values.length !== 1) {
82
- return;
83
- } // rotateZ(rz) => rotate(rz)
84
-
85
-
86
- node.value = 'rotate';
87
- }
88
-
89
- function scale(node, values) {
90
- if (values.length !== 2) {
91
- return;
92
- }
93
-
94
- const {
95
- nodes
96
- } = node;
97
- const [first, second] = values; // scale(sx, sy) => scale(sx)
98
-
99
- if (first === second) {
100
- node.nodes = [nodes[0]];
101
- return;
102
- } // scale(sx, 1) => scaleX(sx)
103
-
104
-
105
- if (second === 1) {
106
- node.value = 'scaleX';
107
- node.nodes = [nodes[0]];
108
- return;
109
- } // scale(1, sy) => scaleY(sy)
110
-
111
-
112
- if (first === 1) {
113
- node.value = 'scaleY';
114
- node.nodes = [nodes[2]];
115
- return;
116
- }
117
- }
118
-
119
- function scale3d(node, values) {
120
- if (values.length !== 3) {
121
- return;
122
- }
123
-
124
- const {
125
- nodes
126
- } = node;
127
- const [first, second, third] = values; // scale3d(sx, 1, 1) => scaleX(sx)
128
-
129
- if (second === 1 && third === 1) {
130
- node.value = 'scaleX';
131
- node.nodes = [nodes[0]];
132
- return;
133
- } // scale3d(1, sy, 1) => scaleY(sy)
134
-
135
-
136
- if (first === 1 && third === 1) {
137
- node.value = 'scaleY';
138
- node.nodes = [nodes[2]];
139
- return;
140
- } // scale3d(1, 1, sz) => scaleZ(sz)
141
-
142
-
143
- if (first === 1 && second === 1) {
144
- node.value = 'scaleZ';
145
- node.nodes = [nodes[4]];
146
- return;
147
- }
148
- }
149
-
150
- function translate(node, values) {
151
- if (values.length !== 2) {
152
- return;
153
- }
154
-
155
- const {
156
- nodes
157
- } = node; // translate(tx, 0) => translate(tx)
158
-
159
- if (values[1] === 0) {
160
- node.nodes = [nodes[0]];
161
- return;
162
- } // translate(0, ty) => translateY(ty)
163
-
164
-
165
- if (values[0] === 0) {
166
- node.value = 'translateY';
167
- node.nodes = [nodes[2]];
168
- return;
169
- }
170
- }
171
-
172
- function translate3d(node, values) {
173
- if (values.length !== 3) {
174
- return;
175
- }
176
-
177
- const {
178
- nodes
179
- } = node; // translate3d(0, 0, tz) => translateZ(tz)
180
-
181
- if (values[0] === 0 && values[1] === 0) {
182
- node.value = 'translateZ';
183
- node.nodes = [nodes[4]];
184
- }
185
- }
186
-
187
- const reducers = {
188
- matrix3d,
189
- rotate3d,
190
- rotateZ,
191
- scale,
192
- scale3d,
193
- translate,
194
- translate3d
195
- };
196
-
197
- function normalizeReducerName(name) {
198
- const lowerCasedName = name.toLowerCase();
199
-
200
- if (lowerCasedName === 'rotatez') {
201
- return 'rotateZ';
202
- }
203
-
204
- return lowerCasedName;
205
- }
206
-
207
- function reduce(node) {
208
- const {
209
- nodes,
210
- type,
211
- value
212
- } = node;
213
- const normalizedReducerName = normalizeReducerName(value);
214
-
215
- if (type === 'function' && Object.prototype.hasOwnProperty.call(reducers, normalizedReducerName)) {
216
- reducers[normalizedReducerName](node, nodes.reduce(getValues, []));
217
- }
218
-
219
- return false;
220
- }
221
-
222
- function pluginCreator() {
223
- return {
224
- postcssPlugin: 'postcss-reduce-transforms',
225
-
226
- prepare() {
227
- const cache = {};
228
- return {
229
- OnceExit(css) {
230
- css.walkDecls(/transform$/i, decl => {
231
- const value = decl.value;
232
-
233
- if (!value) {
234
- return;
235
- }
236
-
237
- if (cache[value]) {
238
- decl.value = cache[value];
239
- return;
240
- }
241
-
242
- const result = (0, _postcssValueParser.default)(value).walk(reduce).toString();
243
- decl.value = result;
244
- cache[value] = result;
245
- });
246
- }
247
-
248
- };
249
- }
250
-
251
- };
252
- }
253
-
254
- pluginCreator.postcss = true;
255
- var _default = pluginCreator;
256
- exports.default = _default;
257
- module.exports = exports.default;