The @use feature of Sass
The new rules @use
/@forward
and the remove from @import
are indeed a really big impact to SASS. It leads to a complete new form to write sass. A try to make an easy explanation for the beginning to use the new technique:
(1) @use
works similar to @import
. It adds the code from a (config- or partial-)file or a module to your stylesheet.
(2) The difference is: SASS changes the scope of variables/mixins/functions from global (all imported files = one scope) to local files (variables are only valid in the actual file). If you need to use variables/mixins/functions from another (config- or partial-)file you need to 'include' them to the actual file first.
That means for your project(*):
//file ###> _config.scss
$columnWidth: 50%;
$projectColors: (
primary: red,
secondary: blue,
);
//file ###> _functions.scss
@use 'config' as * // --> to use config vars (here: without namespace)
@function getColor( $name ){
$color: map-get($projectColors, $name);
@return $color;
}
//file ###> _partial.scss
@use 'config' as * // --> use config vars (here: without namespace)
@use 'functions' as * // --> use functions (here: without namespace)
.class {
width: $width;
color: getColor( primary );
}
//file ###> myStylesheet.scss
// no need to @use 'config' or 'functions'
// as they are not direct needed in this file
@use 'partial' //--> write the css
---
( * ) Including files without using a namespace is a special case to make the example more easy. Normaly you will include variables/mixins/functions to a separated namespace and call them by namespace.$variable
or namespace.mixin
. And there are techniques to move special settings to a @used
file as well so you can move variable settings to the project. Please have a look to official and excelent description: https://sass-lang.com/documentation/at-rules/use
NOTES:
(1) As it is heavily discussed: Yes. This is INDEED the intended new way to work with SASS. (https://github.com/sass/sass/issues/2750)
(2) Very interesting: The actual brandnew version from Bootstrap has moved to the new Sass Version. But as I have seen Bootstrap does not use that new feature @use
and still works with @import
. That may have reasons ... and it seems not to easy to change the technique.
(3) Also it seems to be a little bit complicated there are some advantages comming with that new technique. Using separate namespaces make it much mor easier to work with external modules without causing name conflicts.
Live Sass Compiler - @use causes compilation error
I had same/similar problem two days ago.
@use is a new directive introduced in the new official Version 'Dart Sass' and replaces '@import' which is depricated now.
The popular Extension 'Live Sass Compiler' in VS Code is not longer supported by the maintainer for some time. So unfortunately the Sass Version is not updated in that extension...
In VS Code for 'Dart Sass' I found the the Extension 'DartJS Sass Compiler':
https://marketplace.visualstudio.com/items?itemName=codelios.dartsass
Actual I just did a quick testing so I cannot report much about it. But on the first glance it is running well. But as it is another extension you may have to change some settings.
UPDATE
Additional to the above reported exentsion DartJS Sass Compiler
with actual SASS version I found an actualised and actual maintained fork of Live Sass Compiler
. It has been deep hidden in the searching reuslts. As it is a very popular extension in VS Code here the link to the fork:
https://marketplace.visualstudio.com/items?itemName=glenn2223.live-sass
Remark:
I played arround with both extension. Both are doing the job well and have advantages.
'DartJS Sass Compiler' has the additional possibility for more detailed output like information about sass version and watchers. And additional to included SASS version it allows to use your own SASS version if installed to your system or locally to your project. So there is no/less dependency on updates by the maintainer.
'Live Sass Compiler' runs out of the box with included sass version. As it is a very popular extension in VS Code there is a special advantage: it seems it keeps your settings in the old projects running.
I think both are good solutions.
When importing a file with @use then: Error Undefined Mixin
That may be because the use of the new rule @use
has an high impact to the way you build your project structure. Using of @use
changes it dramatically:
With
@import
you add a file and the mixins/vars/modules are ready to use in every sass file you load later on.With
@use
you have to load the special sass file you need (i.e. '_mixins') direct in the file where you want to use it direct ... not to the main file. (Yes: loading the mixins/functions/variables-files to EVERY single partial file where you want to use them is the NEW INTENDED way to write sass.And one more point to your using of
@use
: If you load by@use 'mixins
you have to call the mixin in your file in the separated namespace@include mixins.yourMixinYouCall
. If you want to load the mixins without separated namespace you can do:@use 'mixins' as *
.
This changing to seperate namespace has some advantages. But indeed that is a really big impact and a very hard discussed theme about the new way to build your sass files.
We had this this week and before posting that twice you may have a look to this posting:
The @use feature of Sass
What's the difference between SCSS and Sass?
Sass is a CSS pre-processor with syntax advancements. Style sheets in the advanced syntax are processed by the program, and turned into regular CSS style sheets. However, they do not extend the CSS standard itself.
CSS variables are supported and can be utilized but not as well as pre-processor variables.
For the difference between SCSS and Sass, this text on the Sass documentation page should answer the question:
There are two syntaxes available for Sass. The first, known as
SCSS (Sassy CSS)
and used throughout this reference, is an extension of the syntax of CSS. This means that every valid CSS stylesheet is a valid SCSS file with the same meaning. This syntax is enhanced with the Sass features described below. Files using this syntax have the .scss extension.The second and older syntax, known as the
indented syntax (or sometimes just “Sass”)
, provides a more concise way of writing CSS. It uses indentation rather than brackets to indicate nesting of selectors, and newlines rather than semicolons to separate properties. Files using this syntax have the .sass extension.
However, all this works only with the Sass pre-compiler which in the end creates CSS. It is not an extension to the CSS standard itself.
Error when attempting to @use sass variables with node-sass
Update: I realized that I misunderstood the page on @use
and that it's only supported by Dart Sass right now, not LibSass, so @import
is not discouraged in this case.
Sass @use not loading partial
That's because you are not using Dart Sass which supports this feature.
Also in the documentation you will find:
Only Dart Sass currently supports @use. Users of other implementations must use the @import rule instead.
So use @import
until @use
is supported.
BTW, If you want to use Dart Sass, just use sass
instead of node-sass
and require it in the webpack config, like:
// ...
{
loader: 'sass-loader',
options: {
implementation: require('sass')
}
}
// ...
SASS - use variables across multiple files
This question was asked a long time ago so I thought I'd post an updated answer.
You should now avoid using @import
. Taken from the docs:
Sass will gradually phase it out over the next few years, and
eventually remove it from the language entirely. Prefer the @use rule
instead.
A full list of reasons can be found here
You should now use @use
as shown below:
_variables.scss
$text-colour: #262626;
_otherFile.scss
@use 'variables'; // Path to _variables.scss Notice how we don't include the underscore or file extension
body {
// namespace.$variable-name
// namespace is just the last component of its URL without a file extension
color: variables.$text-colour;
}
You can also create an alias for the namespace:
_otherFile.scss
@use 'variables' as v;
body {
// alias.$variable-name
color: v.$text-colour;
}
EDIT As pointed out by @und3rdg at the time of writing (November 2020) @use
is currently only available for Dart Sass and not LibSass (now deprecated) or Ruby Sass. See https://sass-lang.com/documentation/at-rules/use for the latest compatibility
Related Topics
Cross Browser Text Gradient in Pure CSS Without Using Background Image
What Is the Right Combination of Prefixes for CSS Transitions and Transforms
Resizing Background-Image Always Proportionally to Scale with a Center Point
CSS Drop Shadow for CSS Drawn Arrow
Can't Get Z-Index to Work for Div Inside of Another Div
Bootstrap Carousel as Website Background
Flexbox Misbehaving with Max-Height
IE7 CSS Inheritance Does Not Work
How to Override Z-Index Inheritance from Parent Element
Css, Background-Repeat Distance
Bullets Disappear with CSS3 Columns
Css3 Animate: How to Have the Object Not Revert to Its Initial Position After Animation Has Run