Sub topic 13
Splitting code with partials, @use and @forward
This chapter covers how to split a stylesheet into files and connect them with Dart Sass's module system. It assumes you can write variables, mixins and functions, and that you can compile a file with the sass command. The module system replaces @import and is the current way to share code between Sass files.
Partials: files that are not compiled on their own
A partial is a Sass file whose name starts with an underscore, such as _tokens.scss. The compiler does not produce a CSS file for a partial. It is meant to be loaded by another file. When you load a partial, you leave out both the underscore and the extension.
src/
main.scss
abstracts/
_index.scss
_tokens.scss
_mixins.scssIf you ran sass src/abstracts/ on this folder, only main.scss would be a compile target in a project that lists its entry files explicitly. The underscore files are skipped when you compile a whole directory.
@use: loading a module
@use loads another Sass file as a module. The module's variables, mixins and functions become available through a namespace. By default the namespace is the last part of the URL, without extension or underscore.
// src/abstracts/_tokens.scss
@use 'sass:color';
$space: 8px !default;
$primary: #3366cc !default;
$_shade-step: 10%;
@function shade($color) {
@return color.mix(black, $color, $_shade-step);
}Three details in this file matter.
!defaultmarks a variable as configurable. A file that loads this module can override the value. Without!default, the value is fixed.- A name that starts with
_or-, like$_shade-step, is private. Other files cannot see it, even though the module's own functions can use it. @use 'sass:color'loads a built-in module with the same syntax as your own files.
Another file loads the module and reads members through the namespace:
// src/abstracts/_mixins.scss
@use 'tokens';
@mixin stack($gap: tokens.$space) {
display: flex;
flex-direction: column;
gap: $gap;
}Variables use namespace.$name. Mixins and functions use namespace.name, and @include tokens.something for mixins. Because each file names where its members come from, a name like $space in one file can never clash with a $space in another.
Properties of @use
- Each module is evaluated once. If ten files
@use 'tokens', its CSS output appears once. @userules must come first in the file. Only@forwardrules and variable declarations may appear before them.- You can change the namespace with
as:@use 'tokens' as t;givest.$space. @use 'tokens' as *;puts members in the global scope with no prefix. Use it only for a file you own and whose names are unlikely to collide.- Members are not shared transitively. If
_mixins.scssloadstokens, a file that loadsmixinsdoes not gettokenstoo. It must loadtokensitself, or receive it through@forward.
Configuring a module with `with`
A module's !default variables can be set at the point where you load it:
@use 'tokens' with ($primary: #cc3333, $space: 4px);Configuration applies only the first time the module is loaded. If another file has already loaded tokens without configuration, a later @use ... with (...) raises an error, because the module has already been evaluated. Put configuration in the entry file, so it is the first to load the module. Only variables declared with !default can be configured.
@forward: re-exporting a module
@use makes a module's members available to the file that loads it. @forward passes them on to whoever loads that file. This lets you build a single entry point for a folder. A file named _index.scss (or _index.sass) is loaded when someone writes the folder name.
// src/abstracts/_index.scss
@forward 'tokens' show $space, $primary, shade;
@forward 'mixins';@forward does not make the members available inside _index.scss itself. If that file also needs tokens, it needs its own @use 'tokens'.
Now the entry file loads the whole folder:
// src/main.scss
@use 'abstracts' with ($primary: #cc3333);
.sidebar {
@include abstracts.stack;
padding: abstracts.$space * 2;
border-color: abstracts.shade(abstracts.$primary);
}Compile it with sass src/main.scss dist/main.css. The output is:
.sidebar {
display: flex;
flex-direction: column;
gap: 8px;
padding: 16px;
border-color: #b82e2e;
}The with configuration travels through @forward to tokens, so $primary becomes #cc3333 there. This works because $primary is declared !default and is included in the show list.
Controlling what is exposed
@forward accepts options that shape the public surface of a folder.
| Option | Example | Effect |
|---|---|---|
| `show` | `@forward 'tokens' show $space;` | Only the listed members are forwarded. |
| `hide` | `@forward 'tokens' hide $primary;` | Everything except the listed members is forwarded. |
| `as prefix-*` | `@forward 'mixins' as mx-*;` | Every member name gets a prefix, so `stack` becomes `mx-stack`. |
Prefixes are useful when two forwarded files define members with the same name. Without them, forwarding both would be an error. Variables in show and hide lists include the $, while mixin and function names do not.
How @use differs from @import
| Behavior | @import | @use |
|---|---|---|
| Member names | Global, can collide | Namespaced per module |
| Repeated loads | File is evaluated each time | File is evaluated once |
| Private members | Not supported | Names starting with `_` or `-` |
| Configuration | Set variables before the import | `with (...)` and `!default` |
The full migration from @import is covered in a later chapter. For now, do not mix the two styles in new code.
Common errors
- "There is no module with the namespace X." The namespace comes from the file name.
@use 'abstracts/tokens'givestokens, notabstracts. Check the last path segment or addas. - "Undefined variable." The variable was loaded in a different file. Each file needs its own
@usefor every module whose members it names, unless it receives them through@forward. - "This variable was not declared with !default." You passed it in
with (...), but the module declares it as fixed. Add!defaultin the module. - "This module was already loaded, so it can't be configured." An earlier
@useevaluated the module first. Move the configured@useinto the entry file, before other files that load the module. - Forwarded name missing. A
showorhidelist left it out, or a prefix renamed it. Check the@forwardline. - Private member access.
tokens.$_shade-stepis a compile error from outside the module. Expose a public variable or function if other files need the value.
Exercise
A file _index.scss contains @forward 'tokens' hide $space; and @forward 'mixins' as mx-*;. The mixins file defines @mixin stack. A file main.scss has @use 'abstracts';. Write the line that includes the stack mixin, and say whether abstracts.$space compiles.
Show answer
The include is @include abstracts.mx-stack;, because the prefix mx- is added to every forwarded member of mixins. abstracts.$space does not compile, because hide $space removes that variable from the forwarded members, so Sass reports an undefined variable.