postcss-reduce-transforms 5.0.3 → 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.
Files changed (3) hide show
  1. package/package.json +3 -7
  2. package/src/index.js +256 -0
  3. package/dist/index.js +0 -244
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "postcss-reduce-transforms",
3
- "version": "5.0.3",
3
+ "version": "5.0.4",
4
4
  "description": "Reduce transform functions with PostCSS.",
5
- "main": "dist/index.js",
5
+ "main": "src/index.js",
6
6
  "files": [
7
7
  "LICENSE-MIT",
8
- "dist"
8
+ "src"
9
9
  ],
10
10
  "license": "MIT",
11
11
  "homepage": "https://github.com/cssnano/cssnano",
@@ -30,9 +30,5 @@
30
30
  "peerDependencies": {
31
31
  "postcss": "^8.2.15"
32
32
  },
33
- "scripts": {
34
- "prebuild": "rimraf dist",
35
- "build": "babel src --config-file ../../babel.config.json --out-dir dist --ignore \"**/__tests__/\""
36
- },
37
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"
38
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/dist/index.js DELETED
@@ -1,244 +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 = new Map([['matrix3d', matrix3d], ['rotate3d', rotate3d], ['rotateZ', rotateZ], ['scale', scale], ['scale3d', scale3d], ['translate', translate], ['translate3d', translate3d]]);
185
-
186
- function normalizeReducerName(name) {
187
- const lowerCasedName = name.toLowerCase();
188
-
189
- if (lowerCasedName === 'rotatez') {
190
- return 'rotateZ';
191
- }
192
-
193
- return lowerCasedName;
194
- }
195
-
196
- function reduce(node) {
197
- if (node.type === 'function') {
198
- const normalizedReducerName = normalizeReducerName(node.value);
199
- const reducer = reducers.get(normalizedReducerName);
200
-
201
- if (reducer !== undefined) {
202
- reducer(node, node.nodes.reduce(getValues, []));
203
- }
204
- }
205
-
206
- return false;
207
- }
208
-
209
- function pluginCreator() {
210
- return {
211
- postcssPlugin: 'postcss-reduce-transforms',
212
-
213
- prepare() {
214
- const cache = new Map();
215
- return {
216
- OnceExit(css) {
217
- css.walkDecls(/transform$/i, decl => {
218
- const value = decl.value;
219
-
220
- if (!value) {
221
- return;
222
- }
223
-
224
- if (cache.has(value)) {
225
- decl.value = cache.get(value);
226
- return;
227
- }
228
-
229
- const result = (0, _postcssValueParser.default)(value).walk(reduce).toString();
230
- decl.value = result;
231
- cache.set(value, result);
232
- });
233
- }
234
-
235
- };
236
- }
237
-
238
- };
239
- }
240
-
241
- pluginCreator.postcss = true;
242
- var _default = pluginCreator;
243
- exports.default = _default;
244
- module.exports = exports.default;