Performance: @import to @use migration
This affects CSS build time and runtime style calculation by improving modularity and reducing redundant style processing.
Jump into concepts and practice - no test required
@use 'variables' as *; @use 'mixins';
@import 'variables'; @import 'mixins'; @import 'variables';
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| @import repeated modules | N/A | N/A | Higher due to larger CSS | [X] Bad |
| @use single module import | N/A | N/A | Lower due to smaller CSS | [OK] Good |
@use instead of @import in Sass?@import behavior@import loads files multiple times and merges all variables globally, which can cause conflicts.@use improvements@use loads files only once and requires a namespace prefix, preventing variable and mixin conflicts.@use = safer, namespaced imports [OK]@use loads once with namespaces [OK]@use imports CSS files directly@use runs JavaScript@use compiles Sass automatically@import 'colors'; with @use and a namespace c?@use syntax@use 'filename' as namespace; with quotes around filename and as keyword.@use 'colors' as c;.@use syntax = @use 'colors' as c; [OK]// _variables.scss
$primary-color: blue;
// styles.scss
@use 'variables' as vars;
.button {
color: vars.$primary-color;
}@use$primary-color is accessed with the namespace prefix vars.$primary-color._variables.scss, $primary-color is set to blue, so the button color will be blue.@import to @use?@use 'mixins';
.button {
@include border-radius(5px);
}@use namespace requirement@use, all variables and mixins must be accessed with the namespace prefix unless configured otherwise.@include border-radius(5px); without prefix. It should be @include mixins.border-radius(5px);.border-radius mixin. -> Option C@use requires namespace prefix [OK]@use@include is disallowed// _colors.scss
$primary: red;
// _theme.scss
@use 'colors' as c;
$primary: blue !default;
// styles.scss
@use 'theme' as t;
.button {
color: t.$primary;
}!default behavior!default flag sets a variable only if it is not already set.$primary in colors is red. In theme, $primary is set to blue !default, so it will only be set if $primary was not already set.theme uses colors as c, so $primary from colors is already set. Therefore, blue !default does not override red.$primary in colors overrides !default in theme. -> Option D!default only sets if variable is unset, so red stays [OK]!default sets only if variable is unset [OK]!default always overrides