Skip to main content
Drupal is a registered trademark of Dries Buytaert
Release: OpenID Connect / OAuth client 3.0.0-alpha9 New alpha version released for module openid_connect (3.0.0-alpha9). Usage Milestone: Google Analytics Module google_analytics crossed 1,000 active installs. Release: GraphQL Compose Codegen 1.1.2 Minor update available for module graphql_compose_codegen (1.1.2). Release: Mapy.com 1.1.3 Minor update available for module mapycom (1.1.3). Release: Ckeditor5 entity browser 3.0.3 Minor update available for module ckeditor5_entity_browser (3.0.3). Release: Ckeditor5 entity browser 3.0.1 Minor update available for module ckeditor5_entity_browser (3.0.1). Release: Ckeditor5 entity browser 3.0.2 Minor update available for module ckeditor5_entity_browser (3.0.2). Release: Teamleader Integration 4.0.2 Minor update available for module teamleader (4.0.2). Release: Change Requests 2.1.9 Minor update available for module change_requests (2.1.9). Module Revived: Entityqueue Buttons 1.1.2 Module entityqueue_buttons updated after 8 months of inactivity (1.1.2).

SCSS Compiler

685 sites Security covered Drupal 10–11
View on drupal.org

This module provides a runtime SCSS compiler for Drupal using scssphp. It allows developers to dynamically compile SCSS stylesheets based on site configuration, enabling custom themes or modules to generate CSS on the fly.

Introduction

This module provides an SCSS compiler plugin powered by scssphp to enable runtime SCSS compilation within Drupal. It is intended for developers building custom modules or themes who need dynamic stylesheets that are directly influenced by the site's configuration.

This module can be used with Theme Compiler to define dynamic assets in your themes. Check its project page for more information. You may also want to check the Compiler ecosystem for other creative applications for this module.

Usage

