postcss-calc 6.0.1 → 10.1.1
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/LICENSE +0 -0
- package/README.md +36 -54
- package/package.json +30 -34
- package/src/index.js +51 -0
- package/src/lib/convertUnit.js +160 -0
- package/src/lib/reducer.js +396 -0
- package/src/lib/stringifier.js +106 -0
- package/src/lib/transform.js +109 -0
- package/src/parser.d.ts +89 -0
- package/src/parser.js +4222 -0
- package/types/index.d.ts +25 -0
- package/types/lib/convertUnit.d.ts +8 -0
- package/types/lib/reducer.d.ts +14 -0
- package/types/lib/stringifier.d.ts +5 -0
- package/types/lib/transform.d.ts +6 -0
- package/CHANGELOG.md +0 -72
- package/dist/index.js +0 -34
- package/dist/lib/transform.js +0 -67
package/LICENSE
CHANGED
|
File without changes
|
package/README.md
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
#
|
|
1
|
+
# PostCSS Calc [<img src="https://postcss.github.io/postcss/logo.svg" alt="PostCSS" width="90" height="90" align="right">][PostCSS]
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[![NPM Version][npm-img]][npm-url]
|
|
4
|
+
[![Support Chat][git-img]][git-url]
|
|
4
5
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
**Note:** When multiple units are mixed together in the same expression, the `calc()` statement is left as is, to fallback to the [w3c calc() feature](http://www.w3.org/TR/css3-values/#calc).
|
|
6
|
+
[PostCSS Calc] lets you reduce `calc()` references whenever it's possible.
|
|
7
|
+
When multiple units are mixed together in the same expression, the `calc()`
|
|
8
|
+
statement is left as is, to fallback to the [W3C calc() implementation].
|
|
9
9
|
|
|
10
10
|
## Installation
|
|
11
11
|
|
|
12
|
-
```
|
|
13
|
-
|
|
12
|
+
```bash
|
|
13
|
+
npm install postcss-calc
|
|
14
14
|
```
|
|
15
15
|
|
|
16
16
|
## Usage
|
|
@@ -31,62 +31,28 @@ var output = postcss()
|
|
|
31
31
|
.css
|
|
32
32
|
```
|
|
33
33
|
|
|
34
|
-
**Example** (with [postcss-custom-properties](https://github.com/postcss/postcss-custom-properties) enabled as well):
|
|
35
|
-
|
|
36
|
-
```js
|
|
37
|
-
// dependencies
|
|
38
|
-
var fs = require("fs")
|
|
39
|
-
var postcss = require("postcss")
|
|
40
|
-
var customProperties = require("postcss-custom-properties")
|
|
41
|
-
var calc = require("postcss-calc")
|
|
42
|
-
|
|
43
|
-
// css to be processed
|
|
44
|
-
var css = fs.readFileSync("input.css", "utf8")
|
|
45
|
-
|
|
46
|
-
// process css
|
|
47
|
-
var output = postcss()
|
|
48
|
-
.use(customProperties())
|
|
49
|
-
.use(calc())
|
|
50
|
-
.process(css)
|
|
51
|
-
.css
|
|
52
|
-
```
|
|
53
|
-
|
|
54
34
|
Using this `input.css`:
|
|
55
35
|
|
|
56
36
|
```css
|
|
57
|
-
:root {
|
|
58
|
-
--main-font-size: 16px;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
body {
|
|
62
|
-
font-size: var(--main-font-size);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
37
|
h1 {
|
|
66
|
-
font-size: calc(
|
|
38
|
+
font-size: calc(16px * 2);
|
|
67
39
|
height: calc(100px - 2em);
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
* 1.5
|
|
71
|
-
)
|
|
40
|
+
width: calc(2*var(--base-width));
|
|
41
|
+
margin-bottom: calc(16px * 1.5);
|
|
72
42
|
}
|
|
73
43
|
```
|
|
74
44
|
|
|
75
45
|
you will get:
|
|
76
46
|
|
|
77
47
|
```css
|
|
78
|
-
body {
|
|
79
|
-
font-size: 16px
|
|
80
|
-
}
|
|
81
|
-
|
|
82
48
|
h1 {
|
|
83
49
|
font-size: 32px;
|
|
84
50
|
height: calc(100px - 2em);
|
|
51
|
+
width: calc(2*var(--base-width));
|
|
85
52
|
margin-bottom: 24px
|
|
86
53
|
}
|
|
87
54
|
```
|
|
88
|
-
|
|
89
|
-
Checkout [tests](test) for more examples.
|
|
55
|
+
Checkout [tests] for more examples.
|
|
90
56
|
|
|
91
57
|
### Options
|
|
92
58
|
|
|
@@ -103,7 +69,8 @@ var out = postcss()
|
|
|
103
69
|
|
|
104
70
|
#### `preserve` (default: `false`)
|
|
105
71
|
|
|
106
|
-
Allow you to preserve calc() usage in output so browsers will handle decimal
|
|
72
|
+
Allow you to preserve calc() usage in output so browsers will handle decimal
|
|
73
|
+
precision themselves.
|
|
107
74
|
|
|
108
75
|
```js
|
|
109
76
|
var out = postcss()
|
|
@@ -155,17 +122,32 @@ div[data-size="calc(3*3)"] {
|
|
|
155
122
|
|
|
156
123
|
---
|
|
157
124
|
|
|
125
|
+
## Related PostCSS plugins
|
|
126
|
+
To replace the value of CSS custom properties at build time, try [PostCSS Custom Properties].
|
|
127
|
+
|
|
158
128
|
## Contributing
|
|
159
129
|
|
|
160
|
-
Work on a branch, install dev-dependencies, respect coding style & run tests
|
|
130
|
+
Work on a branch, install dev-dependencies, respect coding style & run tests
|
|
131
|
+
before submitting a bug fix or a feature.
|
|
161
132
|
|
|
162
|
-
```
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
133
|
+
```bash
|
|
134
|
+
git clone git@github.com:postcss/postcss-calc.git
|
|
135
|
+
git checkout -b patch-1
|
|
136
|
+
npm install
|
|
137
|
+
npm test
|
|
167
138
|
```
|
|
168
139
|
|
|
169
140
|
## [Changelog](CHANGELOG.md)
|
|
170
141
|
|
|
171
142
|
## [License](LICENSE)
|
|
143
|
+
|
|
144
|
+
[git-img]: https://img.shields.io/badge/support-chat-blue.svg
|
|
145
|
+
[git-url]: https://gitter.im/postcss/postcss
|
|
146
|
+
[npm-img]: https://img.shields.io/npm/v/postcss-calc.svg
|
|
147
|
+
[npm-url]: https://www.npmjs.com/package/postcss-calc
|
|
148
|
+
|
|
149
|
+
[PostCSS]: https://github.com/postcss
|
|
150
|
+
[PostCSS Calc]: https://github.com/postcss/postcss-calc
|
|
151
|
+
[PostCSS Custom Properties]: https://github.com/postcss/postcss-custom-properties
|
|
152
|
+
[tests]: src/__tests__/index.js
|
|
153
|
+
[W3C calc() implementation]: https://www.w3.org/TR/css3-values/#calc-notation
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "postcss-calc",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "10.1.1",
|
|
4
4
|
"description": "PostCSS plugin to reduce calc()",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"css",
|
|
@@ -9,46 +9,42 @@
|
|
|
9
9
|
"calculation",
|
|
10
10
|
"calc"
|
|
11
11
|
],
|
|
12
|
-
"
|
|
12
|
+
"homepage": "https://github.com/postcss/postcss-calc",
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "https://github.com/postcss/postcss-calc.git"
|
|
16
|
+
},
|
|
17
|
+
"main": "src/index.js",
|
|
18
|
+
"types": "types/index.d.ts",
|
|
13
19
|
"files": [
|
|
14
|
-
"
|
|
20
|
+
"src",
|
|
21
|
+
"types",
|
|
15
22
|
"LICENSE"
|
|
16
23
|
],
|
|
17
|
-
"
|
|
18
|
-
"prepublish": "npm run build && del-cli dist/__tests__",
|
|
19
|
-
"build": "del-cli dist && cross-env BABEL_ENV=publish babel src --out-dir dist",
|
|
20
|
-
"pretest": "eslint src && npm run build",
|
|
21
|
-
"test": "ava src/__tests__/"
|
|
22
|
-
},
|
|
23
|
-
"author": "Maxime Thirouin",
|
|
24
|
+
"author": "Andy Jansson",
|
|
24
25
|
"license": "MIT",
|
|
25
|
-
"
|
|
26
|
-
|
|
27
|
-
"parser": "babel-eslint",
|
|
28
|
-
"extends": "eslint-config-i-am-meticulous"
|
|
26
|
+
"engines": {
|
|
27
|
+
"node": "^18.12 || ^20.9 || >=22.0"
|
|
29
28
|
},
|
|
30
29
|
"devDependencies": {
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
"
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
"cross-env": "^4.0.0",
|
|
39
|
-
"del-cli": "^0.2.1",
|
|
40
|
-
"eslint": "^3.12.2",
|
|
41
|
-
"eslint-config-i-am-meticulous": "^6.0.1",
|
|
42
|
-
"eslint-plugin-babel": "^4.0.0",
|
|
43
|
-
"eslint-plugin-import": "^2.2.0"
|
|
30
|
+
"@types/node": "^22.10.10",
|
|
31
|
+
"eslint": "^9.19.0",
|
|
32
|
+
"eslint-config-prettier": "^10.0.1",
|
|
33
|
+
"jison-gho": "0.6.1-216",
|
|
34
|
+
"postcss": "^8.5.1",
|
|
35
|
+
"prettier": "^3.4.2",
|
|
36
|
+
"typescript": "~5.7.3"
|
|
44
37
|
},
|
|
45
38
|
"dependencies": {
|
|
46
|
-
"
|
|
47
|
-
"postcss": "^
|
|
48
|
-
"postcss-selector-parser": "^2.2.2",
|
|
49
|
-
"reduce-css-calc": "^2.0.0"
|
|
39
|
+
"postcss-selector-parser": "^7.0.0",
|
|
40
|
+
"postcss-value-parser": "^4.2.0"
|
|
50
41
|
},
|
|
51
|
-
"
|
|
52
|
-
"
|
|
42
|
+
"peerDependencies": {
|
|
43
|
+
"postcss": "^8.4.38"
|
|
44
|
+
},
|
|
45
|
+
"scripts": {
|
|
46
|
+
"build": "jison ./parser.jison -o src/parser.js",
|
|
47
|
+
"lint": "eslint . && tsc",
|
|
48
|
+
"test": "node --test"
|
|
53
49
|
}
|
|
54
|
-
}
|
|
50
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const transform = require('./lib/transform.js');
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @typedef {{precision?: number | false,
|
|
6
|
+
* preserve?: boolean,
|
|
7
|
+
* warnWhenCannotResolve?: boolean,
|
|
8
|
+
* mediaQueries?: boolean,
|
|
9
|
+
* selectors?: boolean}} PostCssCalcOptions
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* @type {import('postcss').PluginCreator<PostCssCalcOptions>}
|
|
13
|
+
* @param {PostCssCalcOptions} opts
|
|
14
|
+
* @return {import('postcss').Plugin}
|
|
15
|
+
*/
|
|
16
|
+
function pluginCreator(opts) {
|
|
17
|
+
const options = Object.assign(
|
|
18
|
+
{
|
|
19
|
+
precision: 5,
|
|
20
|
+
preserve: false,
|
|
21
|
+
warnWhenCannotResolve: false,
|
|
22
|
+
mediaQueries: false,
|
|
23
|
+
selectors: false,
|
|
24
|
+
},
|
|
25
|
+
opts
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
return {
|
|
29
|
+
postcssPlugin: 'postcss-calc',
|
|
30
|
+
OnceExit(css, { result }) {
|
|
31
|
+
css.walk((node) => {
|
|
32
|
+
const { type } = node;
|
|
33
|
+
if (type === 'decl') {
|
|
34
|
+
transform(node, 'value', options, result);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (type === 'atrule' && options.mediaQueries) {
|
|
38
|
+
transform(node, 'params', options, result);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (type === 'rule' && options.selectors) {
|
|
42
|
+
transform(node, 'selector', options, result);
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
pluginCreator.postcss = true;
|
|
50
|
+
|
|
51
|
+
module.exports = pluginCreator;
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* @type {{[key:string]: {[key:string]: number}}}
|
|
4
|
+
*/
|
|
5
|
+
const conversions = {
|
|
6
|
+
// Absolute length units
|
|
7
|
+
px: {
|
|
8
|
+
px: 1,
|
|
9
|
+
cm: 96 / 2.54,
|
|
10
|
+
mm: 96 / 25.4,
|
|
11
|
+
q: 96 / 101.6,
|
|
12
|
+
in: 96,
|
|
13
|
+
pt: 96 / 72,
|
|
14
|
+
pc: 16,
|
|
15
|
+
},
|
|
16
|
+
cm: {
|
|
17
|
+
px: 2.54 / 96,
|
|
18
|
+
cm: 1,
|
|
19
|
+
mm: 0.1,
|
|
20
|
+
q: 0.025,
|
|
21
|
+
in: 2.54,
|
|
22
|
+
pt: 2.54 / 72,
|
|
23
|
+
pc: 2.54 / 6,
|
|
24
|
+
},
|
|
25
|
+
mm: {
|
|
26
|
+
px: 25.4 / 96,
|
|
27
|
+
cm: 10,
|
|
28
|
+
mm: 1,
|
|
29
|
+
q: 0.25,
|
|
30
|
+
in: 25.4,
|
|
31
|
+
pt: 25.4 / 72,
|
|
32
|
+
pc: 25.4 / 6,
|
|
33
|
+
},
|
|
34
|
+
q: {
|
|
35
|
+
px: 101.6 / 96,
|
|
36
|
+
cm: 40,
|
|
37
|
+
mm: 4,
|
|
38
|
+
q: 1,
|
|
39
|
+
in: 101.6,
|
|
40
|
+
pt: 101.6 / 72,
|
|
41
|
+
pc: 101.6 / 6,
|
|
42
|
+
},
|
|
43
|
+
in: {
|
|
44
|
+
px: 1 / 96,
|
|
45
|
+
cm: 1 / 2.54,
|
|
46
|
+
mm: 1 / 25.4,
|
|
47
|
+
q: 1 / 101.6,
|
|
48
|
+
in: 1,
|
|
49
|
+
pt: 1 / 72,
|
|
50
|
+
pc: 1 / 6,
|
|
51
|
+
},
|
|
52
|
+
pt: {
|
|
53
|
+
px: 0.75,
|
|
54
|
+
cm: 72 / 2.54,
|
|
55
|
+
mm: 72 / 25.4,
|
|
56
|
+
q: 72 / 101.6,
|
|
57
|
+
in: 72,
|
|
58
|
+
pt: 1,
|
|
59
|
+
pc: 12,
|
|
60
|
+
},
|
|
61
|
+
pc: {
|
|
62
|
+
px: 0.0625,
|
|
63
|
+
cm: 6 / 2.54,
|
|
64
|
+
mm: 6 / 25.4,
|
|
65
|
+
q: 6 / 101.6,
|
|
66
|
+
in: 6,
|
|
67
|
+
pt: 6 / 72,
|
|
68
|
+
pc: 1,
|
|
69
|
+
},
|
|
70
|
+
// Angle units
|
|
71
|
+
deg: {
|
|
72
|
+
deg: 1,
|
|
73
|
+
grad: 0.9,
|
|
74
|
+
rad: 180 / Math.PI,
|
|
75
|
+
turn: 360,
|
|
76
|
+
},
|
|
77
|
+
grad: {
|
|
78
|
+
deg: 400 / 360,
|
|
79
|
+
grad: 1,
|
|
80
|
+
rad: 200 / Math.PI,
|
|
81
|
+
turn: 400,
|
|
82
|
+
},
|
|
83
|
+
rad: {
|
|
84
|
+
deg: Math.PI / 180,
|
|
85
|
+
grad: Math.PI / 200,
|
|
86
|
+
rad: 1,
|
|
87
|
+
turn: Math.PI * 2,
|
|
88
|
+
},
|
|
89
|
+
turn: {
|
|
90
|
+
deg: 1 / 360,
|
|
91
|
+
grad: 0.0025,
|
|
92
|
+
rad: 0.5 / Math.PI,
|
|
93
|
+
turn: 1,
|
|
94
|
+
},
|
|
95
|
+
// Duration units
|
|
96
|
+
s: {
|
|
97
|
+
s: 1,
|
|
98
|
+
ms: 0.001,
|
|
99
|
+
},
|
|
100
|
+
ms: {
|
|
101
|
+
s: 1000,
|
|
102
|
+
ms: 1,
|
|
103
|
+
},
|
|
104
|
+
// Frequency units
|
|
105
|
+
hz: {
|
|
106
|
+
hz: 1,
|
|
107
|
+
khz: 1000,
|
|
108
|
+
},
|
|
109
|
+
khz: {
|
|
110
|
+
hz: 0.001,
|
|
111
|
+
khz: 1,
|
|
112
|
+
},
|
|
113
|
+
// Resolution units
|
|
114
|
+
dpi: {
|
|
115
|
+
dpi: 1,
|
|
116
|
+
dpcm: 1 / 2.54,
|
|
117
|
+
dppx: 1 / 96,
|
|
118
|
+
},
|
|
119
|
+
dpcm: {
|
|
120
|
+
dpi: 2.54,
|
|
121
|
+
dpcm: 1,
|
|
122
|
+
dppx: 2.54 / 96,
|
|
123
|
+
},
|
|
124
|
+
dppx: {
|
|
125
|
+
dpi: 96,
|
|
126
|
+
dpcm: 96 / 2.54,
|
|
127
|
+
dppx: 1,
|
|
128
|
+
},
|
|
129
|
+
};
|
|
130
|
+
/**
|
|
131
|
+
* @param {number} value
|
|
132
|
+
* @param {string} sourceUnit
|
|
133
|
+
* @param {string} targetUnit
|
|
134
|
+
* @param {number|false} precision
|
|
135
|
+
*/
|
|
136
|
+
function convertUnit(value, sourceUnit, targetUnit, precision) {
|
|
137
|
+
const sourceUnitNormalized = sourceUnit.toLowerCase();
|
|
138
|
+
const targetUnitNormalized = targetUnit.toLowerCase();
|
|
139
|
+
|
|
140
|
+
if (!conversions[targetUnitNormalized]) {
|
|
141
|
+
throw new Error('Cannot convert to ' + targetUnit);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (!conversions[targetUnitNormalized][sourceUnitNormalized]) {
|
|
145
|
+
throw new Error('Cannot convert from ' + sourceUnit + ' to ' + targetUnit);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const converted =
|
|
149
|
+
conversions[targetUnitNormalized][sourceUnitNormalized] * value;
|
|
150
|
+
|
|
151
|
+
if (precision !== false) {
|
|
152
|
+
precision = Math.pow(10, Math.ceil(precision) || 5);
|
|
153
|
+
|
|
154
|
+
return Math.round(converted * precision) / precision;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
return converted;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
module.exports = convertUnit;
|