You are here

function scss_compiler_css_alter in SCSS/Less Compiler 8

Implements hook_css_alter().

File

./scss_compiler.module, line 89
Module compiles scss files into css via scssphp compiler.

Code

function scss_compiler_css_alter(&$css, AttachedAssetsInterface $assets) {

  // Find all scss files and create associative array with info
  // Each scss file has name, source_path, css_path,
  // namespace - theme/module name, uses to build path to static resources.
  $scss_files = [];
  $plugins = \Drupal::service('scss_compiler')
    ->getOption('plugins');
  if (empty($plugins)) {
    return;
  }
  $extensions = array_keys($plugins);
  foreach ($css as $path => $file) {
    if (in_array(pathinfo($path, PATHINFO_EXTENSION), $extensions)) {
      $scss_file = Drupal::service('scss_compiler')
        ->buildCompilationFileInfo($file);
      if (empty($scss_file)) {
        continue;
      }
      $scss_files[$scss_file['namespace']][$scss_file['source_path']] = $scss_file;
      if (!file_exists($scss_file['css_path'])) {
        Drupal::service('scss_compiler')
          ->compile($scss_file);
      }
      unset($css[$path]);
      $css[$scss_file['css_path']] = $file;
      $css[$scss_file['css_path']]['data'] = $scss_file['css_path'];
    }
  }

  // All scss files saves to compile list cache.
  Drupal::service('scss_compiler')
    ->setCompileList($scss_files);
}