A typical development workflow using this module consists of the following steps:

  1. Building configuration forms that utilize the schema types and form elements provided by this module.
  2. Creating and configuring an SCSS compiler plugin instance.
  3. Compiling an asset and storing it on disk (e.g., the public:// file system).
  4. Using the compiled asset (e.g., in a library definition).

The sections below will describe the available configuration schema types and form elements, and the basic process for using the compiler plugin.

Configuration schema

compiler_scss_color

A mapping with the following structure:

  • red: The red channel value (0-255) of the color; integer.
  • green: The green channel value (0-255) of the color; integer.
  • blue: The blue channel value (0-255) of the color; integer.
  • alpha: The alpha channel value (0-1) of the color; float, optional.

If the alpha channel is not supplied, it defaults to fully opaque.

compiler_scss_font_family

A sequence of font family names, or generic font family keywords (string).

compiler_scss_font_weight

An integer value from 1 to 1,000.

compiler_scss_number

A mapping with the following structure:

  • value: The magnitude of the number; float.
  • unit: The unit for the number; string enumeration.

See the schema definition for a list of supported units.

Form elements

compiler_scss_color

A custom element that supports setting color values.

Supported render array keys:

  • #default_value: Sets the default value of the form element. Must be in the same format as the compiler_scss_color schema type.
  • #description: Sets the description of the element; string or translatable markup.
  • #required: Sets whether a value is required for submission; boolean.
  • #title: Sets the title of the element; string or translatable markup.

This element does not support alpha channel values. This is largely because browsers lack support for this in their native inputs.

compiler_scss_font_family

A custom element that extends the textarea element, and supports setting a list of font family names (one per line).

compiler_scss_font_weight

A custom element that extends the number element, overriding the following render array keys:

  • #max: 1,000
  • #min: 1
  • #step: 1

Additionally, the submitted values will be parsed into an integer in this range, or fall back to NULL on failure.

compiler_scss_number

A custom element that supports setting single-unit number values.

Supported render array keys:

  • #default_value: Sets the default value of the form element. Must be in the same format as the compiler_scss_number schema type.
  • #description: Sets the description of the element; string or translatable markup.
  • #required: Sets whether a value is required for submission; boolean.
  • #title: Sets the title of the element; string or translatable markup.
  • #units: Sets the list of units available as options for the element; must be a UnitGroup, Unit, or a list of Unit values; required key.

Compiler plugin

You can get an instance of the plugin provided by this module as follows:

/** @var \Drupal\compiler\Plugin\CompilerPluginManagerInterface */
$compiler_plugin_manager = \Drupal::service('plugin.manager.compiler');
/** @var \Drupal\compiler_scss\Plugin\Compiler\ScssInterface */
$compiler = $compiler_plugin_manager->createInstance('scss');

// @todo Configure the compiler with injected variables, functions, etc...

You can compile SCSS source code as follows:

// Choose the appropriate input type based on how you want to feed source
// code to the compiler plugin.
$file = new \Drupal\compiler\CompilerInputFile('/path/to/source.scss');
$source = new \Drupal\compiler\CompilerInputSource('$text: "Hello, world!" !default; .style { content: $text; }');

// The compiler will return a string containing the compilation result, or
// throw an exception upon failure.
$result = $compiler->compile($file);
echo $result . \PHP_EOL;

$result = $compiler->compile($source);
echo $result . \PHP_EOL;

// @todo Store these assets for later use.

The SCSS compiler plugin offers several interface methods that can be used to configure the compilation process. These methods are summarized below.

Deprecation handling

The following methods allow you to configure how deprecation warnings will be handled:

  • ScssInterface::setFatalDeprecations(Deprecation ...$deprecations): void: Set the list of deprecations that should be considered fatal.
  • ScssInterface::setFutureDeprecations(Deprecation ...$deprecations): void: Set the list of deprecations to opt into early warnings.
  • ScssInterface::setSilenceDeprecations(Deprecation ...$deprecations): void: Set the list of deprecations to silence.
  • ScssInterface::setQuietDeps(bool $value): void: Set whether to silence deprecation warnings for imported stylesheets.

Variable / function injection

The following methods allow you to manage variables and host functions that your source code can leverage during compilation:

  • ScssInterface::setFunction(string $identifier, \Closure $callback, array $arguments = [], bool $overwrite = FALSE): string: Set a user-defined host function to make available to the compiler.
  • ScssInterface::setVariable(string $identifier, mixed $value, bool $overwrite = FALSE): string: Set a variable to inject into the compiler.
  • ScssInterface::unsetFunction(string $identifier): void: Unset the function with the supplied identifier.
  • ScssInterface::unsetVariable(string $identifier): void: Unset the variable with the supplied identifier.

A value conversion process is applied to all values provided to the compiler, including injected variables and function return values. Atomic values, basic arrays, plain data objects, and values that conform to the schema types provided by this module are automatically converted into their corresponding SCSS representations.

Miscellaneous

  • ScssInterface::setImportPaths(string ...$paths): void: Set the import paths that the compiler should use during compilation.
  • ScssInterface::setLogger(LoggerInterface $logger): void: Set the logger to use during compilation.
  • ScssInterface::setOutputStyle(OutputStyle $style): void: Set the output style to use for the compiled result.

Depends on

Dependencies of the latest stable release

Required by

Tracked projects that depend on this one

No tracked projects depend on this one yet.

Activity

Tracked releases
10
Tracked since
Oct 2024
Latest release
6 months ago
Releases (12 mo)
4 ▼ from 6
Maintenance
Slowing

Release Timeline

Releases

Version Type Core Release date
2.0.0 Stable 10–11 Feb 17, 2026
2.1.x-dev Dev 10–11 Feb 17, 2026
2.0.x-dev Dev 10–11 Oct 16, 2025
1.0.0-alpha12 Pre-release 10–11 Oct 14, 2025
1.0.0-alpha11 Pre-release 10–11 Feb 10, 2025
1.0.0-alpha10 Pre-release 10–11 Oct 9, 2024
1.0.0-alpha9 Pre-release 10–11 Oct 9, 2024
1.0.0-alpha8 Pre-release 10–11 Oct 9, 2024
1.0.0-alpha7 Pre-release 10–11 Oct 9, 2024
1.0.0-alpha6 Pre-release 10–11 Oct 9, 2024