Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
JohnAlbin opened this issue Mar 17, 2016 · 1 comment
Closed

Comments

@JohnAlbin
Copy link

JohnAlbin commented Mar 17, 2016

Copying this issue from the node-sass queue, sass/node-sass#1297.

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

@mgreter
Copy link
Contributor

mgreter commented Jan 5, 2017

This was implemented with #2251

@mgreter mgreter closed this as completed Jan 5, 2017
saper pushed a commit to saper/node-sass-issue-1297 that referenced this issue Oct 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants