Skip to content

Instantly share code, notes, and snippets.

@courtsimas
Created August 24, 2011 01:01
Show Gist options
  • 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
}
@jonnybojangles
Copy link

Thanks, this helped me a lot while combining several independent modules into a combined layout CSS. I added !default to $imported-once-files so I only need to define the function once.

@JawsomeJason
Copy link

OMFG! You just saved my life! Placeholders are atrocious if they get imported twice.

I'm totally now using this on JSMACSS!!!! Thank you!

@svallory
Copy link

Hey, I just published a gem which makes import once the default behaviour. Would love to hear what you guys think. Here it is: https://github.com/theblacksmith/sass-import_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