Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

False positive "undefined variable" error when compiling SCSS

Tags:

sass

Getting an error message when compiling my SCSS using the ruby compass gem.

run: /var/lib/gems/1.8/gems/compass-0.12.2/bin/compass compile out: unchanged sass/partial/grid.scss out:     error sass/partial/catalog.scss (Line 5: Undefined variable: "$paragraphFont".) out:    create css/generated/partial/catalog.css  out:    create css/generated/partial/base.css  out: overwrite css/generated/screen.css 

My screen.scss imports partials like this:

@import "partial/base"; @import "partial/catalog"; 

In my base partial I have the $paragraphFont defined.

$paragraphFont: 'Lucida Sans', arial; $regularFontSize: 14px; 

And in catalog.scss I use it:

.product-view #price-block {     p {         font-weight: normal;         font-family: $paragraphFont;         ....     } } 

Weird thing is that the css gets compiled just fine, and the $paragraphFont is populated correctly. So I don't know why the compiler is complaining at me about an error.

like image 327
kalenjordan Avatar asked Jul 31 '13 16:07

kalenjordan


People also ask

Can we use JS variable in SCSS?

Sass variables and JavaScript. Sass is a pre-processing language, meaning it's turned into CSS before it ever is a part of a website. For that reason, accessing them from JavaScript in the same way as CSS custom properties — which are accessible in the DOM as computed styles — is not possible.

Can I use SCSS variables in CSS?

To convert the SASS variable to a CSS custom property you put curly brackets around it, and a hash in front. If you've used template literals in JavaScript it's the same thing, just with a # instead of a $ (because we already have $ in the variable name).

Can you change SCSS variable value dynamically?

Sadly, there is not any way to "make Sass variables dynamic", because Sass itself is pre-compiled, while CSS is interpreted by the browser (dynamically) at runtime.

How do I change variables in SCSS?

A Sass variable must be initialized with a value. To change the value, we simply replace the old value with a new one. A variable that's initialized inside a selector can only be used within that selector's scope. A variable that's initialized outside a selector can be used anywhere in the document.


1 Answers

You're generating files that don't need to be generated.

  • screen.scss -> screen.css
  • base.scss -> base.css
  • catalog.scss -> catalog.css

The catalog file is being compiled on its own. Since it is not importing base.scss, the variables are not set. Your screen.scss file generates as you expect because it is importing all of the necessary information.

What you want to do is rename your partials to begin with an underscore to prevent them from being compiled on their own:

  • screen.scss -> screen.css
  • _base.scss (not compiled)
  • _catalog.scss (not compiled)
like image 200
cimmanon Avatar answered Oct 10 '22 22:10

cimmanon