-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Feature request: @import-once #156
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
Comments
This is something we'll consider for the upcoming re-envisioning of the |
Duplicate of #139 |
BTW, that @if stuff is pretty clever :) It's pretty much how C solves this problem. |
Yah, I've done a lot of C coding, give or take a million lines. :-) For the record, I think it's more meaningful to have the once-ness determined by the importee rather than the importer --- by the file that's being imported rather than by the file that's importing it. @import-once is a safe construct; no matter who imports that file, they can't screw it up, because it's impossible to import that file more than once; the caller can't make a mistake. But the suggestion in the other thread (which I missed when I searched for this topic), where you might add @require --- that suggestion allows the caller to decide whether the file gets imported once or many times, which is dangerous: If any one caller forgets and uses @import instead of @require, the imported code may end up imported more than once because of it. I've been bit by that in other languages, and I'd rather not see the same problem repeated in Sass. |
+1 for native support. I just had a specificity-override scenario where I had to implement the import-once @function above to work around it :( |
Now available as a plugin: https://github.com/chriseppstein/compass/blob/master/import-once/README.md |
@chriseppstein Did this get merged into 3.3 or is it still just a plugin? |
@jlong, it looks like this will be part of the 4.0 release. |
That will be a great addition! |
I've used Sass on a number of projects now with great success, and finally convinced my company to use it on our flagship product --- a massive web application --- to make our 120,000 lines of CSS more manageable.
In doing so, I've found that there's a feature that would make for a really nice addition: The ability to mark some files with an "@import-once" directive, which causes that file to be imported only once no matter how many different other files import it.
Consider this scenario:
Without doing anything else, TheirPage.css will end up with two copies of any CSS found in _SharedBaseStuff.scss, which is undesirable. It'd be far better if inside _SharedBaseStuff.scss, you could just declare @import-once; at the top, which would ensure you only get the first copy in the output no matter how many times it's imported. (By definition, @import-once could not be appear inside an imported file that is nested within another selector or mixin; it could only appear at the root.)
(Yes, this is a real scenario on our huge site. In fact, our real-world scenarios are far uglier, with some .scss files able to be imported dozens of times or more by a single page's stylesheet.)
Currently, a little function like this can be used to implement a workaround:
And then used like this:
That works, but for large filesets with lots of imports, it's inefficient. Having a simple global @import-once; directive would make this much simpler.
The text was updated successfully, but these errors were encountered: