Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Feature request: @import-once #156
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.
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.
referenced this issue
Sep 28, 2013
Now available as a plugin: https://github.com/chriseppstein/compass/blob/master/import-once/README.md