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

Variables #105

Closed
badsyntax opened this issue May 29, 2015 · 4 comments
Closed

Variables #105

badsyntax opened this issue May 29, 2015 · 4 comments

Comments

@badsyntax
Copy link

Hi there

I am facing a problem with sharing sass variables across different require() calls. I am appending values to a list variable and I need that variable to be shared between different SCSS modules.

A real world scenario is attempting to a "import-once" function that prevent CSS from being output more than once:

// _functions.scss
$imported-once-files: () !default;

@warn $imported-once-files; // debug

@function import-once($filename) {
  @if index($imported-once-files, $filename) {
    @return false;
  }
  $imported-once-files: append($imported-once-files, $filename) !global;
  @return true;
}

You cannot use such an approach with sass-loader as each require() call is completely contained from other require() calls, thus $imported-once-files is reset with each require() call. I think the only way to resolve this issue to load all component files with sass and have only one entry point require('app.scss') call.
I'm hoping I'm making sense! Do you have suggestions on how I could make variables global across different require() calls?
Thanks!

@ppalladino
Copy link

+1

@jhnns
Copy link
Member

jhnns commented Jun 10, 2015

Yep, that's a known problem of preprocessors (both SASS and Less). I haven't found an easy way for SASS, though. Less introduced the import (reference) mechanism for that use-case.

@rayshan
Copy link

rayshan commented Jan 7, 2016

@badsyntax if all you need are shared variables, why not have all of them in a file with only variables, that way it doesn't matter if it's imported multiple times? Like this:
https://github.com/twbs/bootstrap/blob/v4-dev/scss/_variables.scss

@jhnns
Copy link
Member

jhnns commented Dec 26, 2016

We can't solve this because each require() of a SASS file from a JS file will trigger a distinct compilation. This is how loaders work.

@jhnns jhnns closed this as completed Dec 26, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants