postcss-reduce-transforms 5.0.2 → 5.1.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/README.md CHANGED
@@ -19,7 +19,7 @@ see the [tests](src/__tests__/index.js).
19
19
 
20
20
  ```css
21
21
  h1 {
22
- transform: rotate3d(0, 0, 1, 20deg);
22
+ transform: rotate3d(0, 0, 1, 20deg);
23
23
  }
24
24
  ```
25
25
 
@@ -27,7 +27,7 @@ h1 {
27
27
 
28
28
  ```css
29
29
  h1 {
30
- transform: rotate(20deg);
30
+ transform: rotate(20deg);
31
31
  }
32
32
  ```
33
33
 
package/package.json CHANGED
@@ -1,11 +1,13 @@
1
1
  {
2
2
  "name": "postcss-reduce-transforms",
3
- "version": "5.0.2",
3
+ "version": "5.1.0",
4
4
  "description": "Reduce transform functions with PostCSS.",
5
- "main": "dist/index.js",
5
+ "main": "src/index.js",
6
+ "types": "types/index.d.ts",
6
7
  "files": [
7
8
  "LICENSE-MIT",
8
- "dist"
9
+ "src",
10
+ "types"
9
11
  ],
10
12
  "license": "MIT",
11
13
  "homepage": "https://github.com/cssnano/cssnano",
@@ -30,9 +32,5 @@
30
32
  "peerDependencies": {
31
33
  "postcss": "^8.2.15"
32
34
  },
33
- "scripts": {
34
- "prebuild": "rimraf dist",
35
- "build": "babel src --config-file ../../babel.config.json --out-dir dist --ignore \"**/__tests__/\""
36
- },
37
- "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"
35
+ "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"
38
36
  }
package/src/index.js ADDED
@@ -0,0 +1,309 @@
1
+ 'use strict';
2
+ const valueParser = require('postcss-value-parser');
3
+
4
+ /**
5
+ * @param {(number|string)[]} list
6
+ * @param {valueParser.Node} node
7
+ * @param {number} index
8
+ * @return {(number|string)[]}
9
+ */
10
+ function getValues(list, node, index) {
11
+ if (index % 2 === 0) {
12
+ /** @type {number|string} */
13
+ let value = NaN;
14
+
15
+ if (
16
+ node.type === 'function' &&
17
+ (node.value === 'var' || node.value === 'env') &&
18
+ node.nodes.length === 1
19
+ ) {
20
+ value = valueParser.stringify(node.nodes);
21
+ } else if (node.type === 'word') {
22
+ value = parseFloat(node.value);
23
+ }
24
+
25
+ return [...list, value];
26
+ }
27
+
28
+ return list;
29
+ }
30
+
31
+ /**
32
+ * @param {valueParser.FunctionNode} node
33
+ * @param {(number|string)[]} values
34
+ * @return {void}
35
+ */
36
+ function matrix3d(node, values) {
37
+ if (values.length !== 16) {
38
+ return;
39
+ }
40
+
41
+ // matrix3d(a, b, 0, 0, c, d, 0, 0, 0, 0, 1, 0, tx, ty, 0, 1) => matrix(a, b, c, d, tx, ty)
42
+ if (
43
+ values[15] &&
44
+ values[2] === 0 &&
45
+ values[3] === 0 &&
46
+ values[6] === 0 &&
47
+ values[7] === 0 &&
48
+ values[8] === 0 &&
49
+ values[9] === 0 &&
50
+ values[10] === 1 &&
51
+ values[11] === 0 &&
52
+ values[14] === 0 &&
53
+ values[15] === 1
54
+ ) {
55
+ const { nodes } = node;
56
+
57
+ node.value = 'matrix';
58
+ node.nodes = [
59
+ nodes[0], // a
60
+ nodes[1], // ,
61
+ nodes[2], // b
62
+ nodes[3], // ,
63
+ nodes[8], // c
64
+ nodes[9], // ,
65
+ nodes[10], // d
66
+ nodes[11], // ,
67
+ nodes[24], // tx
68
+ nodes[25], // ,
69
+ nodes[26], // ty
70
+ ];
71
+ }
72
+ }
73
+
74
+ const rotate3dMappings = new Map([
75
+ [[1, 0, 0].toString(), 'rotateX'], // rotate3d(1, 0, 0, a) => rotateX(a)
76
+ [[0, 1, 0].toString(), 'rotateY'], // rotate3d(0, 1, 0, a) => rotateY(a)
77
+ [[0, 0, 1].toString(), 'rotate'], // rotate3d(0, 0, 1, a) => rotate(a)
78
+ ]);
79
+
80
+ /**
81
+ * @param {valueParser.FunctionNode} node
82
+ * @param {(number|string)[]} values
83
+ * @return {void}
84
+ */
85
+ function rotate3d(node, values) {
86
+ if (values.length !== 4) {
87
+ return;
88
+ }
89
+
90
+ const { nodes } = node;
91
+ const match = rotate3dMappings.get(values.slice(0, 3).toString());
92
+
93
+ if (match) {
94
+ node.value = match;
95
+ node.nodes = [nodes[6]];
96
+ }
97
+ }
98
+
99
+ /**
100
+ * @param {valueParser.FunctionNode} node
101
+ * @param {(number|string)[]} values
102
+ * @return {void}
103
+ */
104
+ function rotateZ(node, values) {
105
+ if (values.length !== 1) {
106
+ return;
107
+ }
108
+
109
+ // rotateZ(rz) => rotate(rz)
110
+ node.value = 'rotate';
111
+ }
112
+
113
+ /**
114
+ * @param {valueParser.FunctionNode} node
115
+ * @param {(number|string)[]} values
116
+ * @return {void}
117
+ */
118
+ function scale(node, values) {
119
+ if (values.length !== 2) {
120
+ return;
121
+ }
122
+
123
+ const { nodes } = node;
124
+ const [first, second] = values;
125
+
126
+ // scale(sx, sy) => scale(sx)
127
+ if (first === second) {
128
+ node.nodes = [nodes[0]];
129
+
130
+ return;
131
+ }
132
+
133
+ // scale(sx, 1) => scaleX(sx)
134
+ if (second === 1) {
135
+ node.value = 'scaleX';
136
+ node.nodes = [nodes[0]];
137
+
138
+ return;
139
+ }
140
+
141
+ // scale(1, sy) => scaleY(sy)
142
+ if (first === 1) {
143
+ node.value = 'scaleY';
144
+ node.nodes = [nodes[2]];
145
+
146
+ return;
147
+ }
148
+ }
149
+
150
+ /**
151
+ * @param {valueParser.FunctionNode} node
152
+ * @param {(number|string)[]} values
153
+ * @return {void}
154
+ */
155
+ function scale3d(node, values) {
156
+ if (values.length !== 3) {
157
+ return;
158
+ }
159
+
160
+ const { nodes } = node;
161
+ const [first, second, third] = values;
162
+
163
+ // scale3d(sx, 1, 1) => scaleX(sx)
164
+ if (second === 1 && third === 1) {
165
+ node.value = 'scaleX';
166
+ node.nodes = [nodes[0]];
167
+
168
+ return;
169
+ }
170
+
171
+ // scale3d(1, sy, 1) => scaleY(sy)
172
+ if (first === 1 && third === 1) {
173
+ node.value = 'scaleY';
174
+ node.nodes = [nodes[2]];
175
+
176
+ return;
177
+ }
178
+
179
+ // scale3d(1, 1, sz) => scaleZ(sz)
180
+ if (first === 1 && second === 1) {
181
+ node.value = 'scaleZ';
182
+ node.nodes = [nodes[4]];
183
+
184
+ return;
185
+ }
186
+ }
187
+
188
+ /**
189
+ * @param {valueParser.FunctionNode} node
190
+ * @param {(number|string)[]} values
191
+ * @return {void}
192
+ */
193
+ function translate(node, values) {
194
+ if (values.length !== 2) {
195
+ return;
196
+ }
197
+
198
+ const { nodes } = node;
199
+
200
+ // translate(tx, 0) => translate(tx)
201
+ if (values[1] === 0) {
202
+ node.nodes = [nodes[0]];
203
+
204
+ return;
205
+ }
206
+
207
+ // translate(0, ty) => translateY(ty)
208
+ if (values[0] === 0) {
209
+ node.value = 'translateY';
210
+ node.nodes = [nodes[2]];
211
+
212
+ return;
213
+ }
214
+ }
215
+
216
+ /**
217
+ * @param {valueParser.FunctionNode} node
218
+ * @param {(number|string)[]} values
219
+ * @return {void}
220
+ */
221
+ function translate3d(node, values) {
222
+ if (values.length !== 3) {
223
+ return;
224
+ }
225
+
226
+ const { nodes } = node;
227
+
228
+ // translate3d(0, 0, tz) => translateZ(tz)
229
+ if (values[0] === 0 && values[1] === 0) {
230
+ node.value = 'translateZ';
231
+ node.nodes = [nodes[4]];
232
+ }
233
+ }
234
+
235
+ const reducers = new Map([
236
+ ['matrix3d', matrix3d],
237
+ ['rotate3d', rotate3d],
238
+ ['rotateZ', rotateZ],
239
+ ['scale', scale],
240
+ ['scale3d', scale3d],
241
+ ['translate', translate],
242
+ ['translate3d', translate3d],
243
+ ]);
244
+ /**
245
+ * @param {string} name
246
+ * @return {string}
247
+ */
248
+ function normalizeReducerName(name) {
249
+ const lowerCasedName = name.toLowerCase();
250
+
251
+ if (lowerCasedName === 'rotatez') {
252
+ return 'rotateZ';
253
+ }
254
+
255
+ return lowerCasedName;
256
+ }
257
+
258
+ /**
259
+ * @param {valueParser.Node} node
260
+ * @return {false}
261
+ */
262
+ function reduce(node) {
263
+ if (node.type === 'function') {
264
+ const normalizedReducerName = normalizeReducerName(node.value);
265
+ const reducer = reducers.get(normalizedReducerName);
266
+ if (reducer !== undefined) {
267
+ reducer(node, node.nodes.reduce(getValues, []));
268
+ }
269
+ }
270
+ return false;
271
+ }
272
+
273
+ /**
274
+ * @type {import('postcss').PluginCreator<void>}
275
+ * @return {import('postcss').Plugin}
276
+ */
277
+ function pluginCreator() {
278
+ return {
279
+ postcssPlugin: 'postcss-reduce-transforms',
280
+ prepare() {
281
+ const cache = new Map();
282
+ return {
283
+ OnceExit(css) {
284
+ css.walkDecls(/transform$/i, (decl) => {
285
+ const value = decl.value;
286
+
287
+ if (!value) {
288
+ return;
289
+ }
290
+
291
+ if (cache.has(value)) {
292
+ decl.value = cache.get(value);
293
+
294
+ return;
295
+ }
296
+
297
+ const result = valueParser(value).walk(reduce).toString();
298
+
299
+ decl.value = result;
300
+ cache.set(value, result);
301
+ });
302
+ },
303
+ };
304
+ },
305
+ };
306
+ }
307
+
308
+ pluginCreator.postcss = true;
309
+ module.exports = pluginCreator;
@@ -0,0 +1,9 @@
1
+ export = pluginCreator;
2
+ /**
3
+ * @type {import('postcss').PluginCreator<void>}
4
+ * @return {import('postcss').Plugin}
5
+ */
6
+ declare function pluginCreator(): import('postcss').Plugin;
7
+ declare namespace pluginCreator {
8
+ const postcss: true;
9
+ }
package/dist/index.js DELETED
@@ -1,254 +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
- function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
11
-
12
- function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && 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; }
13
-
14
- function getValues(list, node, index) {
15
- if (index % 2 === 0) {
16
- let value = NaN;
17
-
18
- if (node.type === 'function' && (node.value === 'var' || node.value === 'env') && node.nodes.length === 1) {
19
- value = (0, _postcssValueParser.stringify)(node.nodes);
20
- } else if (node.type === 'word') {
21
- value = parseFloat(node.value);
22
- }
23
-
24
- return [...list, value];
25
- }
26
-
27
- return list;
28
- }
29
-
30
- function matrix3d(node, values) {
31
- if (values.length !== 16) {
32
- return;
33
- } // matrix3d(a, b, 0, 0, c, d, 0, 0, 0, 0, 1, 0, tx, ty, 0, 1) => matrix(a, b, c, d, tx, ty)
34
-
35
-
36
- 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) {
37
- const {
38
- nodes
39
- } = node;
40
- node.value = 'matrix';
41
- node.nodes = [nodes[0], // a
42
- nodes[1], // ,
43
- nodes[2], // b
44
- nodes[3], // ,
45
- nodes[8], // c
46
- nodes[9], // ,
47
- nodes[10], // d
48
- nodes[11], // ,
49
- nodes[24], // tx
50
- nodes[25], // ,
51
- nodes[26] // ty
52
- ];
53
- }
54
- }
55
-
56
- const rotate3dMappings = new Map([[[1, 0, 0].toString(), 'rotateX'], // rotate3d(1, 0, 0, a) => rotateX(a)
57
- [[0, 1, 0].toString(), 'rotateY'], // rotate3d(0, 1, 0, a) => rotateY(a)
58
- [[0, 0, 1].toString(), 'rotate'] // rotate3d(0, 0, 1, a) => rotate(a)
59
- ]);
60
-
61
- function rotate3d(node, values) {
62
- if (values.length !== 4) {
63
- return;
64
- }
65
-
66
- const {
67
- nodes
68
- } = node;
69
- const match = rotate3dMappings.get(values.slice(0, 3).toString());
70
-
71
- if (match) {
72
- node.value = match;
73
- node.nodes = [nodes[6]];
74
- }
75
- }
76
-
77
- function rotateZ(node, values) {
78
- if (values.length !== 1) {
79
- return;
80
- } // rotateZ(rz) => rotate(rz)
81
-
82
-
83
- node.value = 'rotate';
84
- }
85
-
86
- function scale(node, values) {
87
- if (values.length !== 2) {
88
- return;
89
- }
90
-
91
- const {
92
- nodes
93
- } = node;
94
- const [first, second] = values; // scale(sx, sy) => scale(sx)
95
-
96
- if (first === second) {
97
- node.nodes = [nodes[0]];
98
- return;
99
- } // scale(sx, 1) => scaleX(sx)
100
-
101
-
102
- if (second === 1) {
103
- node.value = 'scaleX';
104
- node.nodes = [nodes[0]];
105
- return;
106
- } // scale(1, sy) => scaleY(sy)
107
-
108
-
109
- if (first === 1) {
110
- node.value = 'scaleY';
111
- node.nodes = [nodes[2]];
112
- return;
113
- }
114
- }
115
-
116
- function scale3d(node, values) {
117
- if (values.length !== 3) {
118
- return;
119
- }
120
-
121
- const {
122
- nodes
123
- } = node;
124
- const [first, second, third] = values; // scale3d(sx, 1, 1) => scaleX(sx)
125
-
126
- if (second === 1 && third === 1) {
127
- node.value = 'scaleX';
128
- node.nodes = [nodes[0]];
129
- return;
130
- } // scale3d(1, sy, 1) => scaleY(sy)
131
-
132
-
133
- if (first === 1 && third === 1) {
134
- node.value = 'scaleY';
135
- node.nodes = [nodes[2]];
136
- return;
137
- } // scale3d(1, 1, sz) => scaleZ(sz)
138
-
139
-
140
- if (first === 1 && second === 1) {
141
- node.value = 'scaleZ';
142
- node.nodes = [nodes[4]];
143
- return;
144
- }
145
- }
146
-
147
- function translate(node, values) {
148
- if (values.length !== 2) {
149
- return;
150
- }
151
-
152
- const {
153
- nodes
154
- } = node; // translate(tx, 0) => translate(tx)
155
-
156
- if (values[1] === 0) {
157
- node.nodes = [nodes[0]];
158
- return;
159
- } // translate(0, ty) => translateY(ty)
160
-
161
-
162
- if (values[0] === 0) {
163
- node.value = 'translateY';
164
- node.nodes = [nodes[2]];
165
- return;
166
- }
167
- }
168
-
169
- function translate3d(node, values) {
170
- if (values.length !== 3) {
171
- return;
172
- }
173
-
174
- const {
175
- nodes
176
- } = node; // translate3d(0, 0, tz) => translateZ(tz)
177
-
178
- if (values[0] === 0 && values[1] === 0) {
179
- node.value = 'translateZ';
180
- node.nodes = [nodes[4]];
181
- }
182
- }
183
-
184
- const reducers = {
185
- matrix3d,
186
- rotate3d,
187
- rotateZ,
188
- scale,
189
- scale3d,
190
- translate,
191
- translate3d
192
- };
193
-
194
- function normalizeReducerName(name) {
195
- const lowerCasedName = name.toLowerCase();
196
-
197
- if (lowerCasedName === 'rotatez') {
198
- return 'rotateZ';
199
- }
200
-
201
- return lowerCasedName;
202
- }
203
-
204
- function reduce(node) {
205
- const {
206
- nodes,
207
- type,
208
- value
209
- } = node;
210
- const normalizedReducerName = normalizeReducerName(value);
211
-
212
- if (type === 'function' && Object.prototype.hasOwnProperty.call(reducers, normalizedReducerName)) {
213
- reducers[normalizedReducerName](node, nodes.reduce(getValues, []));
214
- }
215
-
216
- return false;
217
- }
218
-
219
- function pluginCreator() {
220
- return {
221
- postcssPlugin: 'postcss-reduce-transforms',
222
-
223
- prepare() {
224
- const cache = new Map();
225
- return {
226
- OnceExit(css) {
227
- css.walkDecls(/transform$/i, decl => {
228
- const value = decl.value;
229
-
230
- if (!value) {
231
- return;
232
- }
233
-
234
- if (cache.has(value)) {
235
- decl.value = cache.get(value);
236
- return;
237
- }
238
-
239
- const result = (0, _postcssValueParser.default)(value).walk(reduce).toString();
240
- decl.value = result;
241
- cache.set(value, result);
242
- });
243
- }
244
-
245
- };
246
- }
247
-
248
- };
249
- }
250
-
251
- pluginCreator.postcss = true;
252
- var _default = pluginCreator;
253
- exports.default = _default;
254
- module.exports = exports.default;