Skip to content

Instantly share code, notes, and snippets.

@courtsimas
Created August 24, 2011 01:01
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save courtsimas/1167053 to your computer and use it in GitHub Desktop.
Save courtsimas/1167053 to your computer and use it in GitHub Desktop.
@import-once hack for sass
//sessions.css.scss - file #1 that i only want to let the _style.scss import be included once
$imported-once-files: ();
@function import-once($name) {
@if index($imported-once-files, $name) {
@return false;
}
$imported-once-files: append($imported-once-files, $name);
@return true;
}
@import "style";
body.sessions {
//normal css for the sessions pages
}
/////////
//donations.css.scss - file that i only want to have _style.scss included once, but if it was already included in sessions, dont double include it
$imported-once-files: ();
@function import-once($name) {
@if index($imported-once-files, $name) {
@return false;
}
$imported-once-files: append($imported-once-files, $name);
@return true;
}
@import "style";
body.donations {
//normal css for the donations pages
}
//////////
//_style.scss shared file with base styles
@if import-once("_style.scss"){
//base styles and declarations of cool sass-iness that you only want included once
}
@jslegers
Copy link

It's a shame we need to use crazy hacks like these to do something as basic as conditional loading of @import files.

Note that the core dev team is reluctant to implement this feature, although they are considering the implementation of a brand new dependency system.

See the following Github issues :

@franciscop
Copy link

Related to sass/sass#139 , they have been considering this for 4 years. Such a pity it's not there yet, it'd be really helpful something like this:

@import "style" !once;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment