Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sass function not available from import file

Tags:

sass

file1.scss

@function toPx($n) {
    @return $n + 0px;
}

file2.scss

body {
   font-size:toPx(10);
}

file3.scss

@import "file1.scss";
@import "file2.scss";

The output of file3.css contains

body {
       font-size:toPx(10);
    }

I can't get my toPx function to work, even if I import directly into file2.scss. If I declare toPx inside file2.scss it will work however.

I'm new to SASS so assuming I'm missing something simple here, is anyone able to tell me what?

Surprisingly Scout, the sass compiler i'm using, does not throw an error but simply renders the toPx(10) into the output CSS.

Edit

I've found this reference which seems to suggest that I will need to use the Ruby API in order to achieve a global function. Is anyone able to clarify?

http://sass-lang.com/docs/yardoc/Sass/Script/Functions.html#adding_custom_functions

like image 533
Chris Avatar asked Mar 14 '13 00:03

Chris


People also ask

Is Sass import deprecated?

In brief, @import is being replaced with more explicit @use and @forward rules. Over the next few years Sass @import will be deprecated, and then removed. You can still use CSS imports, but they won't be compiled by Sass.

How do I declare a function in sass?

Functions can be defined using @function at-rule. A function name can be any Sass identifier. It must only contain universal statements, as well as the @return at-rule which indicates the value to use result of the function call. The normal CSS functions are used for calling functions.

How do I link a SCSS file to HTML?

You can not "attach" a SASS/SCSS file to an HTML document. SASS/SCSS is a CSS preprocessor that runs on the server and compiles to CSS code that your browser understands.


1 Answers

I stumbled upon a hack answer, that makes me think it's probably a Scout bug.

I altered file3.scss to this

@import "file1.scss";
@import "file2.scss";
body {
       font-size:toPx(10);
    }

After compiling that, it worked as intended. Next I changed my files back to how I described in the question and now all my calls for toPx work in any file.

like image 147
Chris Avatar answered Sep 23 '22 23:09

Chris