ParserError when dividing a CSS variable by a negative number
See original GitHub issueDescribe the bug
Dividing a CSS variable by a negative number yields a ParserError
during production builds. Live preview works ok.
Which terms did you search for in User Guide?
ParserError, CSS variables
Environment
Environment Info:
current version of create-react-app: 4.0.3
running from <snip>\create-react-app
System:
OS: Windows 10 10.0.19042
CPU: (16) x64 Intel(R) Core(TM) i7-10700K CPU @ 3.80GHz
Binaries:
Node: 15.5.1 - C:\Program Files\nodejs\node.EXE
Yarn: Not Found
npm: 7.9.0 - C:\Program Files\nodejs\npm.CMD
Browsers:
Chrome: Not Found
Edge: Spartan (44.19041.906.0), Chromium (90.0.818.46)
Internet Explorer: 11.0.19041.1
npmPackages:
react: ^17.0.2 => 17.0.2
react-dom: ^17.0.2 => 17.0.2
react-scripts: 4.0.3 => 4.0.3
npmGlobalPackages:
create-react-app: Not Found
Steps to reproduce
- Create a fresh react app with
npx create-react-app --template typescript
- Add the following css to
App.css
:
.App-logo {
--size: -10px;
width: calc(var(--size)/-2);
}
- run
npm run build
Expected behavior
The build should compile successfully, since live preview works as expected.
Actual behavior
PS C:\<snip>\cra> npm run build
> cra@0.1.0 build
> react-scripts build
Creating an optimized production build...
Failed to compile.
./src/App.css
ParserError: Syntax Error at line: 1, column 18
Etc
If either var(--size)
is replaced by some constant, or the expression is rewritten as calc(-1/2 * var(--size))
, the problem disappears.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:3
- Comments:9
Top Results From Across the Web
Post CSS parse error on calc() of CSS variables with ...
I'm running a Nuxt.JS project with Post CSS and get the following error while generating the project's static ...
Read more >Simple way to negate a number in CSS - Dzhavat Ushev
At one point I wanted to negate a variable that held a positive number. My first attempt was: :root { --base-padding: 1rem ...
Read more >Why is this not working : r/Sass
Why can't I divide a css variable with a negative number? ... ParserError: Syntax Error at line: 1, column 19
Read more >CSS Syntax Module Level 3
Conformance checkers must report at least one parse error condition ... U+002D HYPHEN-MINUS (-): If the input stream starts with a number, ...
Read more >SyntaxError: identifier starts immediately after numeric literal
Only subsequent characters can be digits (0-9). Examples. Variable names starting with numeric literals. Variable names can't start with numbers in JavaScript ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Problem and Error
I can confirm this. The following rules will cause an error during
npm run build
(react-scripts build
)The error is:
BTW the rule is not in line 1 column 25, so it is totally misleading.
Solutions
margin-top:calc(-1*var(--bs-gutter-y))
-
:margin-top:calc(var(--bs-gutter-y)* -1)
This code is from Bootstrap 5.0.2 which has the spaces (even in the minified version) so my assumption is that somewhere, the script minifies it again and removes the spaces. This could explain why it works in dev mode, where presumably there is no minification.
Also, check out this related Stack Overflow question.
Sass recently added additional features for
calc()
which includes more aggressively minifying whitespace incalc()
expressions. Note that the problematic expressioncalc(var(--bs-gutter-y)*-1)
is valid CSS. To quote Values and Units Level 4:It seems like some other tool is parsing Sass’s output and choking on this valid CSS, which is likely a bug in that tool.