question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug] : css nano strips away important factors from multiplications in calc()

See original GitHub issue

COPY OF https://github.com/cssnano/cssnano/issues/901

Describe the bug A CSS input with a calc function using css variables strips away factors from multiplications:

calc(var(--foo) * 0.4 - var(--bar) * 0.4); … will get converted to … calc(var(--foo) - var(--bar));

… and that is incorrect. This happens using cssnanos default preset.

Input given:

.baz {
  --correct-1: calc(var(--foo) * 0.2 - var(--bar) * 0.8);
  --correct-2: var(--foo) * 0.4 - var(--bar) * 0.4;

  --incorrect-1: calc(var(--foo) * 0.4 - var(--bar) * 0.4);
  --incorrect-2: calc(var(--foo) * 0.5 - var(--bar) * 0.5);
  --incorrect-3: calc((var(--foo) * 0.5) - (var(--bar) * 0.5));
}

Expected output

.baz {
  --correct-1: calc(var(--foo) * 0.2 - var(--bar) * 0.8);
  --correct-2: var(--foo) * 0.4 - var(--bar) * 0.4;

  --incorrect-1: calc(var(--foo) * 0.4 - var(--bar) * 0.4);
  --incorrect-2: calc(var(--foo) * 0.5 - var(--bar) * 0.5);
  --incorrect-3: calc((var(--foo) * 0.5) - (var(--bar) * 0.5));
}

Actual output

.baz {
  --correct-1: calc(var(--foo) * 0.2 - var(--bar) * 0.8);
  --correct-2: var(--foo) * 0.4 - var(--bar) * 0.4;
  --incorrect-1: calc(var(--foo) - var(--bar));
  --incorrect-2: calc(var(--foo) - var(--bar));
  --incorrect-3: calc(var(--foo) - var(--bar));
}

Repo for reproduction https://github.com/Sanderand/css-nano-calc-bug

Additional context

  "dependencies": {
    "cssnano": "4.1.10",
    "postcss-cli": "7.1.0"
  },
const cssnano = require('cssnano');

module.exports = {
  plugins: [
    cssnano({ preset: 'default' }),
  ],
};

Disabling the calc optimization is a temporary workaround that I now use. I guess the calc optimization needs to be fixed.

Thanks!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:6
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
coreyworrellcommented, Sep 1, 2020

Unfortunately #114 does not fix the following case (--fluid-bp):

Source:

:root {
    --fluid-min-width: 320;
    --fluid-max-width: 1140;
    --fluid-screen: 100vw;
    --fluid-bp: calc(
        (var(--fluid-screen) - ((var(--fluid-min-width) / 16) * 1rem)) /
        ((var(--fluid-max-width) / 16) - (var(--fluid-min-width) / 16))
    );
}

7.0.4 outputs:

:root {
    --fluid-min-width: 320;
    --fluid-max-width: 1140;
    --fluid-screen: 100vw;
    --fluid-bp: calc(
        (var(--fluid-screen) - var(--fluid-min-width) / 16 * 1rem) / 
        (var(--fluid-max-width) - var(--fluid-min-width)) / 16
    );
}

The output should instead be:

:root {
    --fluid-bp: calc(
        (var(--fluid-screen) - (var(--fluid-min-width) / 16) * 1rem)) / 
        ((var(--fluid-max-width) - var(--fluid-min-width)) / 16
    );
}

postcss-calc is removing the necessary parentheses and affecting the operator precedence.

1reaction
mischniccommented, Aug 26, 2020

Seems to happen with division as well:

const postcss = require('postcss');
const calc = require('postcss-calc');

const INPUT = `
.a {
    border-width: calc(var(--foo) / 2 - var(--bar) / 2);
}
`;

const output = postcss().use(calc()).process(INPUT).css;

console.log(output.trim());

/*
.a {
    border-width: calc(var(--foo) - var(--bar));
}
*/
Read more comments on GitHub >

github_iconTop Results From Across the Web

Add configuration to postcss in Sage 10 - Roots Discourse
COPY OF cssnano/cssnano#901 Describe the bug A CSS input with a calc function using css variables strips away factors from multiplications: calc ......
Read more >
https://www.med.upenn.edu/dart/assets/design/files...
... reduction # 7.0.4 - Fixed: strips away important factors from multiplications in calc() ([#107](https://github.com/postcss/postcss-calc/issues/107)) ...
Read more >
postcss-calc | Yarn - Package Manager
PostCSS Calc lets you reduce calc() references whenever it's possible. When multiple units are mixed together in the same expression, the calc() statement ......
Read more >
calc() - CSS: Cascading Style Sheets - MDN Web Docs
The calc() CSS function lets you perform calculations when specifying CSS property values. It can be used with , , , , ,...
Read more >
Does the CSS calc() impact performance? - Quora
So if you're not sure you should use CSS Calc() because of impact on ... and by that I mean back in the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found