{"name":"postcss-calc","version":"11.2.1","keywords":["css","postcss","postcss-plugin","calculation","calc"],"author":"Andy Jansson","license":"MIT","_id":"postcss-calc@11.2.1","maintainers":[{"name":"beneb","email":"beneb.info@gmail.com"},{"name":"moox","email":"npm@moox.io"},{"name":"ludovicofischer","email":"livrerie@gmail.com"},{"name":"semigradsky","email":"semigradskyd@gmail.com"},{"name":"jonathantneal","email":"jonathantneal@hotmail.com"},{"name":"andyjansson","email":"jansson.andy@gmail.com"},{"name":"evilebottnawi","email":"sheo13666q@gmail.com"}],"homepage":"https://github.com/postcss/postcss-calc","dist":{"shasum":"851c627091d96e0d765318f97f34ec6599641639","tarball":"http://registry-npm.ywwl.com/postcss-calc/download/postcss-calc-11.2.1.tgz","fileCount":76,"integrity":"sha512-B5KZhrbeVyFznh7aj96O9oYvmipuobzWJVlPZ2NksjHdgSaqiVJ0UijuOuQTk/KVIzrKXp7399AiZGKtZF0jZA==","signatures":[{"sig":"MEQCID5UKC1rPqlsbQEbFvqm52OJeeJhIQkCA26oGTFtXtUJAiBDZ3JHQNtOJQAM+B3edR76Iyxd87b5F2/4bgzQnPM5rw==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/postcss-calc@11.2.1","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"unpackedSize":163819,"size":42873,"noattachment":false,"key":"/postcss-calc/-/postcss-calc-11.2.1.tgz"},"type":"module","engines":{"node":"^22.22.3 || ^24.15 || >=26.0"},"exports":{".":{"types":"./types/index.d.ts","default":"./src/index.js"},"./reduce":{"types":"./types/reduce.d.ts","default":"./src/reduce.js"}},"scripts":{"fmt":"oxfmt","lint":"oxlint . && tsc && oxfmt --check","test":"node --test --test-reporter=dot 'test/**/*.test.js' 'test/**/*.test.cjs'","test:benchmark":"node --test 'test/unit/benchmark-*.test.js' test/unit/compare-parser-benchmarks.test.js test/unit/corpus-benchmark.test.js","benchmark:corpus":"node scripts/benchmark.js","test:corpus:full":"POSTCSS_CALC_FULL_CORPUS=1 node --test test/conformance/corpus.test.js","benchmark:reanalyze":"node scripts/compare-parser-benchmarks.js","test:mutation:corpus":"node test/mutation/corpus-selection.js","benchmark:serialization":"node scripts/benchmark-serialization.js","test:benchmark:simulation":"node test/benchmark/statistical-simulation.js","benchmark:nested-fallbacks":"node scripts/benchmark-nested-fallbacks.js","benchmark:arithmetic-chains":"node scripts/benchmark-arithmetic-chains.js"},"_npmUser":{"name":"GitHub Actions","email":"npm-oidc-no-reply@github.com","approver":{"name":"ludovicofischer","email":"livrerie@gmail.com"},"trustedPublisher":{"id":"github","oidcConfigId":"oidc:12fec003-011d-4ec1-bc80-f6b0007d9e4b"}},"devEngines":{"packageManager":{"name":"pnpm","version":"12.4.2"}},"repository":{"url":"git+https://github.com/postcss/postcss-calc.git","type":"git"},"description":"PostCSS plugin to reduce calc()","directories":{},"dependencies":{"@csstools/css-tokenizer":"^4.0.1"},"_hasShrinkwrap":false,"devDependencies":{"oxfmt":"^0.68.0","oxlint":"^1.83.0","postcss":"^8.5.28","fast-check":"^4.10.1","typescript":"~7.0.2","@types/node":"^26.6.1","@csstools/css-calc":"^3.4.0"},"peerDependencies":{"postcss":"^8.5.28"},"_npmOperationalInternal":{"tmp":"tmp/postcss-calc_11.2.1_1789905446928_0.41594836696112303","host":"s3://npm-registry-packages-npm-production"},"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 an expression mixes units that cannot be combined exactly (such as `px`\nand `em`), or contains values only known at runtime (such as `var()`), the\nunresolved part is left for the browser's [CSS Values 4][css-values-4]\nimplementation.\n\n## Installation\n\n```bash\nnpm install postcss-calc\n```\n\n## PostCSS 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().use(calc()).process(css).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: calc(32px);\n  height: calc(100px - 2em);\n  width: calc(2 * var(--base-width));\n  margin-bottom: calc(24px);\n}\n```\n\nCheckout [tests] for more examples.\n\n## Use the reducer without PostCSS\n\nFor a single CSS component-value string, import the dedicated reducer entry\npoint. It reduces `calc()` and the supported CSS math functions it finds while\nleaving all other text untouched.\n\n```js\nimport reduceCalc from 'postcss-calc/reduce';\n\nreduceCalc('calc(1in + 10px)');\n// => 'calc(106px)'\n\nreduceCalc('min(50px, calc(2 * 40px))');\n// => 'calc(50px)'\n```\n\nIt accepts `precision`, `unwrapSingleValue`, `warnWhenCannotResolve`,\n`onParseError`, and `onWarn`:\n\n```js\nconst result = reduceCalc('calc(100% + var(--gap))', {\n  precision: false,\n  warnWhenCannotResolve: true,\n  onWarn: console.warn,\n  onParseError(error, input) {\n    console.error(`Invalid calculation: ${input}`, error);\n  },\n});\n```\n\nUnlike the PostCSS plugin, the standalone reducer does not show warnings\nby default; provide `onParseError` and/or `onWarn` if you want diagnostics.\n\n### Standalone reducer options\n\n#### `unwrapSingleValue` (default: `false`)\n\nSerializes a fully resolved finite scalar result without calculation syntax.\nKeep the default for standard CSS so the browser can perform range clamping\nand integer rounding. Set it to `true` for a non-standard context that requires\na bare value, such as a selector:\n\n```js\nreduceCalc('calc(5px - 10px)');\n// => 'calc(-5px)'\n\nreduceCalc('calc(5px - 10px)', { unwrapSingleValue: true });\n// => '-5px'\n\nreduceCalc('calc(1 / 2)', { unwrapSingleValue: true });\n// => '.5'\n```\n\nThe published `unwrapSingleNegativeNumber` option is retained as a deprecated\nalias for `unwrapSingleValue`.\n\n### PostCSS plugin options\n\nThese options apply when using the PostCSS plugin:\n\n```js\npostcss().use(calc({ precision: 10 }));\n```\n\n#### `precision` (default: `5`)\n\nAllows you to define the precision for decimal numbers. Set it to `false` to\ndisable rounding and preserve full IEEE-754 floating-point precision (emitting\nthe shortest round-tripping decimal representation).\n\nValues below 1 keep `precision` significant digits (`.0123456px` becomes\n`.012346px`), and larger values keep `precision` decimals. Divisions and unit\nconversions are folded only when the result is exact at the precision;\notherwise they stay symbolic and everything around them is still simplified:\n\n```css\n.a {\n  width: calc(100% / 4);\n} /* calc(25%) */\n.b {\n  width: calc(100% / 3);\n} /* calc(100% / 3), not 33.33333% */\n.c {\n  width: calc(1cm + 1px);\n} /* calc(1cm + 1px) */\n.d {\n  width: calc(1px + 1pt);\n} /* calc(1.75pt) */\n```\n\nWith `precision: false` nothing is rounded, so every division is folded.\n\n```js\nvar out = postcss()\n  .use(calc({ precision: 10 }))\n  .process(css).css;\n```\n\n#### `unwrapSingleValue` (default: `false`)\n\nSerializes fully resolved finite scalar results without calculation syntax.\nThis can discard browser-applied range clamping or integer rounding. Selectors\nenable it automatically because selectors cannot contain `calc()`.\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).css;\n```\n\n#### `mediaQueries` (default: `false`)\n\nAllows calc() usage in media query parameters.\n\n```js\nvar out = postcss()\n  .use(calc({ mediaQueries: true }))\n  .process(css).css;\n```\n\nExample:\n\n```css\n@media (min-width: calc(100px + 100px)) {\n  div {\n    width: 100px;\n  }\n}\n```\n\nWith `mediaQueries: true`, this becomes:\n\n```css\n@media (min-width: calc(200px)) {\n  div {\n    width: 100px;\n  }\n}\n```\n\nAdd `unwrapSingleValue: true` to get the bare value, `(min-width: 200px)`.\n\n#### `selectors` (default: `false`)\n\nReduces `calc()` functions found in selectors. Selectors do not accept\n`calc()` functions, so the plugin replaces them with their reduced values, as\nif `unwrapSingleValue` were enabled.\n\n```js\nvar out = postcss()\n  .use(calc({ selectors: true }))\n  .process(css).css;\n```\n\nExample:\n\n```css\ndiv:nth-child(calc(1 + 2)) {\n  width: 100px;\n}\n```\n\nWith `selectors: true`, this becomes `div:nth-child(3)`.\n\n#### `onParseError`\n\nCallback invoked with the error and the input value when a `calc()` body fails\nto parse or simplify:\n\n```js\npostcss().use(\n  calc({\n    onParseError: (err, input) => {\n      throw err; // or log, route to a different channel, etc.\n    },\n  })\n);\n```\n\nWhen omitted, errors are reported via PostCSS `result.warn()` so the\nplugin never throws at the postcss level.\n\n### Behavior differences from the legacy parser\n\nThe legacy [jison][jison]-generated parser was replaced by a hand-written\nPratt parser whose simplifier follows [CSS Values 4][css-values-4]. Most\ninputs reduce to identical output; the differences are spec-aligned or\ncanonical-form decisions:\n\n- **Strict whitespace (§10.1).** `calc(2px+3px)` is invalid CSS (binary\n  `+` / `-` require surrounding whitespace) and is preserved with a\n  warning instead of reduced.\n- **Canonical operand order.** Sums serialize numbers first, then\n  dimensions in first-seen order, then unresolved terms:\n  `calc(var(--foo) + 10px)` → `calc(10px + var(--foo))`.\n- **Zero buckets are kept.** `calc(100px - (100px - 100%))` →\n  `calc(0px + 100%)`, not `100%` — [WPT calc-serialization-002][wpt-calc-serialization]\n  requires the zero term because it carries the length-percentage type.\n- **Constant folding.** `calc(43 + pi)` now folds to `calc(46.14159)` (§10.7.1).\n  Previously `pi` / `e` stayed symbolic.\n- **Distributive multiplication.** `calc(0.5 * (100vw - 10px))` becomes\n  `calc(50vw - 5px)`.\n- **Unit case normalization.** `2PX` becomes `2px` (CSS units are case-\n  insensitive; lowercase is conventional).\n- **Spec-style spaced operators.** `2px*var(--x)` is serialized as\n  `2px * var(--x)`. The tokenizer is unaffected; only output spacing\n  differs.\n- **Division by zero / by a unit.** `calc(500px/0)` reduces to\n  `calc(infinity * 1px)` (§10.13) instead of throwing. Use `onParseError`\n  if you want validation behavior.\n\n[css-values-4]: https://www.w3.org/TR/css-values-4/\n[wpt-calc-serialization]: https://github.com/web-platform-tests/wpt/blob/master/css/css-values/calc-serialization-002.html\n[jison]: https://github.com/zaach/jison\n\n---\n\n## Related PostCSS plugins\n\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. See [CONTRIBUTING.md](CONTRIBUTING.md)\nfor the full guidelines. The project uses [pnpm](https://pnpm.io/).\n\n```bash\ngit clone git@github.com:postcss/postcss-calc.git\ncd postcss-calc\ngit checkout -b patch-1\npnpm install\npnpm test\npnpm lint\n```\n\nThe normal test run uses a deterministic structural sample of the harvested\nreal-world corpus. Run the complete differential corpus before releases or\nwhen changing parsing/simplification behavior:\n\n```bash\npnpm test:corpus:full\n```\n\nCheck performance changes with the benchmarks described in\n[BENCHMARKS.md](BENCHMARKS.md), and read it before interpreting results.\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[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]: test/\n","publish_time":1789905447047,"_cnpm_publish_time":1789905447047,"dist-tags":{"latest":"11.2.3","next":"11.0.0-rc.3"}}