postcss-calc 8.0.0 → 8.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/dist/lib/transform.js +15 -5
- package/package.json +23 -28
- package/CHANGELOG.md +0 -126
package/dist/lib/transform.js
CHANGED
|
@@ -23,7 +23,7 @@ const MATCH_CALC = /((?:-(moz|webkit)-)?calc)/i;
|
|
|
23
23
|
function transformValue(value, options, result, item) {
|
|
24
24
|
return (0, _postcssValueParser.default)(value).walk(node => {
|
|
25
25
|
// skip anything which isn't a calc() function
|
|
26
|
-
if (node.type !==
|
|
26
|
+
if (node.type !== "function" || !MATCH_CALC.test(node.value)) {
|
|
27
27
|
return node;
|
|
28
28
|
} // stringify calc expression and produce an AST
|
|
29
29
|
|
|
@@ -36,7 +36,7 @@ function transformValue(value, options, result, item) {
|
|
|
36
36
|
|
|
37
37
|
const reducedAst = (0, _reducer.default)(ast, options.precision); // stringify AST and write it back
|
|
38
38
|
|
|
39
|
-
node.type =
|
|
39
|
+
node.type = "word";
|
|
40
40
|
node.value = (0, _stringifier.default)(node.value, reducedAst, value, options, result, item);
|
|
41
41
|
return false;
|
|
42
42
|
}).toString();
|
|
@@ -47,13 +47,13 @@ function transformSelector(value, options, result, item) {
|
|
|
47
47
|
selectors.walk(node => {
|
|
48
48
|
// attribute value
|
|
49
49
|
// e.g. the "calc(3*3)" part of "div[data-size="calc(3*3)"]"
|
|
50
|
-
if (node.type ===
|
|
50
|
+
if (node.type === "attribute" && node.value) {
|
|
51
51
|
node.setValue(transformValue(node.value, options, result, item));
|
|
52
52
|
} // tag value
|
|
53
53
|
// e.g. the "calc(3*3)" part of "div:nth-child(2n + calc(3*3))"
|
|
54
54
|
|
|
55
55
|
|
|
56
|
-
if (node.type ===
|
|
56
|
+
if (node.type === "tag") {
|
|
57
57
|
node.value = transformValue(node.value, options, result, item);
|
|
58
58
|
}
|
|
59
59
|
|
|
@@ -63,11 +63,21 @@ function transformSelector(value, options, result, item) {
|
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
var _default = (node, property, options, result) => {
|
|
66
|
-
|
|
66
|
+
let value = node[property];
|
|
67
|
+
|
|
68
|
+
try {
|
|
69
|
+
value = property === "selector" ? transformSelector(node[property], options, result, node) : transformValue(node[property], options, result, node);
|
|
70
|
+
} catch (error) {
|
|
71
|
+
result.warn(error.message, {
|
|
72
|
+
node
|
|
73
|
+
});
|
|
74
|
+
return;
|
|
75
|
+
} // if the preserve option is enabled and the value has changed, write the
|
|
67
76
|
// transformed value into a cloned node which is inserted before the current
|
|
68
77
|
// node, preserving the original value. Otherwise, overwrite the original
|
|
69
78
|
// value.
|
|
70
79
|
|
|
80
|
+
|
|
71
81
|
if (options.preserve && node[property] !== value) {
|
|
72
82
|
const clone = node.clone();
|
|
73
83
|
clone[property] = value;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "postcss-calc",
|
|
3
|
-
"version": "8.
|
|
3
|
+
"version": "8.1.0",
|
|
4
4
|
"description": "PostCSS plugin to reduce calc()",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"css",
|
|
@@ -14,39 +14,30 @@
|
|
|
14
14
|
"dist",
|
|
15
15
|
"LICENSE"
|
|
16
16
|
],
|
|
17
|
-
"scripts": {
|
|
18
|
-
"prepublish": "npm run build",
|
|
19
|
-
"build": "del-cli dist && cross-env BABEL_ENV=publish babel src --out-dir dist --ignore src/__tests__/**/*.js && jison src/parser.jison -o dist/parser.js",
|
|
20
|
-
"pretest": "npm run build && eslint src",
|
|
21
|
-
"test": "ava"
|
|
22
|
-
},
|
|
23
17
|
"author": "Andy Jansson",
|
|
24
18
|
"license": "MIT",
|
|
25
19
|
"repository": "https://github.com/postcss/postcss-calc.git",
|
|
26
20
|
"eslintConfig": {
|
|
27
|
-
"
|
|
28
|
-
|
|
21
|
+
"extends": [
|
|
22
|
+
"eslint:recommended",
|
|
23
|
+
"plugin:import/recommended"
|
|
24
|
+
],
|
|
29
25
|
"rules": {
|
|
30
26
|
"curly": "error"
|
|
31
27
|
}
|
|
32
28
|
},
|
|
33
29
|
"devDependencies": {
|
|
34
|
-
"@babel
|
|
35
|
-
"@babel/
|
|
36
|
-
"@babel/
|
|
37
|
-
"@babel/
|
|
38
|
-
"@babel/register": "^7.0.0",
|
|
30
|
+
"@ava/babel": "^2.0.0",
|
|
31
|
+
"@babel/cli": "^7.16.0",
|
|
32
|
+
"@babel/core": "^7.16.5",
|
|
33
|
+
"@babel/plugin-transform-modules-commonjs": "^7.16.5",
|
|
39
34
|
"ava": "^3.15.0",
|
|
40
|
-
"babel-eslint": "^10.0.1",
|
|
41
35
|
"babel-plugin-add-module-exports": "^1.0.0",
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
"eslint": "^5.7.0",
|
|
45
|
-
"eslint-config-i-am-meticulous": "^11.0.0",
|
|
46
|
-
"eslint-plugin-babel": "^5.2.1",
|
|
47
|
-
"eslint-plugin-import": "^2.14.0",
|
|
36
|
+
"eslint": "^8.5.0",
|
|
37
|
+
"eslint-plugin-import": "^2.25.3",
|
|
48
38
|
"jison-gho": "^0.6.1-216",
|
|
49
|
-
"postcss": "^8.2.2"
|
|
39
|
+
"postcss": "^8.2.2",
|
|
40
|
+
"rimraf": "^3.0.2"
|
|
50
41
|
},
|
|
51
42
|
"dependencies": {
|
|
52
43
|
"postcss-selector-parser": "^6.0.2",
|
|
@@ -56,9 +47,13 @@
|
|
|
56
47
|
"postcss": "^8.2.2"
|
|
57
48
|
},
|
|
58
49
|
"ava": {
|
|
59
|
-
"
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
50
|
+
"babel": true
|
|
51
|
+
},
|
|
52
|
+
"scripts": {
|
|
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",
|
|
55
|
+
"pretest": "pnpm run build",
|
|
56
|
+
"test": "ava"
|
|
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[![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
|
+
}
|
package/CHANGELOG.md
DELETED
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
# 8.0.0
|
|
2
|
-
|
|
3
|
-
- Breaking: Updated PostCSS from v7.x to v8.x ([#125](https://github.com/postcss/postcss-calc/pull/125))
|
|
4
|
-
|
|
5
|
-
# 7.0.5
|
|
6
|
-
|
|
7
|
-
- Fixed: reduction
|
|
8
|
-
|
|
9
|
-
# 7.0.4
|
|
10
|
-
|
|
11
|
-
- Fixed: strips away important factors from multiplications in calc() ([#107](https://github.com/postcss/postcss-calc/issues/107))
|
|
12
|
-
|
|
13
|
-
# 7.0.3
|
|
14
|
-
|
|
15
|
-
- Fixed: substracted css-variable from zero ([#111](https://github.com/postcss/postcss-calc/issues/111))
|
|
16
|
-
|
|
17
|
-
# 7.0.2
|
|
18
|
-
|
|
19
|
-
- Fixed: incorrect reduction of subtraction from zero ([#88](https://github.com/postcss/postcss-calc/issues/88))
|
|
20
|
-
- Fixed: doesn't remove calc for single function
|
|
21
|
-
- Fixed: relax parser on unknown units ([#76](https://github.com/postcss/postcss-calc/issues/76))
|
|
22
|
-
- Fixed: handle numbers with exponen composed ([#83](https://github.com/postcss/postcss-calc/pull/83))
|
|
23
|
-
- Fixed: handle plus sign before value ([#79](https://github.com/postcss/postcss-calc/pull/79))
|
|
24
|
-
- Fixed: better handle precision for nested calc ([#75](https://github.com/postcss/postcss-calc/pull/75))
|
|
25
|
-
- Fixed: properly handle nested add and sub expression inside sub expression ([#64](https://github.com/postcss/postcss-calc/issues/64))
|
|
26
|
-
- Fixed: handle uppercase units and functions ([#71](https://github.com/postcss/postcss-calc/pull/71))
|
|
27
|
-
- Fixed: do not break `calc` with single var ([cssnano/cssnano#725](https://github.com/cssnano/cssnano/issues/725))
|
|
28
|
-
- Updated: `postcss` to 7.0.27 (patch)
|
|
29
|
-
- Updated: `postcss-selector-parser` to 6.0.2
|
|
30
|
-
- Updated: `postcss-value-parser` to 4.0.2
|
|
31
|
-
|
|
32
|
-
# 7.0.1
|
|
33
|
-
|
|
34
|
-
- Updated: `postcss` to 7.0.2 (patch)
|
|
35
|
-
- Updated: `postcss-selector-parser` to 5.0.0-rc.4 (patch)
|
|
36
|
-
- Updated: `postcss-value-parser` to 3.3.1 (patch)
|
|
37
|
-
|
|
38
|
-
# 7.0.0
|
|
39
|
-
|
|
40
|
-
- Changed: Updated postcss-selector-parser to version 5.0.0-rc.3
|
|
41
|
-
- Changed: Dropped reduce-css-calc as a dependency
|
|
42
|
-
- Fixed: Support constant() and env() ([#42](https://github.com/postcss/postcss-calc/issues/42), [#48](https://github.com/postcss/postcss-calc/issues/48))
|
|
43
|
-
- Fixed: Support custom properties with "calc" in its name ([#50](https://github.com/postcss/postcss-calc/issues/50))
|
|
44
|
-
- Fixed: Remove unnecessary whitespace around `*` and `/` ([cssnano#625](https://github.com/cssnano/cssnano/issues/625))
|
|
45
|
-
- Fixed: Arithmetic bugs around subtraction ([#49](https://github.com/postcss/postcss-calc/issues/49))
|
|
46
|
-
- Fixed: Handling of nested calc statements ([reduce-css-calc#49](https://github.com/MoOx/reduce-css-calc/issues/49))
|
|
47
|
-
- Fixed: Bugs regarding complex calculations ([reduce-cs-calc#45](https://github.com/MoOx/reduce-css-calc/issues/45))
|
|
48
|
-
- Fixed: `100%` incorrectly being transformed to `1` ([reduce-css-calc#44](https://github.com/MoOx/reduce-css-calc/issues/44))
|
|
49
|
-
- Added: support for case-insensitive calc statements
|
|
50
|
-
|
|
51
|
-
# 6.0.2 - 2018-09-25
|
|
52
|
-
|
|
53
|
-
- Fixed: use PostCSS 7 (thanks to @douglasduteil)
|
|
54
|
-
|
|
55
|
-
# 6.0.1 - 2017-10-10
|
|
56
|
-
|
|
57
|
-
- Fixed: throwing error for attribute selectors without a value
|
|
58
|
-
|
|
59
|
-
# 6.0.0 - 2017-05-08
|
|
60
|
-
|
|
61
|
-
- Breaking: Updated PostCSS from v5.x to v6.x, and reduce-css-calc from v1.x
|
|
62
|
-
to v2.x (thanks to @andyjansson).
|
|
63
|
-
|
|
64
|
-
# 5.3.1 - 2016-08-22
|
|
65
|
-
|
|
66
|
-
- Fixed: avoid security issue related to ``reduce-css-calc@< 1.2.4``.
|
|
67
|
-
|
|
68
|
-
# 5.3.0 - 2016-07-11
|
|
69
|
-
|
|
70
|
-
- Added: support for selector transformation via `selectors` option.
|
|
71
|
-
([#29](https://github.com/postcss/postcss-calc/pull/29) - @uniquegestaltung)
|
|
72
|
-
|
|
73
|
-
# 5.2.1 - 2016-04-10
|
|
74
|
-
|
|
75
|
-
- Fixed: support for multiline value
|
|
76
|
-
([#27](https://github.com/postcss/postcss-calc/pull/27))
|
|
77
|
-
|
|
78
|
-
# 5.2.0 - 2016-01-08
|
|
79
|
-
|
|
80
|
-
- Added: "mediaQueries" option for `@media` support
|
|
81
|
-
([#22](https://github.com/postcss/postcss-calc/pull/22))
|
|
82
|
-
|
|
83
|
-
# 5.1.0 - 2016-01-07
|
|
84
|
-
|
|
85
|
-
- Added: "warnWhenCannotResolve" option to warn when calc() are not reduced to a single value
|
|
86
|
-
([#20](https://github.com/postcss/postcss-calc/pull/20))
|
|
87
|
-
|
|
88
|
-
# 5.0.0 - 2015-08-25
|
|
89
|
-
|
|
90
|
-
- Removed: compatibility with postcss v4.x
|
|
91
|
-
- Added: compatibility with postcss v5.x
|
|
92
|
-
|
|
93
|
-
# 4.1.0 - 2015-04-09
|
|
94
|
-
|
|
95
|
-
- Added: compatibility with postcss v4.1.x ([#12](https://github.com/postcss/postcss-calc/pull/12))
|
|
96
|
-
|
|
97
|
-
# 4.0.1 - 2015-04-09
|
|
98
|
-
|
|
99
|
-
- Fixed: `preserve` option does not create duplicated values ([#7](https://github.com/postcss/postcss-calc/issues/7))
|
|
100
|
-
|
|
101
|
-
# 4.0.0 - 2015-01-26
|
|
102
|
-
|
|
103
|
-
- Added: compatibility with postcss v4.x
|
|
104
|
-
- Changed: partial compatiblity with postcss v3.x (stack traces have lost filename)
|
|
105
|
-
|
|
106
|
-
# 3.0.0 - 2014-11-24
|
|
107
|
-
|
|
108
|
-
- Added: GNU like exceptions ([#4](https://github.com/postcss/postcss-calc/issues/4))
|
|
109
|
-
- Added: `precision` option ([#5](https://github.com/postcss/postcss-calc/issues/5))
|
|
110
|
-
- Added: `preserve` option ([#6](https://github.com/postcss/postcss-calc/issues/6))
|
|
111
|
-
|
|
112
|
-
# 2.1.0 - 2014-10-15
|
|
113
|
-
|
|
114
|
-
- Added: source of the error (gnu like message) (fix [#3](https://github.com/postcss/postcss-calc/issues/3))
|
|
115
|
-
|
|
116
|
-
# 2.0.1 - 2014-08-10
|
|
117
|
-
|
|
118
|
-
- Fixed: correctly ignore unrecognized values (fix [#2](https://github.com/postcss/postcss-calc/issues/2))
|
|
119
|
-
|
|
120
|
-
# 2.0.0 - 2014-08-06
|
|
121
|
-
|
|
122
|
-
- Changed: Plugin now return a function to have a consistent api. ([ref 1](https://github.com/ianstormtaylor/rework-color-function/issues/6), [ref 2](https://twitter.com/jongleberry/status/496552790416576513))
|
|
123
|
-
|
|
124
|
-
# 1.0.0 - 2014-08-04
|
|
125
|
-
|
|
126
|
-
✨ First release based on [rework-calc](https://github.com/reworkcss/rework-calc) v1.1.0 (code mainly exported to [`reduce-css-calc`](https://github.com/MoOx/reduce-css-calc))
|