Sub topic 15
Organizing a Sass project
This chapter assumes you can write mixins, functions and maps, and that you know how @use and @forward load a partial as a module. Here you will combine those pieces into a folder layout that stays readable as the stylesheet grows.
Why structure matters
One large .scss file works for a demo. In a real project it becomes hard to search, and a change to a color can affect rules you did not expect. Splitting the code by purpose gives each file one job and gives each team member an obvious place to look.
A common approach is a simplified version of the "7-1 pattern": several folders of partials and one entry file.
src/
main.scss
abstracts/
_index.scss
_tokens.scss
_mixins.scss
base/
_index.scss
_reset.scss
components/
_index.scss
_button.scssThe folders have these roles:
| Folder | Contents | Emits CSS? |
|---|---|---|
| `abstracts` | Variables, maps, mixins, functions | No |
| `base` | Resets and element defaults | Yes |
| `components` | One partial per UI component | Yes |
Only abstracts is safe to load from anywhere, because it produces no output. That is why it holds the shared tools.
Index files as folder entry points
A file named _index.scss is loaded when you write @use with the folder name. Each folder's index uses @forward to republish its partials, so callers load the folder, not each file.
// src/abstracts/_index.scss
@forward 'tokens';
@forward 'mixins';// src/abstracts/_tokens.scss
$primary: #3366cc;
$space: 0.5rem;// src/abstracts/_mixins.scss
@mixin respond($min) {
@media (min-width: $min) {
@content;
}
}Using the shared tools in a component
A component loads abstracts with a short namespace and reads members through it.
// src/components/_button.scss
@use '../abstracts' as a;
.btn {
padding: a.$space * 2;
background: a.$primary;
@include a.respond(48rem) {
padding: a.$space * 3;
}
}The remaining files are small:
// src/components/_index.scss
@forward 'button';// src/base/_index.scss
@forward 'reset';// src/base/_reset.scss
*,
*::before,
*::after {
box-sizing: border-box;
}// src/main.scss
@use 'base';
@use 'components';Compile the entry file only. Partials start with an underscore so that Sass does not compile them on their own.
sass src/main.scss dist/main.cssRules that keep the structure healthy
- Keep the dependency direction one way. Components may load
abstracts, butabstractsshould never load components. - Load
basebeforecomponentsinmain.scss, because the order of@userules sets the order of the CSS, and later rules win ties in the cascade. - Give each component its own partial, named after the component.
- Put
@extendplaceholders inabstractsonly if every user needs them. Otherwise keep them in the file that uses them, so the extended selectors stay close together.
Growing the layout
When the project grows, add folders such as layout for page grids or utilities for classes generated from maps. Follow the same pattern each time: partials, an _index.scss that forwards them, and one @use line in main.scss.
Exercise
You add components/_card.scss that begins with @use '../abstracts' as a;. What two changes make the card styles appear in dist/main.css, and why is main.scss itself left unchanged?
Show answer
Add @forward 'card'; to components/_index.scss, then recompile with sass src/main.scss dist/main.css. main.scss already loads components, which forwards every partial listed in its index, so the new file is reached through the existing @use 'components'; line.