SyntaxStudy
Sign Up
SASS / SCSS Namespaced Imports, sass:meta, and Modern Best Practices
SASS / SCSS Beginner 1 min read

Namespaced Imports, sass:meta, and Modern Best Practices

Modern SASS development is built around the `@use` and `@forward` module system, which replaced the legacy `@import` directive. Every file declares its dependencies explicitly, variables and mixins are namespaced, and each file is compiled at most once regardless of how many other files reference it. This eliminates the duplicate-output bugs, global namespace pollution, and unpredictable load-order issues that plagued large `@import`-based codebases. The `sass:meta` module provides meta-programming capabilities. `meta.type-of($value)` returns the type of any SASS value as a string (number, string, color, list, map, bool, null, function). `meta.inspect($value)` converts any value to its string representation, useful for debugging. `meta.load-css($url)` dynamically loads a SASS file as plain CSS (useful in mixins). `meta.get-function($name)` retrieves a function by name so it can be passed as an argument. Best practices for modern SASS: use `@use` everywhere, organise files with an `abstracts/` directory and barrel `_index.scss`, keep files under 300 lines, use `@forward` with `show`/`hide` to control public APIs, prefix private members with a hyphen, use `@error` for invalid inputs, `@warn` for deprecations, and `@debug` for development-time inspection.
Example
// ── Modern SASS project: abstracts/_index.scss (barrel) ───────────────────────

@forward 'variables';
@forward 'functions';
@forward 'mixins';
@forward 'breakpoints' as bp-*;

// ── sass:meta for type checking and introspection ─────────────────────────────

@use 'sass:meta';
@use 'sass:list';

// Type-safe color function
@function ensure-color($value, $name: 'value') {
    @if meta.type-of($value) != 'color' {
        @error "#{$name} must be a color, got #{meta.type-of($value)}: #{meta.inspect($value)}.";
    }
    @return $value;
}

// Function passed as a first-class argument
@function apply($fn, $value) {
    @return meta.call($fn, $value);
}

$double: meta.get-function('str-length', $module: 'sass:string');

// ── @error / @warn / @debug ───────────────────────────────────────────────────

@mixin grid($columns) {
    @if meta.type-of($columns) != 'number' or not math.is-unitless($columns) {
        @error "grid() expects a unitless number, got `#{$columns}`.";
    }
    @if $columns > 24 {
        @warn "#{$columns} columns is unusually large; consider 12 or fewer.";
    }
    @debug "Generating #{$columns}-column grid";  // only shown in dev mode

    display              : grid;
    grid-template-columns: repeat($columns, 1fr);
}

// ── Namespace aliasing for readability ────────────────────────────────────────

@use 'sass:color'  as c;
@use 'sass:math'   as m;
@use 'abstracts'   as a;

.hero {
    background: c.scale(a.$color-primary, $lightness: -10%);
    font-size : m.div(36px, 16px) * 1rem;  // 2.25rem
    padding   : a.$space-xl a.$space-lg;
    max-width : 80ch;
    margin    : 0 auto;
}

This is the last lesson in this section.

Create a free account to earn a certificate