Global Variables are ignored
See original GitHub issueIf you try to define a variable like so:
/* Global Variables */
:global :root {
--bkg: red;
}
/* Local Variables */
:root {
--color: blue;
}
html {
background-color: var(--bkg);
color: var(--color);
}
You will get the following error back from webpack:
app/containers/App/styles.css
4:3 Custom property ignored: not scoped to the top-level :root element (:global :root { ... --bkg: ... }) [postcss-custom-properties]
12:3 variable '--bkg' is undefined and used without a fallback [postcss-custom-properties]
And a response that looks like this:
/* Global Variables */
:root {
--bkg: red;
}
/* Local Variables */
html {
background-color: var(--bkg);
color: blue;
}
Essentially postcss-custom-properties looks for :root specifically and if it finds a variable in any other scope, it leaves the variable as is and doesn’t touch it. When it sees :global :root it automatically assumes it’s not the right scope and moves on.
This prevents a dev from add global variables such as theme colors and similar that can be reused across files.
Issue Analytics
- State:
- Created 7 years ago
- Comments:13 (6 by maintainers)
Top Results From Across the Web
Are global variables bad? - c++ - Stack Overflow
So, yes, globals are often bad. But if you feel that in the end, the intent of the programmer is made clearer by...
Read more >Global variable set to true being ignored - Adventure Creator
I have a number of global variables that I set to true after certain interactions are performed. However, when I select a different ......
Read more >Unused global variables ignored by compiler's optimization?
When I declare a global variable (even if not initialized) but never use it, it shows up as linker symbol. Further the total...
Read more >Global Variables - UNC Comp 110
This means that the value of the global variable is ignored when working with a variable of the same name in a local...
Read more >IF expression ignored using global variable even if false
Note that the Player's Y variable is stored on a global variable on a persistent object. I checked, it ALWAYS shows the same...
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 Free
Top 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

I’m still having a problem with this. Getting warnings for every global variable I use. I tried adding
postcss-custom-propertiesandpostcss-import, but probably misconfigured them and can’t find a good example of Webpack config. Tried the one right above, but also no luck. Can someone please point me in the right direction?My config:
I tired adding
customPropertiesin various variations into the mix, but warnings remain. Thank you!Currently this project does not handle
@importstatement. You will need something like postcss-import in your stack to resolve this.