You are here

public function LessphpCompiler::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/LessphpCompiler.php, line 83

Class

LessphpCompiler
Plugin implementation of the Less compiler.

Namespace

Drupal\scss_compiler\Plugin\ScssCompiler

Code

public function compile(array $scss_file) {
  $import_paths = [
    dirname($scss_file['source_path']),
    DRUPAL_ROOT,
  ];
  if ($this->scssCompiler
    ->getAdditionalImportPaths()) {
    $import_paths = array_merge($import_paths, $this->scssCompiler
      ->getAdditionalImportPaths());
  }
  $this->parser
    ->setImportDirs($import_paths);
  $this->parser
    ->setOption('import_callback', [
    $this,
    'importCallback',
  ]);
  $css_folder = dirname($scss_file['css_path']);
  if ($this->scssCompiler
    ->getOption('sourcemaps')) {
    $sourcemap_file = $css_folder . '/' . $scss_file['name'] . '.css.map';
    $this->parser
      ->setOptions([
      'sourceMap' => TRUE,
      'sourceMapWriteTo' => $sourcemap_file,
      'sourceMapURL' => $scss_file['name'] . '.css.map',
      'sourceMapBasepath' => DRUPAL_ROOT,
      'sourceMapRootpath' => '/',
    ]);
  }
  $this->fileSystem
    ->prepareDirectory($css_folder, FileSystemInterface::CREATE_DIRECTORY);
  $this->parser
    ->parseFile($scss_file['source_path'], $scss_file['assets_path']);

  // Alter variables.
  $variables = $this->scssCompiler
    ->getVariables()
    ->getAll($scss_file['namespace'], $scss_file['source_path']);
  $this->parser
    ->ModifyVars($variables);
  $content = $this->parser
    ->getCss();
  return $content;
}