question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't @use mixin, but can @import

See original GitHub issue

I have a top-level file that only has:

@mixin bg_img($path) {
  background-image: url("http://localhost:4102/app/ui/" + $path);
}

If I @import it, everything is fine. If I @use it, then it doesn’t come through. For example, this fails, but if I change the @use to @import, it succeeds:

@use 'mixins';

.bg {
    @include bg_img("photo.jpg");
}

Side point: the file is minimal on purpose - I plan to rewrite it just before running sass so that the string can be replaced (depending on whether it’s in production or dev environment). Is there a better way to do that?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
Halfistcommented, Dec 16, 2020

I’m willing to migrate from deprecated @import to @use. I’m sorry if this shouldn’t be posted here, and I’m also sorry if an answer is already given somewhere, but I tried really hard to find it over the internet.

So, the question is basically how can I migrate from

.my-theme {
  @import 'variables';
  @import 'global';
  @import 'components';
}

to

.my-theme {
  @use 'variables';
  @use 'global';
  @use 'components';
}

in Angular 10 app.

The second results in “SassError: This at-rule is not allowed here.”.

The point is that styles in imported stylesheets shouldn’t leak outside of .my-theme.

My imported stylesheets aren’t mixins, functions or variables. They’re just a bunch of CSS rule-sets. Some of them however could contain varibles, but this is secondary.

1reaction
jathakcommented, Dec 16, 2020

One note: @import is not deprecated yet. It will be in the future, but at the moment it’s still actively supported. You mention that you’re using Angular, which hasn’t yet migrated to the module system (they’re working on it right now), and we recommend waiting to migrate until all of your dependencies have migrated.

When you do migrate, provided you don’t need access to any Sass members, you can use the meta.load-css mixin, which loads the CSS rules from a stylesheet without making variables, mixins, or functions accessible. Unlike @use, it can be used anywhere.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SCSS switching from @import to @use: undefined mixin
As I understand the new ruls @use the way, that you have to @use the needed files with the needed mixin to EVERY...
Read more >
How to import Sass/SCSS mixins global? - DEV Community ‍ ‍
I import the two .scss files in the App.js so I can access the classes/mixins/variables global. But only the classes are accessible.
Read more >
mixin and @include - Sass
Mixins allow you to define styles that can be re-used throughout your stylesheet. They make it easy to avoid using non-semantic classes like...
Read more >
Sass - Importing files and partials - TutorialsTeacher
But with a few exceptions, you can also nest @import statements. You can't use nested imports within mixins (discussed later in next chapter)...
Read more >
Features In-Depth | Less.js
Mixins copy all of the properties into a selector, which can lead to unnecessary duplication. Therefore you can use extends instead of mixins...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found