Sub topic 13
Splitting code with partials, @use and @forward
This chapter assumes you can write SCSS variables and mixins, and that you can run the Dart Sass command line from the earlier chapters. It covers how to split a stylesheet into files and connect them with the module system.
Partials
A partial is a Sass file whose name starts with an underscore, such as _tokens.scss. Sass does not compile a partial into its own CSS file. It only compiles the partial when another file loads it. You leave the underscore and the extension out when you load it: @use 'tokens' finds _tokens.scss.
Loading a file with @use
@use loads a file as a module. Everything the module defines (variables, mixins, functions) is reached through a namespace. By default the namespace is the file name without the underscore.
// _tokens.scss
$space: 8px;
@mixin stack($gap: $space) {
display: grid;
gap: $gap;
}
// main.scss
@use 'tokens';
.box {
padding: tokens.$space * 2;
@include tokens.stack;
}Compiled with sass main.scss main.css, this produces:
.box {
padding: 16px;
display: grid;
gap: 8px;
}Three rules apply to @use:
- It must come before any rule other than
@forward(and variable declarations that set up configuration). - The module's CSS is included once, however many files load it.
- Members whose names start with
-or_are private and cannot be reached from outside the module.
You can change the namespace with as, or remove it with as *.
@use 'tokens' as t;
@use 'tokens' as *;Use as * sparingly. It brings back the name collisions that namespaces exist to prevent.
Configuring a module
A variable marked !default can be overridden when the module is loaded, using with.
// _theme.scss
$primary: #3366cc !default;
// main.scss
@use 'theme' with ($primary: #aa3366);A module can be configured only once, in the first @use that loads it. A later @use ... with for the same module is an error.
Re-exporting with @forward
@forward loads a module and makes its members available to whoever loads your file. It does not make them available inside your own file. Use it to build one entry point for a folder.
// abstracts/_index.scss
@forward 'tokens';
@forward 'mixins';
// main.scss
@use 'abstracts';
.box {
padding: abstracts.$space;
}Loading a folder works because Sass looks for _index.scss or index.scss inside it.
You can add a prefix to avoid collisions, or limit what is exposed:
@forward 'tokens' as token-*;
@forward 'mixins' hide internal-helper;With the prefix, $space becomes abstracts.$token-space.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| `There is no module with the namespace "tokens"` | The file was never loaded with `@use`, or it was loaded under another name | Add `@use 'tokens'` or use the `as` name |
| `Undefined variable` on a forwarded variable | `@forward` does not give access in the forwarding file | Add a `@use` in the file that needs it |
| `@use rules must be written before any other rules` | A style rule appears above `@use` | Move `@use` to the top |
| `This variable was not declared with !default` | `with` targets a variable that lacks `!default` | Add `!default` in the module |
| `This module was already loaded` | A second `@use ... with` configures the same module | Configure it only at the first load |
Exercise
Your file components/_index.scss contains @forward 'button';. In main.scss you write @use 'components'; and call @include components.button-style;. Inside components/_index.scss, you also try to call @include button-style; and get an error. Why, and how do you fix it?
Show answer
@forward re-exports members to files that load the index, but it does not bring them into the index file's own scope. In the index file, add @use 'button'; and call @include button.button-style;. The call in main.scss is fine as written.