Skip to content
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

Using multiple "@use ... with()" with different config on same library #1291

Closed
borisdamevin opened this issue Apr 21, 2021 · 9 comments
Closed

Comments

@borisdamevin
Copy link

borisdamevin commented Apr 21, 2021

Hi,

Is a way to call twice the same library with @use ... with() but with completely different config?

Example:

// component1.scss

@use 'library' with (
    $color: green
);

.foo {
    @include library.foo;
}
// component2.scss

@use 'library' with (
    $color: orange
);

.bar{
    @include library.foo;
}
// index.scss

@use 'component1';
@use 'component2';
@borisdamevin borisdamevin changed the title Using twice @use ... with() with different config Using multiple @use ... with() with different config Apr 21, 2021
@borisdamevin borisdamevin changed the title Using multiple @use ... with() with different config Using multiple @use ... with() with different config Apr 21, 2021
@borisdamevin borisdamevin changed the title Using multiple @use ... with() with different config Using multiple @use ... with() with different config Apr 21, 2021
@borisdamevin borisdamevin changed the title Using multiple @use ... with() with different config Using multiple "@use ... with()" with different config on same library Apr 21, 2021
@jathak
Copy link
Member

jathak commented Apr 26, 2021

No, this is not possible. One of the goals of the module system is to ensure that each module is only ever loaded once.

If you wish to load the same bit of code multiple times with different options, you should use mixins instead.

@jathak jathak closed this as completed Apr 26, 2021
@borisdamevin
Copy link
Author

borisdamevin commented Jul 5, 2021

Hi @jathak ,

Work good with linear dependencies tree, but what happen if more dpendencies we need use same with?

image

@borisdamevin
Copy link
Author

Hi @jathak 😃

Any idea about my last question?

Thank you by advance!

@jathak
Copy link
Member

jathak commented Aug 4, 2021

We generally recommend using @use with only from top-level stylesheets that aren't depended on by any other stylesheets. @use with is really only intended as a limited replacement for overriding !default variables in the module system. For more complex configuration, we recommend using mixins instead.

@borisdamevin
Copy link
Author

borisdamevin commented Nov 29, 2021

Hi @jathak,

So on my example, the top level is each sub-library or the only way is use config mixins everywhere?

The with option is more inlighted on documentation instead of mixin config, I sure I'm not alone to thing the with option is magical, but finaly not :(.

Maybe we need better official examples to how use each cases, because I'm lost when errors comes. :(

@jathak
Copy link
Member

jathak commented Dec 15, 2021

Yes, using mixins is the best way to go here. I've filed sass/sass-site#604 to improve our documentation here and add some examples of how to adapt code using !default variables to use mixins instead.

@borisdamevin
Copy link
Author

Right, thank you @jathak !

@borisdamevin
Copy link
Author

borisdamevin commented Mar 17, 2023

Hi @jathak

Now the next problem I see is that the config mixin is not scoped in the @use call. If we include some mixins from the top library after a @use call that uses config mixin, the new config is used inside the include mixin.

Is this the right behavior? If so, we need to understand that all code that is not overridden must be called before any config mixin call.

In this case, the code architecture can come very complicated to prevent any unvolunteer override.

@jathak
Copy link
Member

jathak commented Mar 20, 2023

In this case, you probably want to avoid any sort of global state and just have your entire configuration passed in a variable to a main mixin, which then calls any other mixins necessary with that configuration (rather than setting global variables in your main mixin and then reading it in the other mixins).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants