Sass/SCSS

Sass preprocessing with variables and mixins

Sass/SCSS

Packem compiles .scss and .sass files through the Sass loader in @visulima/rollup-plugin-css. It supports both Dart Sass (sass) and the faster sass-embedded implementation.

Overview

This example demonstrates:

  • Compiling SCSS with variables, mixins, and nesting
  • Registering the Sass loader in packem.config.ts
  • Configuring the Sass implementation and compiler options

Setup

The Sass loader requires a Sass implementation. Install either sass or sass-embedded.

npm install --save-dev sass
npm install --save-dev sass-embedded

Directory Structure

my-package/
├── src/
│   ├── index.ts
│   └── styles.scss
├── package.json
└── packem.config.ts

Source Files

src/styles.scss

$primary-color: #007bff;
$border-radius: 0.25rem;

@mixin button-style($bg-color) {
  background-color: $bg-color;
  color: white;
  padding: 0.5rem 1rem;
  border: none;
  border-radius: $border-radius;
  cursor: pointer;

  &:hover {
    background-color: darken($bg-color, 10%);
  }
}

.btn-primary {
  @include button-style($primary-color);
}

src/index.ts

import './styles.scss'

export function createButton() {
  const button = document.createElement('button')
  button.className = 'btn-primary'
  button.textContent = 'Click me'
  return button
}

Configuration

Register the Sass loader by importing it and adding it to rollup.css.loaders. The sass option accepts the Sass loader options, which extend the Sass compiler's StringOptions.

import { defineConfig } from '@visulima/packem/config'
import transformer from '@visulima/packem/transformer/esbuild'
import sassLoader from '@visulima/packem/css/loader/sass'

export default defineConfig({
  transformer,
  rollup: {
    css: {
      mode: 'extract',
      loaders: [sassLoader],
      sass: {
        implementation: 'sass', // or 'sass-embedded'
      },
    },
  },
})

Compiler Options

The sass option forwards options to the underlying Sass compiler. The loader also adds an additionalData hook for prepending shared variables or mixins to every file, an implementation switch, and warnRuleAsWarning.

css: {
  mode: 'extract',
  loaders: [sassLoader],
  sass: {
    implementation: 'sass-embedded',
    // Prepend shared variables/mixins to every entry file
    additionalData: `@use "src/styles/_variables.scss" as *;`,
    // Forwarded to the Sass compiler
    style: 'compressed',
  },
}

The charset and indentedSyntax options are managed by the loader and cannot be set directly. Use the .sass extension for indented syntax files.

Building

packem build
Support

Contribute to our work and keep us going

Community is the heart of open source. The success of our packages wouldn't be possible without the incredible contributions of users, testers, and developers who collaborate with us every day.Want to get involved? Here are some tips on how you can make a meaningful impact on our open source projects.

Ready to help us out?

Be sure to check out the package's contribution guidelines first. They'll walk you through the process on how to properly submit an issue or pull request to our repositories.

Submit a pull request

Found something to improve? Fork the repo, make your changes, and open a PR. We review every contribution and provide feedback to help you get merged.

Good first issues

Simple issues suited for people new to open source development, and often a good place to start working on a package.
View good first issues