You are here

public function ScssphpCompiler::compile in SCSS/Less Compiler 8

Compiles single source file.

Parameters

array $source_file: An associative array with file info.

  • name: filename. Required.
  • namespace: theme/module name. Required.
  • source_path: source file path. Required.
  • css_path: css file destination path. Required.

Overrides ScssCompilerPluginInterface::compile

File

src/Plugin/ScssCompiler/ScssphpCompiler.php, line 90

Class

ScssphpCompiler
Plugin implementation of the Scss compiler.

Namespace

Drupal\scss_compiler\Plugin\ScssCompiler

Code

public function compile(array $scss_file) {
  $import_paths = [
    dirname($scss_file['source_path']),
    DRUPAL_ROOT,
    [
      $this,
      'getImportNamespace',
    ],
  ];
  if ($this->scssCompiler
    ->getAdditionalImportPaths()) {
    $import_paths = array_merge($import_paths, $this->scssCompiler
      ->getAdditionalImportPaths());
  }
  $this->parser
    ->setImportPaths($import_paths);

  // Alter variables.
  $variables = $this->scssCompiler
    ->getVariables()
    ->getAll($scss_file['namespace'], $scss_file['source_path']);
  $this->parser
    ->setVariables($variables);

  // Add assets path to compiler. By default it's theme/module root folder.
  $this->parser->assetsPath = isset($scss_file['assets_path']) ? $scss_file['assets_path'] : '';
  $css_folder = dirname($scss_file['css_path']);
  if ($this->scssCompiler
    ->getOption('sourcemaps')) {
    $this->parser
      ->setSourceMap(Compiler::SOURCE_MAP_FILE);
    $sourcemap_file = $css_folder . '/' . $scss_file['name'] . '.css.map';
    $this->parser
      ->setSourceMapOptions([
      'sourceMapWriteTo' => $sourcemap_file,
      'sourceMapURL' => $scss_file['name'] . '.css.map',
      'sourceMapBasepath' => DRUPAL_ROOT,
      'sourceMapRootpath' => '/',
    ]);
  }
  $this->fileSystem
    ->prepareDirectory($css_folder, FileSystemInterface::CREATE_DIRECTORY);
  $source_content = file_get_contents($scss_file['source_path']);
  return $this->parser
    ->compile($source_content, $scss_file['source_path']);
}