Skip to content

Extending

hook_scss_preprocess_alter()

Alter the SCSS source of a file before it is compiled. The hook is invoked for each compiled (non-partial) file, first for the active theme and its base themes, in THEME.theme, then for modules, in MODULE.module. Partials loaded by an import rule are not passed through the hook; alter the file that imports them.

/**
 * Implements hook_scss_preprocess_alter().
 */
function mytheme_scss_preprocess_alter(string &$stylesheet, string $path) {
  // Make a value from configuration available as a Sass variable.
  if (str_ends_with($path, '/style.scss')) {
    $color = \Drupal::config('mytheme.settings')->get('brand_color') ?? '#0678be';
    $stylesheet = '$brand-color: ' . $color . ";\n" . $stylesheet;
  }
}
$stylesheet
The file's SCSS source, by reference.
$path
The file's path relative to the Drupal root, for example themes/custom/mytheme/scss/style.scss.

The hook is documented in scss.api.php.

Services

scss.compiler (Drupal\scss\Services\ScssCompilerInterface)

Compiles the configured directories.

$compiler = \Drupal::service('scss.compiler');

// Compile everything, as drush scss:compile does.
$ok = $compiler->setForce(TRUE)->compileScss();

// Compile and report the CSS as messages without writing.
$compiler->setForce(TRUE)->setTesting(TRUE)->compileScss();

// Whether any source changed since the last compile.
$needed = $compiler->compileNeeded();

// The directories that would be compiled, with their files.
$list = $compiler->getListToCompile();

compileScss() returns FALSE when nothing needed compiling or anything failed.

scss.monitor (Drupal\scss\Services\ScssMonitor)
The request subscriber behind watch mode.

Drush

drush scss:compile (aliases scss, scss-c) is defined in Drupal\scss\Commands\ScssCommands.

State

The module stores, in Drupal state:

  • scss_last_compile_date_DIRECTORY, with / in the directory replaced by %: the time of each directory's last compile;
  • scss.last_watch_check: the time of the last watch check.

All are deleted on uninstall.