Performance: @use directive for imports
This affects CSS build time and runtime style calculation by managing how stylesheets are imported and scoped.
@use 'variables' as vars; .button { color: vars.$primary-color; }
@import 'variables'; @import 'variables'; .button { color: $primary-color; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| @import repeated | N/A | N/A | High due to large CSS | [X] Bad |
| @use once with namespace | N/A | N/A | Lower due to smaller CSS | [OK] Good |
@use directive in Sass?@use@use directive imports Sass files and applies a namespace to their contents.@import, @use avoids naming conflicts by requiring namespace prefixes.@use adds namespace = C [OK]@use always adds a namespace [OK]@use copies styles without namespace@use with JavaScript execution@use exports variablescolors.scss using @use?@use directive requires the file path as a string without the extension, usually quoted.@use [OK]@useh1 text?@use 'colors';
h1 {
color: colors.$primary;
}// colors.scss $primary: blue; $secondary: green;
@use 'colors'; imports variables with the namespace colors. The variable $primary is set to blue.h1colors.$primary, which refers to blue.@use 'theme';
h1 {
color: $main-color;
}// theme.scss $main-color: red;
@use namespace behavioras * is used to remove it.@use 'theme'; without as *, so variables must be accessed with the theme. prefix, but $main-color is used without it, causing an undefined variable error.as *, the prefix is required, so Variables must be accessed with namespace prefix unless configured identifies the issue.as * to access variables directly [OK]as * is invalid syntaxas *@usevariables.scss but rename its namespace to vars and override the variable $font-size to 1.5rem. Which @use syntax is correct?@use syntax for renaming and configuring@use 'file' as namespace with (variable overrides);.as then with for renaming and overrides [OK]as and with orderrenamePass the test and this pattern moves unknown