Sub topic 16
Moving from @import to modules
This chapter assumes you know variables, mixins, functions and partials, and that you have read the chapter on @use and @forward. It covers why @import is being retired and how to convert a codebase one file at a time.
Why @import is being replaced
@import pastes the target file into the current scope. Every variable, mixin and function from every imported file becomes global. This causes three problems:
- Name collisions. Two files can both define
$space, and the later one silently wins. - No origin. In
padding: $space, nothing shows which file defined$space. - Repeated output. A file imported from several places has its CSS emitted each time.
Dart Sass has deprecated @import and will remove it in a future major version. @use fixes all three problems. A file loads once, and its members live in a namespace named after the file.
Converting a file by hand
Here is a file written with @import:
// main.scss (old)
@import 'tokens';
@import 'mixins';
.box {
padding: $space * 2;
@include respond(48rem) {
display: flex;
}
}Here is the same file with @use:
// main.scss (new)
@use 'tokens';
@use 'mixins';
.box {
padding: tokens.$space * 2;
@include mixins.respond(48rem) {
display: flex;
}
}Each member now carries the namespace of its file. @use 'tokens' creates the namespace tokens. Add as to rename it, or as * to drop the prefix. Use as * rarely, because it brings back the origin problem.
@use 'tokens' as t;
@use 'mixins' as *;Configuring modules
With @import, you changed a library default by defining a variable before the import. With modules, you pass the value through with. The variable must be declared !default in the module.
// _tokens.scss
$space: 8px !default;
// main.scss
@use 'tokens' with ($space: 4px);A module can be configured only once, in the first @use that loads it. A later with on the same module is an error.
Migrating a whole project
Convert leaf files first (tokens, functions), then the files that depend on them. If other files still use @import, bridge the gap with @forward. A file you @import can forward its members from an index partial.
// abstracts/_index.scss
@forward 'tokens';
@forward 'mixins';Then @use 'abstracts' gives access to both modules' members under one namespace.
The official migrator automates most of this:
npm install -g sass-migrator
sass-migrator module --migrate-deps main.scssIt rewrites @import to @use and adds namespaces. Review the diff, because it can leave as * in places where you may want a real namespace.
Common errors
| Error message or symptom | Cause | Fix |
|---|---|---|
| `Undefined variable` | The namespace prefix is missing. | Write `tokens.$space`. |
| `@use rules must be written before any other rules` | An `@use` appears after a style rule. | Move all `@use` lines to the top. |
| `This variable was not declared with !default` | `with` targets a variable that is not configurable. | Add `!default` in the module. |
| Members from `_theme.scss` are missing | `@use` does not re-export. | Use `@forward` in the index. |
Exercise
A file _buttons.scss uses $primary, which main.scss used to get from @import 'tokens'. You switch main.scss to @use 'tokens', and the build reports an undefined variable inside _buttons.scss. Why, and how do you fix it?
Show answer
Modules do not share scope, so _buttons.scss cannot see members that main.scss loaded. Add @use 'tokens'; to _buttons.scss and write tokens.$primary.