Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

Accessing global Sass variables from within Sass functions written in JS #1297

Open
JohnAlbin opened this issue Dec 6, 2015 · 2 comments
Open

Comments

@JohnAlbin
Copy link
Contributor

Using the new experimental functions feature of libSass, I can write a Sass function in JavaScript. But I'm running into problems trying to access global Sass variables.

It is common for Sass module developers to specify the default values of their mixin or function parameters as equaling a global variables value. e.g.

@function hi($name: $my_module_global) {
  @return "Hello, #{$name}";
}

But I get an error when I try to specify a global variable in the function definition in JS-land. Specifically, doing this:

var sass = require('node-sass');

sass.render({
  data: '$my_module_global: "World"; .test { content: hi(); }',
  functions: {
    'hi($name: $my_module_global)': function(name, done) {
      done(sass.types.String('Hello, ' + name.getValue() + '!'));
    }
  }
}, function(error, result) {
  console.log(result ? result.css.toString() : error);
});

gives me this error:

Error: Undefined variable: "$my-module-global".
        on line 1 of [c function]
>> hi($name: $my_module_global)
   ----------^

Now, if you say its not possible to use a global variable in the string that contains the function and parameters names ('hi($name: $my_module_global)'), I'm cool with that.

However, I would still need to access Sass global variables from within the main body of my function. Like this:

var sass = require('node-sass');

sass.render({
  data: '$my_module_global: "World"; .test { content: hi(); }',
  functions: {
    'hi($name: "nocanhazglobalhere")': function(name, done) {
      // How do I access global Sass variables like $my_module_global in here?
      done(sass.types.String('Hello, ' + name.getValue() + '!'));
    }
  }
}, function(error, result) {
  console.log(result ? result.css.toString() : error);
});

I'm not sure if this is a problem in node-sass or if its a missing part of the API in libSass. I looked at https://github.com/sass/libsass/blob/master/docs/api-function.md but there are parts of it I don't understand.

@xzyfer
Copy link
Contributor

xzyfer commented Dec 10, 2015

Thanks for the report @JohnAlbin. This is something I had previously considered. I expect this is a bug on the LibSass side.

@saper
Copy link
Member

saper commented Oct 18, 2019

Basic functionality got implemented in libsass with sass/libsass#2251

That does not include, however, C function signature parser. Maybe the values could be extracted somehow from the stack via the new functions, but the is getting complicated (there are quite few of them).

https://github.com/saper/node-sass-issue-1297

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants