postcss-calc 8.2.3 → 9.0.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/package.json +35 -25
- package/src/index.js +51 -0
- package/src/lib/convertUnit.js +160 -0
- package/{dist → src}/lib/reducer.js +108 -142
- package/{dist → src}/lib/stringifier.js +36 -51
- package/src/lib/transform.js +109 -0
- package/src/parser.d.ts +51 -0
- package/types/index.d.ts +14 -13
- package/types/lib/convertUnit.d.ts +1 -1
- package/types/lib/reducer.d.ts +8 -5
- package/types/lib/stringifier.d.ts +2 -11
- package/types/lib/transform.d.ts +2 -2
- package/dist/index.js +0 -64
- package/dist/lib/convertUnit.js +0 -167
- package/dist/lib/transform.js +0 -119
- /package/{dist → src}/parser.js +0 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "postcss-calc",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "9.0.0",
|
|
4
4
|
"description": "PostCSS plugin to reduce calc()",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"css",
|
|
@@ -9,51 +9,61 @@
|
|
|
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",
|
|
13
18
|
"types": "types/index.d.ts",
|
|
14
19
|
"files": [
|
|
15
|
-
"
|
|
20
|
+
"src",
|
|
16
21
|
"types",
|
|
17
22
|
"LICENSE"
|
|
18
23
|
],
|
|
24
|
+
"scripts": {
|
|
25
|
+
"prepare": "pnpm run build && tsc",
|
|
26
|
+
"build": "jison ./parser.jison -o src/parser.js",
|
|
27
|
+
"lint": "eslint . && tsc",
|
|
28
|
+
"test": "uvu test"
|
|
29
|
+
},
|
|
19
30
|
"author": "Andy Jansson",
|
|
20
31
|
"license": "MIT",
|
|
21
32
|
"repository": "https://github.com/postcss/postcss-calc.git",
|
|
22
33
|
"eslintConfig": {
|
|
23
34
|
"extends": [
|
|
24
35
|
"eslint:recommended",
|
|
25
|
-
"
|
|
36
|
+
"prettier"
|
|
37
|
+
],
|
|
38
|
+
"env": {
|
|
39
|
+
"node": true,
|
|
40
|
+
"es2017": true
|
|
41
|
+
},
|
|
42
|
+
"ignorePatterns": [
|
|
43
|
+
"src/parser.js"
|
|
26
44
|
],
|
|
27
45
|
"rules": {
|
|
28
46
|
"curly": "error"
|
|
29
47
|
}
|
|
30
48
|
},
|
|
49
|
+
"engines": {
|
|
50
|
+
"node": "^14 || ^16 || >=18.0"
|
|
51
|
+
},
|
|
31
52
|
"devDependencies": {
|
|
32
|
-
"@
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
"@babel/register": "^7.16.9",
|
|
36
|
-
"babel-plugin-add-module-exports": "^1.0.0",
|
|
37
|
-
"eslint": "^8.7.0",
|
|
38
|
-
"eslint-plugin-import": "^2.25.4",
|
|
53
|
+
"@types/node": "^18.16.2",
|
|
54
|
+
"eslint": "^8.39.0",
|
|
55
|
+
"eslint-config-prettier": "^8.8.0",
|
|
39
56
|
"jison-gho": "^0.6.1-216",
|
|
40
57
|
"postcss": "^8.2.2",
|
|
41
|
-
"
|
|
42
|
-
"typescript": "
|
|
43
|
-
"uvu": "^0.5.
|
|
58
|
+
"prettier": "^2.8.8",
|
|
59
|
+
"typescript": "~5.0.4",
|
|
60
|
+
"uvu": "^0.5.6"
|
|
44
61
|
},
|
|
45
62
|
"dependencies": {
|
|
46
|
-
"postcss-selector-parser": "^6.0.
|
|
47
|
-
"postcss-value-parser": "^4.0
|
|
63
|
+
"postcss-selector-parser": "^6.0.11",
|
|
64
|
+
"postcss-value-parser": "^4.2.0"
|
|
48
65
|
},
|
|
49
66
|
"peerDependencies": {
|
|
50
67
|
"postcss": "^8.2.2"
|
|
51
|
-
}
|
|
52
|
-
|
|
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 && tsc",
|
|
55
|
-
"pretest": "pnpm run build",
|
|
56
|
-
"test": "uvu -r @babel/register src/__tests__"
|
|
57
|
-
},
|
|
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[![Support Chat][git-img]][git-url]\n\n[PostCSS Calc] lets you reduce `calc()` references whenever it's possible.\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\nUsing this `input.css`:\n\n```css\nh1 {\n font-size: calc(16px * 2);\n height: calc(100px - 2em);\n width: calc(2*var(--base-width));\n margin-bottom: calc(16px * 1.5);\n}\n```\n\nyou will get:\n\n```css\nh1 {\n font-size: 32px;\n height: calc(100px - 2em);\n width: calc(2*var(--base-width));\n margin-bottom: 24px\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## Related PostCSS plugins\nTo replace the value of CSS custom properties at build time, try [PostCSS Custom Properties].\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[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
|
-
}
|
|
68
|
+
}
|
|
69
|
+
}
|
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;
|