postcss-calc 8.1.0 → 8.2.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/dist/index.js +13 -0
- package/dist/lib/convertUnit.js +11 -1
- package/dist/lib/reducer.js +87 -22
- package/dist/lib/stringifier.js +22 -2
- package/dist/lib/transform.js +32 -4
- package/package.json +9 -9
- package/types/index.d.ts +30 -0
- package/types/lib/convertUnit.d.ts +8 -0
- package/types/lib/reducer.d.ts +10 -0
- package/types/lib/stringifier.d.ts +14 -0
- package/types/lib/transform.d.ts +6 -0
package/dist/index.js
CHANGED
|
@@ -9,6 +9,15 @@ var _transform = _interopRequireDefault(require("./lib/transform"));
|
|
|
9
9
|
|
|
10
10
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
11
|
|
|
12
|
+
/**
|
|
13
|
+
* @typedef {{precision?: number | false,
|
|
14
|
+
* preserve?: boolean,
|
|
15
|
+
* warnWhenCannotResolve?: boolean,
|
|
16
|
+
* mediaQueries?: boolean,
|
|
17
|
+
* selectors?: boolean}} PostCssCalcOptions
|
|
18
|
+
*
|
|
19
|
+
* @param {PostCssCalcOptions} opts
|
|
20
|
+
*/
|
|
12
21
|
function pluginCreator(opts) {
|
|
13
22
|
const options = Object.assign({
|
|
14
23
|
precision: 5,
|
|
@@ -20,6 +29,10 @@ function pluginCreator(opts) {
|
|
|
20
29
|
return {
|
|
21
30
|
postcssPlugin: 'postcss-calc',
|
|
22
31
|
|
|
32
|
+
/**
|
|
33
|
+
* @param {import('postcss').Root} css
|
|
34
|
+
* @param {{result: import('postcss').Result}} helpers
|
|
35
|
+
*/
|
|
23
36
|
OnceExit(css, {
|
|
24
37
|
result
|
|
25
38
|
}) {
|
package/dist/lib/convertUnit.js
CHANGED
|
@@ -4,6 +4,10 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @type {{[key:string]: {[key:string]: number}}}
|
|
10
|
+
*/
|
|
7
11
|
const conversions = {
|
|
8
12
|
// Absolute length units
|
|
9
13
|
'px': {
|
|
@@ -129,6 +133,12 @@ const conversions = {
|
|
|
129
133
|
'dppx': 1
|
|
130
134
|
}
|
|
131
135
|
};
|
|
136
|
+
/**
|
|
137
|
+
* @param {number} value
|
|
138
|
+
* @param {string} sourceUnit
|
|
139
|
+
* @param {string} targetUnit
|
|
140
|
+
* @param {number|false} precision
|
|
141
|
+
*/
|
|
132
142
|
|
|
133
143
|
function convertUnit(value, sourceUnit, targetUnit, precision) {
|
|
134
144
|
const sourceUnitNormalized = sourceUnit.toLowerCase();
|
|
@@ -145,7 +155,7 @@ function convertUnit(value, sourceUnit, targetUnit, precision) {
|
|
|
145
155
|
const converted = conversions[targetUnitNormalized][sourceUnitNormalized] * value;
|
|
146
156
|
|
|
147
157
|
if (precision !== false) {
|
|
148
|
-
precision = Math.pow(10,
|
|
158
|
+
precision = Math.pow(10, Math.ceil(precision) || 5);
|
|
149
159
|
return Math.round(converted * precision) / precision;
|
|
150
160
|
}
|
|
151
161
|
|
package/dist/lib/reducer.js
CHANGED
|
@@ -9,8 +9,12 @@ var _convertUnit = _interopRequireDefault(require("./convertUnit"));
|
|
|
9
9
|
|
|
10
10
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
/**
|
|
13
|
+
* @param {import('../parser').CalcNode} node
|
|
14
|
+
* @return {node is import('../parser').ValueExpression}
|
|
15
|
+
*/
|
|
16
|
+
function isValueType(node) {
|
|
17
|
+
switch (node.type) {
|
|
14
18
|
case 'LengthValue':
|
|
15
19
|
case 'AngleValue':
|
|
16
20
|
case 'TimeValue':
|
|
@@ -31,34 +35,54 @@ function isValueType(type) {
|
|
|
31
35
|
|
|
32
36
|
return false;
|
|
33
37
|
}
|
|
38
|
+
/** @param {'-'|'+'} operator */
|
|
39
|
+
|
|
34
40
|
|
|
35
41
|
function flip(operator) {
|
|
36
42
|
return operator === '+' ? '-' : '+';
|
|
37
43
|
}
|
|
44
|
+
/**
|
|
45
|
+
* @param {string} operator
|
|
46
|
+
* @returns {operator is '+'|'-'}
|
|
47
|
+
*/
|
|
48
|
+
|
|
38
49
|
|
|
39
50
|
function isAddSubOperator(operator) {
|
|
40
51
|
return operator === '+' || operator === '-';
|
|
41
52
|
}
|
|
53
|
+
/**
|
|
54
|
+
* @typedef {{preOperator: '+'|'-', node: import('../parser').CalcNode}} Collectible
|
|
55
|
+
*/
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* @param {'+'|'-'} preOperator
|
|
59
|
+
* @param {import('../parser').CalcNode} node
|
|
60
|
+
* @param {Collectible[]} collected
|
|
61
|
+
* @param {number} precision
|
|
62
|
+
*/
|
|
63
|
+
|
|
42
64
|
|
|
43
65
|
function collectAddSubItems(preOperator, node, collected, precision) {
|
|
44
66
|
if (!isAddSubOperator(preOperator)) {
|
|
45
67
|
throw new Error(`invalid operator ${preOperator}`);
|
|
46
68
|
}
|
|
47
69
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
if (isValueType(type)) {
|
|
51
|
-
const itemIndex = collected.findIndex(x => x.node.type === type);
|
|
70
|
+
if (isValueType(node)) {
|
|
71
|
+
const itemIndex = collected.findIndex(x => x.node.type === node.type);
|
|
52
72
|
|
|
53
73
|
if (itemIndex >= 0) {
|
|
54
74
|
if (node.value === 0) {
|
|
55
75
|
return;
|
|
56
|
-
}
|
|
76
|
+
} // can cast because of the criterion used to find itemIndex
|
|
57
77
|
|
|
78
|
+
|
|
79
|
+
const otherValueNode =
|
|
80
|
+
/** @type import('../parser').ValueExpression*/
|
|
81
|
+
collected[itemIndex].node;
|
|
58
82
|
const {
|
|
59
83
|
left: reducedNode,
|
|
60
84
|
right: current
|
|
61
|
-
} =
|
|
85
|
+
} = convertNodesUnits(otherValueNode, node, precision);
|
|
62
86
|
|
|
63
87
|
if (collected[itemIndex].preOperator === '-') {
|
|
64
88
|
collected[itemIndex].preOperator = '+';
|
|
@@ -99,7 +123,7 @@ function collectAddSubItems(preOperator, node, collected, precision) {
|
|
|
99
123
|
});
|
|
100
124
|
}
|
|
101
125
|
}
|
|
102
|
-
} else if (type === "MathExpression") {
|
|
126
|
+
} else if (node.type === "MathExpression") {
|
|
103
127
|
if (isAddSubOperator(node.operator)) {
|
|
104
128
|
collectAddSubItems(preOperator, node.left, collected, precision);
|
|
105
129
|
const collectRightOperator = preOperator === '-' ? flip(node.operator) : node.operator;
|
|
@@ -124,22 +148,31 @@ function collectAddSubItems(preOperator, node, collected, precision) {
|
|
|
124
148
|
});
|
|
125
149
|
}
|
|
126
150
|
}
|
|
151
|
+
/**
|
|
152
|
+
* @param {import('../parser').CalcNode} node
|
|
153
|
+
* @param {number} precision
|
|
154
|
+
*/
|
|
155
|
+
|
|
127
156
|
|
|
128
157
|
function reduceAddSubExpression(node, precision) {
|
|
158
|
+
/** @type Collectible[] */
|
|
129
159
|
const collected = [];
|
|
130
160
|
collectAddSubItems('+', node, collected, precision);
|
|
131
|
-
const withoutZeroItem = collected.filter(item => !(isValueType(item.node
|
|
161
|
+
const withoutZeroItem = collected.filter(item => !(isValueType(item.node) && item.node.value === 0));
|
|
132
162
|
const firstNonZeroItem = withoutZeroItem[0]; // could be undefined
|
|
133
163
|
// prevent producing "calc(-var(--a))" or "calc()"
|
|
134
164
|
// which is invalid css
|
|
135
165
|
|
|
136
|
-
if (!firstNonZeroItem || firstNonZeroItem.preOperator === '-' && !isValueType(firstNonZeroItem.node
|
|
137
|
-
const firstZeroItem = collected.find(item => isValueType(item.node
|
|
138
|
-
|
|
166
|
+
if (!firstNonZeroItem || firstNonZeroItem.preOperator === '-' && !isValueType(firstNonZeroItem.node)) {
|
|
167
|
+
const firstZeroItem = collected.find(item => isValueType(item.node) && item.node.value === 0);
|
|
168
|
+
|
|
169
|
+
if (firstZeroItem) {
|
|
170
|
+
withoutZeroItem.unshift(firstZeroItem);
|
|
171
|
+
}
|
|
139
172
|
} // make sure the preOperator of the first item is +
|
|
140
173
|
|
|
141
174
|
|
|
142
|
-
if (withoutZeroItem[0].preOperator === '-' && isValueType(withoutZeroItem[0].node
|
|
175
|
+
if (withoutZeroItem[0].preOperator === '-' && isValueType(withoutZeroItem[0].node)) {
|
|
143
176
|
withoutZeroItem[0].node.value *= -1;
|
|
144
177
|
withoutZeroItem[0].preOperator = '+';
|
|
145
178
|
}
|
|
@@ -157,9 +190,13 @@ function reduceAddSubExpression(node, precision) {
|
|
|
157
190
|
|
|
158
191
|
return root;
|
|
159
192
|
}
|
|
193
|
+
/**
|
|
194
|
+
* @param {import('../parser').MathExpression} node
|
|
195
|
+
*/
|
|
196
|
+
|
|
160
197
|
|
|
161
198
|
function reduceDivisionExpression(node) {
|
|
162
|
-
if (!isValueType(node.right
|
|
199
|
+
if (!isValueType(node.right)) {
|
|
163
200
|
return node;
|
|
164
201
|
}
|
|
165
202
|
|
|
@@ -168,7 +205,14 @@ function reduceDivisionExpression(node) {
|
|
|
168
205
|
}
|
|
169
206
|
|
|
170
207
|
return applyNumberDivision(node.left, node.right.value);
|
|
171
|
-
}
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* apply (expr) / number
|
|
211
|
+
*
|
|
212
|
+
* @param {import('../parser').CalcNode} node
|
|
213
|
+
* @param {number} divisor
|
|
214
|
+
* @return {import('../parser').CalcNode}
|
|
215
|
+
*/
|
|
172
216
|
|
|
173
217
|
|
|
174
218
|
function applyNumberDivision(node, divisor) {
|
|
@@ -176,7 +220,7 @@ function applyNumberDivision(node, divisor) {
|
|
|
176
220
|
throw new Error('Cannot divide by zero');
|
|
177
221
|
}
|
|
178
222
|
|
|
179
|
-
if (isValueType(node
|
|
223
|
+
if (isValueType(node)) {
|
|
180
224
|
node.value /= divisor;
|
|
181
225
|
return node;
|
|
182
226
|
}
|
|
@@ -207,6 +251,10 @@ function applyNumberDivision(node, divisor) {
|
|
|
207
251
|
}
|
|
208
252
|
};
|
|
209
253
|
}
|
|
254
|
+
/**
|
|
255
|
+
* @param {import('../parser').MathExpression} node
|
|
256
|
+
*/
|
|
257
|
+
|
|
210
258
|
|
|
211
259
|
function reduceMultiplicationExpression(node) {
|
|
212
260
|
// (expr) * number
|
|
@@ -220,11 +268,17 @@ function reduceMultiplicationExpression(node) {
|
|
|
220
268
|
}
|
|
221
269
|
|
|
222
270
|
return node;
|
|
223
|
-
}
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* apply (expr) * number
|
|
274
|
+
* @param {number} multiplier
|
|
275
|
+
* @param {import('../parser').CalcNode} node
|
|
276
|
+
* @return {import('../parser').CalcNode}
|
|
277
|
+
*/
|
|
224
278
|
|
|
225
279
|
|
|
226
280
|
function applyNumberMultiplication(node, multiplier) {
|
|
227
|
-
if (isValueType(node
|
|
281
|
+
if (isValueType(node)) {
|
|
228
282
|
node.value *= multiplier;
|
|
229
283
|
return node;
|
|
230
284
|
}
|
|
@@ -255,8 +309,14 @@ function applyNumberMultiplication(node, multiplier) {
|
|
|
255
309
|
}
|
|
256
310
|
};
|
|
257
311
|
}
|
|
312
|
+
/**
|
|
313
|
+
* @param {import('../parser').ValueExpression} left
|
|
314
|
+
* @param {import('../parser').ValueExpression} right
|
|
315
|
+
* @param {number} precision
|
|
316
|
+
*/
|
|
258
317
|
|
|
259
|
-
|
|
318
|
+
|
|
319
|
+
function convertNodesUnits(left, right, precision) {
|
|
260
320
|
switch (left.type) {
|
|
261
321
|
case 'LengthValue':
|
|
262
322
|
case 'AngleValue':
|
|
@@ -284,6 +344,11 @@ function covertNodesUnits(left, right, precision) {
|
|
|
284
344
|
};
|
|
285
345
|
}
|
|
286
346
|
}
|
|
347
|
+
/**
|
|
348
|
+
* @param {import('../parser').CalcNode} node
|
|
349
|
+
* @param {number} precision
|
|
350
|
+
*/
|
|
351
|
+
|
|
287
352
|
|
|
288
353
|
function reduce(node, precision) {
|
|
289
354
|
if (node.type === "MathExpression") {
|
|
@@ -297,10 +362,10 @@ function reduce(node, precision) {
|
|
|
297
362
|
|
|
298
363
|
switch (node.operator) {
|
|
299
364
|
case "/":
|
|
300
|
-
return reduceDivisionExpression(node
|
|
365
|
+
return reduceDivisionExpression(node);
|
|
301
366
|
|
|
302
367
|
case "*":
|
|
303
|
-
return reduceMultiplicationExpression(node
|
|
368
|
+
return reduceMultiplicationExpression(node);
|
|
304
369
|
}
|
|
305
370
|
|
|
306
371
|
return node;
|
package/dist/lib/stringifier.js
CHANGED
|
@@ -10,6 +10,10 @@ const order = {
|
|
|
10
10
|
"+": 1,
|
|
11
11
|
"-": 1
|
|
12
12
|
};
|
|
13
|
+
/**
|
|
14
|
+
* @param {number} value
|
|
15
|
+
* @param {number | false} prec
|
|
16
|
+
*/
|
|
13
17
|
|
|
14
18
|
function round(value, prec) {
|
|
15
19
|
if (prec !== false) {
|
|
@@ -19,6 +23,11 @@ function round(value, prec) {
|
|
|
19
23
|
|
|
20
24
|
return value;
|
|
21
25
|
}
|
|
26
|
+
/**
|
|
27
|
+
* @param {number | false} prec
|
|
28
|
+
* @param {import('../parser').CalcNode} node
|
|
29
|
+
*/
|
|
30
|
+
|
|
22
31
|
|
|
23
32
|
function stringify(node, prec) {
|
|
24
33
|
switch (node.type) {
|
|
@@ -49,15 +58,26 @@ function stringify(node, prec) {
|
|
|
49
58
|
}
|
|
50
59
|
|
|
51
60
|
case 'Number':
|
|
52
|
-
return round(node.value, prec);
|
|
61
|
+
return round(node.value, prec).toString();
|
|
53
62
|
|
|
54
63
|
case 'Function':
|
|
55
|
-
return node.value;
|
|
64
|
+
return node.value.toString();
|
|
56
65
|
|
|
57
66
|
default:
|
|
58
67
|
return round(node.value, prec) + node.unit;
|
|
59
68
|
}
|
|
60
69
|
}
|
|
70
|
+
/**
|
|
71
|
+
* @param {string} calc
|
|
72
|
+
* @param {import('../parser').CalcNode} node
|
|
73
|
+
* @param {string} originalValue
|
|
74
|
+
* @param {{precision: number | false, warnWhenCannotResolve: boolean}} options
|
|
75
|
+
* @param {import("postcss").Result} result
|
|
76
|
+
* @param {import("postcss").ChildNode} item
|
|
77
|
+
*
|
|
78
|
+
* @returns {string}
|
|
79
|
+
*/
|
|
80
|
+
|
|
61
81
|
|
|
62
82
|
function _default(calc, node, originalValue, options, result, item) {
|
|
63
83
|
let str = stringify(node, options.precision);
|
package/dist/lib/transform.js
CHANGED
|
@@ -19,12 +19,18 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
|
|
|
19
19
|
|
|
20
20
|
// eslint-disable-next-line import/no-unresolved
|
|
21
21
|
const MATCH_CALC = /((?:-(moz|webkit)-)?calc)/i;
|
|
22
|
+
/**
|
|
23
|
+
* @param {string} value
|
|
24
|
+
* @param {{precision: number, warnWhenCannotResolve: boolean}} options
|
|
25
|
+
* @param {import("postcss").Result} result
|
|
26
|
+
* @param {import("postcss").ChildNode} item
|
|
27
|
+
*/
|
|
22
28
|
|
|
23
29
|
function transformValue(value, options, result, item) {
|
|
24
30
|
return (0, _postcssValueParser.default)(value).walk(node => {
|
|
25
31
|
// skip anything which isn't a calc() function
|
|
26
32
|
if (node.type !== "function" || !MATCH_CALC.test(node.value)) {
|
|
27
|
-
return
|
|
33
|
+
return;
|
|
28
34
|
} // stringify calc expression and produce an AST
|
|
29
35
|
|
|
30
36
|
|
|
@@ -36,11 +42,19 @@ function transformValue(value, options, result, item) {
|
|
|
36
42
|
|
|
37
43
|
const reducedAst = (0, _reducer.default)(ast, options.precision); // stringify AST and write it back
|
|
38
44
|
|
|
45
|
+
/** @type {valueParser.Node} */
|
|
39
46
|
node.type = "word";
|
|
40
47
|
node.value = (0, _stringifier.default)(node.value, reducedAst, value, options, result, item);
|
|
41
48
|
return false;
|
|
42
49
|
}).toString();
|
|
43
50
|
}
|
|
51
|
+
/**
|
|
52
|
+
* @param {import("postcss-selector-parser").Selectors} value
|
|
53
|
+
* @param {{precision: number, warnWhenCannotResolve: boolean}} options
|
|
54
|
+
* @param {import("postcss").Result} result
|
|
55
|
+
* @param {import("postcss").ChildNode} item
|
|
56
|
+
*/
|
|
57
|
+
|
|
44
58
|
|
|
45
59
|
function transformSelector(value, options, result, item) {
|
|
46
60
|
return (0, _postcssSelectorParser.default)(selectors => {
|
|
@@ -61,6 +75,13 @@ function transformSelector(value, options, result, item) {
|
|
|
61
75
|
});
|
|
62
76
|
}).processSync(value);
|
|
63
77
|
}
|
|
78
|
+
/**
|
|
79
|
+
* @param {any} node
|
|
80
|
+
* @param {{precision: number, preserve: boolean, warnWhenCannotResolve: boolean}} options
|
|
81
|
+
* @param {'value'|'params'|'selector'} property
|
|
82
|
+
* @param {import("postcss").Result} result
|
|
83
|
+
*/
|
|
84
|
+
|
|
64
85
|
|
|
65
86
|
var _default = (node, property, options, result) => {
|
|
66
87
|
let value = node[property];
|
|
@@ -68,9 +89,16 @@ var _default = (node, property, options, result) => {
|
|
|
68
89
|
try {
|
|
69
90
|
value = property === "selector" ? transformSelector(node[property], options, result, node) : transformValue(node[property], options, result, node);
|
|
70
91
|
} catch (error) {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
92
|
+
if (error instanceof Error) {
|
|
93
|
+
result.warn(error.message, {
|
|
94
|
+
node
|
|
95
|
+
});
|
|
96
|
+
} else {
|
|
97
|
+
result.warn('Error', {
|
|
98
|
+
node
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
74
102
|
return;
|
|
75
103
|
} // if the preserve option is enabled and the value has changed, write the
|
|
76
104
|
// transformed value into a cloned node which is inserted before the current
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "postcss-calc",
|
|
3
|
-
"version": "8.
|
|
3
|
+
"version": "8.2.0",
|
|
4
4
|
"description": "PostCSS plugin to reduce calc()",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"css",
|
|
@@ -10,8 +10,10 @@
|
|
|
10
10
|
"calc"
|
|
11
11
|
],
|
|
12
12
|
"main": "dist/index.js",
|
|
13
|
+
"types": "types/index.d.ts",
|
|
13
14
|
"files": [
|
|
14
15
|
"dist",
|
|
16
|
+
"types",
|
|
15
17
|
"LICENSE"
|
|
16
18
|
],
|
|
17
19
|
"author": "Andy Jansson",
|
|
@@ -27,17 +29,18 @@
|
|
|
27
29
|
}
|
|
28
30
|
},
|
|
29
31
|
"devDependencies": {
|
|
30
|
-
"@ava/babel": "^2.0.0",
|
|
31
32
|
"@babel/cli": "^7.16.0",
|
|
32
33
|
"@babel/core": "^7.16.5",
|
|
33
34
|
"@babel/plugin-transform-modules-commonjs": "^7.16.5",
|
|
34
|
-
"
|
|
35
|
+
"@babel/register": "^7.16.7",
|
|
35
36
|
"babel-plugin-add-module-exports": "^1.0.0",
|
|
36
37
|
"eslint": "^8.5.0",
|
|
37
38
|
"eslint-plugin-import": "^2.25.3",
|
|
38
39
|
"jison-gho": "^0.6.1-216",
|
|
39
40
|
"postcss": "^8.2.2",
|
|
40
|
-
"rimraf": "^3.0.2"
|
|
41
|
+
"rimraf": "^3.0.2",
|
|
42
|
+
"typescript": "^4.5.4",
|
|
43
|
+
"uvu": "^0.5.2"
|
|
41
44
|
},
|
|
42
45
|
"dependencies": {
|
|
43
46
|
"postcss-selector-parser": "^6.0.2",
|
|
@@ -46,14 +49,11 @@
|
|
|
46
49
|
"peerDependencies": {
|
|
47
50
|
"postcss": "^8.2.2"
|
|
48
51
|
},
|
|
49
|
-
"ava": {
|
|
50
|
-
"babel": true
|
|
51
|
-
},
|
|
52
52
|
"scripts": {
|
|
53
53
|
"build": "rimraf dist && babel src --out-dir dist --ignore src/__tests__/**/*.js && jison src/parser.jison -o dist/parser.js",
|
|
54
|
-
"lint": "eslint src",
|
|
54
|
+
"lint": "eslint src && tsc",
|
|
55
55
|
"pretest": "pnpm run build",
|
|
56
|
-
"test": "
|
|
56
|
+
"test": "uvu -r @babel/register src/__tests__"
|
|
57
57
|
},
|
|
58
58
|
"readme": "# PostCSS Calc [<img src=\"https://postcss.github.io/postcss/logo.svg\" alt=\"PostCSS\" width=\"90\" height=\"90\" align=\"right\">][PostCSS]\n\n[![NPM Version][npm-img]][npm-url]\n[![Build Status][cli-img]][cli-url]\n[![Support Chat][git-img]][git-url]\n\n[PostCSS Calc] lets you reduce `calc()` references whenever it's possible. This\ncan be particularly useful with the [PostCSS Custom Properties] plugin.\n\nWhen multiple units are mixed together in the same expression, the `calc()`\nstatement is left as is, to fallback to the [W3C calc() implementation].\n\n## Installation\n\n```bash\nnpm install postcss-calc\n```\n\n## Usage\n\n```js\n// dependencies\nvar fs = require(\"fs\")\nvar postcss = require(\"postcss\")\nvar calc = require(\"postcss-calc\")\n\n// css to be processed\nvar css = fs.readFileSync(\"input.css\", \"utf8\")\n\n// process css\nvar output = postcss()\n .use(calc())\n .process(css)\n .css\n```\n\n**Example** (with [PostCSS Custom Properties] enabled as well):\n\n```js\n// dependencies\nvar fs = require(\"fs\")\nvar postcss = require(\"postcss\")\nvar customProperties = require(\"postcss-custom-properties\")\nvar calc = require(\"postcss-calc\")\n\n// css to be processed\nvar css = fs.readFileSync(\"input.css\", \"utf8\")\n\n// process css\nvar output = postcss()\n .use(customProperties())\n .use(calc())\n .process(css)\n .css\n```\n\nUsing this `input.css`:\n\n```css\n:root {\n --main-font-size: 16px;\n}\n\nbody {\n font-size: var(--main-font-size);\n}\n\nh1 {\n font-size: calc(var(--main-font-size) * 2);\n height: calc(100px - 2em);\n margin-bottom: calc(\n var(--main-font-size)\n * 1.5\n )\n}\n```\n\nyou will get:\n\n```css\nbody {\n font-size: 16px\n}\n\nh1 {\n font-size: 32px;\n height: calc(100px - 2em);\n margin-bottom: 24px\n}\n```\n\nCheckout [tests] for more examples.\n\n### Options\n\n#### `precision` (default: `5`)\n\nAllow you to define the precision for decimal numbers.\n\n```js\nvar out = postcss()\n .use(calc({precision: 10}))\n .process(css)\n .css\n```\n\n#### `preserve` (default: `false`)\n\nAllow you to preserve calc() usage in output so browsers will handle decimal\nprecision themselves.\n\n```js\nvar out = postcss()\n .use(calc({preserve: true}))\n .process(css)\n .css\n```\n\n#### `warnWhenCannotResolve` (default: `false`)\n\nAdds warnings when calc() are not reduced to a single value.\n\n```js\nvar out = postcss()\n .use(calc({warnWhenCannotResolve: true}))\n .process(css)\n .css\n```\n\n#### `mediaQueries` (default: `false`)\n\nAllows calc() usage as part of media query declarations.\n\n```js\nvar out = postcss()\n .use(calc({mediaQueries: true}))\n .process(css)\n .css\n```\n\n#### `selectors` (default: `false`)\n\nAllows calc() usage as part of selectors.\n\n```js\nvar out = postcss()\n .use(calc({selectors: true}))\n .process(css)\n .css\n```\n\nExample:\n\n```css\ndiv[data-size=\"calc(3*3)\"] {\n width: 100px;\n}\n```\n\n---\n\n## Contributing\n\nWork on a branch, install dev-dependencies, respect coding style & run tests\nbefore submitting a bug fix or a feature.\n\n```bash\ngit clone git@github.com:postcss/postcss-calc.git\ngit checkout -b patch-1\nnpm install\nnpm test\n```\n\n## [Changelog](CHANGELOG.md)\n\n## [License](LICENSE)\n\n[cli-img]: https://img.shields.io/travis/postcss/postcss-calc/master.svg\n[cli-url]: https://travis-ci.org/postcss/postcss-calc\n[git-img]: https://img.shields.io/badge/support-chat-blue.svg\n[git-url]: https://gitter.im/postcss/postcss\n[npm-img]: https://img.shields.io/npm/v/postcss-calc.svg\n[npm-url]: https://www.npmjs.com/package/postcss-calc\n\n[PostCSS]: https://github.com/postcss\n[PostCSS Calc]: https://github.com/postcss/postcss-calc\n[PostCSS Custom Properties]: https://github.com/postcss/postcss-custom-properties\n[tests]: src/__tests__/index.js\n[W3C calc() implementation]: https://www.w3.org/TR/css3-values/#calc-notation\n"
|
|
59
59
|
}
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export default pluginCreator;
|
|
2
|
+
export type PostCssCalcOptions = {
|
|
3
|
+
precision?: number | false;
|
|
4
|
+
preserve?: boolean;
|
|
5
|
+
warnWhenCannotResolve?: boolean;
|
|
6
|
+
mediaQueries?: boolean;
|
|
7
|
+
selectors?: boolean;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* @typedef {{precision?: number | false,
|
|
11
|
+
* preserve?: boolean,
|
|
12
|
+
* warnWhenCannotResolve?: boolean,
|
|
13
|
+
* mediaQueries?: boolean,
|
|
14
|
+
* selectors?: boolean}} PostCssCalcOptions
|
|
15
|
+
*
|
|
16
|
+
* @param {PostCssCalcOptions} opts
|
|
17
|
+
*/
|
|
18
|
+
declare function pluginCreator(opts: PostCssCalcOptions): {
|
|
19
|
+
postcssPlugin: string;
|
|
20
|
+
/**
|
|
21
|
+
* @param {import('postcss').Root} css
|
|
22
|
+
* @param {{result: import('postcss').Result}} helpers
|
|
23
|
+
*/
|
|
24
|
+
OnceExit(css: import('postcss').Root, { result }: {
|
|
25
|
+
result: import('postcss').Result;
|
|
26
|
+
}): void;
|
|
27
|
+
};
|
|
28
|
+
declare namespace pluginCreator {
|
|
29
|
+
const postcss: boolean;
|
|
30
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export default convertUnit;
|
|
2
|
+
/**
|
|
3
|
+
* @param {number} value
|
|
4
|
+
* @param {string} sourceUnit
|
|
5
|
+
* @param {string} targetUnit
|
|
6
|
+
* @param {number|false} precision
|
|
7
|
+
*/
|
|
8
|
+
declare function convertUnit(value: number, sourceUnit: string, targetUnit: string, precision: number | false): number;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export default reduce;
|
|
2
|
+
export type Collectible = {
|
|
3
|
+
preOperator: '+' | '-';
|
|
4
|
+
node: import('../parser').CalcNode;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* @param {import('../parser').CalcNode} node
|
|
8
|
+
* @param {number} precision
|
|
9
|
+
*/
|
|
10
|
+
declare function reduce(node: import('../parser').CalcNode, precision: number): import("../parser").MathExpression | import("../parser").DimensionExpression | import("../parser").NumberExpression | import("../parser").FunctionExpression;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param {string} calc
|
|
3
|
+
* @param {import('../parser').CalcNode} node
|
|
4
|
+
* @param {string} originalValue
|
|
5
|
+
* @param {{precision: number | false, warnWhenCannotResolve: boolean}} options
|
|
6
|
+
* @param {import("postcss").Result} result
|
|
7
|
+
* @param {import("postcss").ChildNode} item
|
|
8
|
+
*
|
|
9
|
+
* @returns {string}
|
|
10
|
+
*/
|
|
11
|
+
export default function _default(calc: string, node: import('../parser').CalcNode, originalValue: string, options: {
|
|
12
|
+
precision: number | false;
|
|
13
|
+
warnWhenCannotResolve: boolean;
|
|
14
|
+
}, result: import("postcss").Result, item: import("postcss").ChildNode): string;
|