When to use SCSS rule: @use and when to use @import? What is the difference between them and the best-case scenarios to use them?
@use '_styles.scss';
@import '_styles.scss';
Fundamentally both rules do the same thing - load members inside another module. The main differences is how they handle members. @import makes everything globally accessible in the target file.
Sass Importing Files Just like CSS, Sass also supports the @import directive. The @import directive allows you to include the content of one file in another. The CSS @import directive has a major drawback due to performance issues; it creates an extra HTTP request each time you call it.
The @mixin directive lets you create CSS code that is to be reused throughout the website. The @include directive is created to let you use (include) the mixin.
It is now possible to import css files directly into your sass file. The following PR in github solves the issue. The syntax is the same as now - @import "your/path/to/the/file" , without an extension after the file name. This will import your file directly.
The new
@use
is similar to@import
. but has some notable differences:
- The file is only imported once, no matter how many times you
@use
it in a project.- Variables, mixins, and functions (what Sass calls "members") that start with an underscore (_) or hyphen (-) are considered private, and not imported.
- Members from the used file (buttons.scss in this case) are only made available locally, but not passed along to future imports.
- Similarly,
@extends
will only apply up the chain; extending selectors in imported files, but not extending files that import this one.- All imported members are namespaced by default.
https://css-tricks.com/introducing-sass-modules/
@import
will be deprecated in favor of @use
and @forward
, and support will be dropped by October 2022 at the latest.
July 2022 update:
In light of the fact that LibSass was deprecated before ever adding support for the new module system, the timeline for deprecating and removing @import has been pushed back. We now intend to wait until 80% of users are using Dart Sass (measured by npm downloads) before deprecating @import, and wait at least a year after that and likely more before removing it entirely.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With