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.

Global Variables are ignored

See original GitHub issue

If 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:closed
  • Created 7 years ago
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

17reactions
kastsiushkincommented, Feb 19, 2017

I’m still having a problem with this. Getting warnings for every global variable I use. I tried adding postcss-custom-properties and postcss-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:

var precss = require('precss');
var cssnext = require('postcss-cssnext');
var atImport = require('postcss-import');

...
postcss: [
    atImport,
    precss,
    cssnext,
],

I tired adding customProperties in various variations into the mix, but warnings remain. Thank you!

2reactions
MoOxcommented, Jun 1, 2016

Currently this project does not handle @import statement. You will need something like postcss-import in your stack to resolve this.

Read more comments on GitHub >

github_iconTop 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 >

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