-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
One time imports #139
Comments
This would be for performance, right? I can think of this being useful on big packages that have one file that does lots of includes. |
Yes. performance is a big part, it allows each module to be used in isolation without a performance penalty. It also makes it easier to override mixins because they won't be re-parsed later if you define the override after the first time it's loaded. |
I like that last part.. making overriding easier would be great. |
Definitely. This is something I've been seeing a need for lately as well. It would be super handy. |
Marked #156 as a duplicate of this issue. |
Copying my proposal from the other thread: I propose adding an @import-once directive that can be dropped into the imported file, as the first statement in that file. It takes no parameters:
This is a safer directive than the @import-vs-@require solution, since it ensures that the imported file can't be accidentally imported wrong; if it was designed to be imported only once, it can assert that itself, and a caller need not know its behavior to be able to use it correctly. |
Oh, the other thing in the other thread: If anybody needs it, I have a cute workaround involving adding a @function and an @if statement in the right places; it produces results that are very much like @import-once would produce. It's not perfect, but it does work. You can find my solution here: #156 |
@seanofw Can you please provide the use case of a file that is "designed to be imported only once"? |
+1 for @depend |
@chriseppstein: The best example I can give is a diamond-style dependency pattern, which occurs in larger software. So let's say you have these four files in a big system that's been well-factored into small units:
So "DropDownMenu" has the stylings for general, shared dropdown menus that appear when somebody clicks a dropdown button. "CarModelPicker" is the layout for a frequently-reused blob of markup that, say, lets a user choose a car by its year and make and model, and that markup contains dropdown menus. "ColorPicker" is the layout for a frequently-reused blob of markup that lets a user choose a color, and that markup also contains dropdown menus. Finally, the "FindACar" page displays both a model-picker and a color-picker to let a user find a car. See the problem? On the "FindACar" page, if you don't design "DropDownMenu" to be imported only once, you'll get the same dropdown-menu CSS generated twice --- two different places in the same resulting FindACar.css file. You don't need to use @Mixins or anything fancier than @import to demonstrate the problem. In a large system where the CSS has been heavily sliced-and-diced like this --- like our flagship application with 1.2 million lines of code in it --- the @imports of a single page can end up both broad and deep. It's possible for the same page to unknowingly import, say, "_Links.scss" twenty times, because from the page's perspective, it's listing out its dependencies, and each of those dependencies are listing theirs, and so on until you get to a few base styles that nearly everybody needs. For a small application, this isn't an issue anyone would notice; but for a big application like ours, avoiding redundant imports is absolutely critical. |
@seanofw I totally understand this pattern and complex structure. My own site is quite complex. but I don't understand why |
@chriseppstein: The answer is because of the potential for misuse. I work in a large software shop, and believe me, even among really smart, capable coders, if somebody can do it wrong, they will do it wrong. By making _DropDownMenu.scss the one to declare "You can only import me once," you don't give anybody the option of screwing it up and importing it six times; you can't do it wrong even if you want to. But if _CarModelPicker.scss and _ColorPicker.scss are the ones that are responsible for saying, "Only import DropDownMenu once," that provides two points for failure instead of one: It's two different places where somebody can --- and will --- screw it up. And in a large system, two points for failure quickly become twenty, and those twenty quickly become two hundred. Putting it inside _DropDownMenu.scss is a form of encapsulation, to use an OO term; _DropDownMenu.scss is then guarding itself against misuse. Another thing to consider: Is there ever a scenario when it makes sense for a single .scss file to be able to both be imported many times by some callers and only once by others? Is there any scenario where that's sane, desirable behavior? If the answer is no (and I think it's no), then the system should guard against it, because that then must be an accident --- undesirable behavior --- if it happens. |
Is |
This feature seems fundamental for having clean re-usable code in an architecture with inheritance. I wish I had it even for this very simple use case. Plus, I agree with @jacobrask, and think |
What about |
+1 for |
Being unable to do this in a large modular codebase is a bummer. We ship different combinations of "modules" to our primary web client that we do to secondary clients (widgets, gadgets, etc). When I also think allowing the callee to specify this behavior would be interesting, if not expected. |
The more I try to develop modular components in SASS the more this feature becomes necessary to not have to deal with the order of dependencies by hand in my Letting files So, +1 this a lot. |
Having to include I like I’m curious, what are some example use cases for importing files multiple times? I’ve worked on large and small projects and this was never something I or my team wanted. Would you consider changing the |
+1000 for this. |
I'm with @zakness, importing a file more than once is the odd scenario. Makes more sense to me going with changing |
+1 for this feature like |
@svallory except that |
+1 to @MoOx's suggestion: |
@Snugug That's kinda the motive behind my suggestion. You see, mixins can be included multiple times. It's already known how |
@svallory That's a dangerous, and in fact incorrect, assumption to make. Mixins absolutely do not need to be included inside a rule, and there are many many "setup" mixins that you in fact specifically do not include inside a rule. Take, for example, Compass' Moreover, I firmly believe that the mental model behind a mixin and a file are different enough that context alone isn't a good enough indicator alone of what you mean to do, especially in the case of files that include mixins of the same name. Take a CSS Normalize file as a for instance. The logical name of the file would probably I personally like @MoOx's |
The I'm more interested in seeing this happen than I am attached to any On Thursday, January 31, 2013, Snugug wrote:
|
@Snugug thank you for your comment. I missed that point. I have nothing more to say than I totally agree with you. |
Eh, I generally keep most of my placeholders on a mobile-first context, meaning they apply to all. I try not to complicate that part too much. |
Give it 10 years and we'll see native placeholders for CSS... guaranteed. |
Can you please elaborate further? |
hello all, this issue is quite old. I've just reported a duplicated of this #928 . |
xzyfer: I just mean that I try not to create placeholders that would need to be overwritten in MQs. brauliobo: read through for an import-once function. |
@TheJase: @import-once is integrated into the official code? |
pasting https://gist.github.com/courtsimas/1167053 before including anything isn't nice. this or another solution should be on the sass core code |
Why is it not nice? It is actually safer and more specific (you specify you should only include this once in the actual file). And no. No official support yet. But I actually feel this is safer than a native @import-once. — On Sat, Sep 28, 2013 at 9:10 AM, Bráulio Bhavamitra
|
it is not nice because it adds more than 10 lines of code in each sass file I need it. |
How would it be "safer"? The gist is clever, but it's a total hack in my opinion. For one, it requires you to wrap each file you want to be able to protect in an if, so you couldn't use it w/ any files you don't own, for another it requires you to litter your top level files w/ that function code. I'm holding out for a native version. |
@aaronjensen +1 for a native version. it is very needed. the sass developers are considering this?? |
Safer because your other devs don't need to remember to use an @import-once over and over (humans are flawed like that). And if you don't own the code, just wrap the check around the import instead. More specific, less magic. — On Sat, Sep 28, 2013 at 9:24 AM, Bráulio Bhavamitra
|
@brauliobo it's not a built in solution, but you can use SmartSass. It will use the SSS compiler you have installed but replace the importer by one which makes include import files only once |
@svallory why shouldn't it be built in? |
@brauliobo He's not saying it shouldn't. He's saying this is a possible solution in the meantime. |
@brauliobo It's not "built in" because it's not part of the sass compiler. But it's totally transparent for the apps using it. And I sure hope it will be made part of sass someday. |
@brauliobo : I do have a slight variation of the solution mentioned above — see https://gist.github.com/StefanoRausch/6744610 . I do import it only once at the very beginning —think framework here, no duplication in every Sass file — and from there on I only have to either wrap the code in discussion as content for the mixin or I can use the function, depending on the constraints set by the mixin. @import file -> @include once( "" ) { ... } OR @if( include-once( "" ) { ... } Still a workaround, but nice to have ;) |
See #156 too. |
Now available as a plugin: https://github.com/chriseppstein/compass/blob/master/import-once/README.md |
Three years... and counting! |
Tick, tock.....thought this would have been addressed by now, having read through the entire thread. Are @StefanoRausch & @courtsimas workarounds still the only viable solution to not have duplicated css when extending a selector from another (imported) file? |
Why is this still not fixed? Almost every language has some sort of import-once mechanism, but not sass. |
Because it's a 4.0 feature. |
Closing this in favor of #1094. |
We need a way to import library files just a single time. The
@import
statement is basically like ruby'sload
directive, but I think we need an import with ruby'srequire
semantics (does nothing if it's been imported already).What do you think about
@depend
?The text was updated successfully, but these errors were encountered: