class LibsassCompiler in SCSS/Less Compiler 8
Plugin implementation of the Libsass compiler.
Plugin annotation
@ScssCompilerPlugin(
id = "scss_compiler_libsass",
name = "Libsass (Experimental)",
description = "",
extensions = {
"scss" = "scss",
"sass" = "sass",
}
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\scss_compiler\ScssCompilerPluginBase implements ContainerFactoryPluginInterface, ScssCompilerPluginInterface uses MessengerTrait, StringTranslationTrait
- class \Drupal\scss_compiler\Plugin\ScssCompiler\LibsassCompiler
- class \Drupal\scss_compiler\ScssCompilerPluginBase implements ContainerFactoryPluginInterface, ScssCompilerPluginInterface uses MessengerTrait, StringTranslationTrait
Expanded class hierarchy of LibsassCompiler
File
- src/
Plugin/ ScssCompiler/ LibsassCompiler.php, line 23
Namespace
Drupal\scss_compiler\Plugin\ScssCompilerView source
class LibsassCompiler extends ScssCompilerPluginBase {
/**
* Base cli command.
*
* @var string
*/
const BASE_COMMAND = 'node';
/**
* Valid hash sum of the node script.
*/
const SCRIPT_HASH = 'b0f0ed985ed9a065c7ceace66b7d44a9806e9df403ec8401480f224a442baca6347de40372458810ee9b29a21cffbc71519aeb1e64179c56c4b312808bca871f';
/**
* Node script file size.
*/
const SCRIPT_SIZE = 1485;
/**
* Array with queued files.
*
* @var array
*/
protected $queueList;
/**
* Flag indicates if queue already compiled.
*
* @var string
*/
protected $queueRun;
/**
* Path to nodejs script.
*
* @var string
*/
protected $scriptPath;
/**
* {@inheritdoc}
*/
public function init() {
$status = self::getStatus();
if ($status !== TRUE) {
throw new \Exception($status);
}
$module_path = DRUPAL_ROOT . '/' . drupal_get_path('module', 'scss_compiler');
$this->scriptPath = $module_path . '/js/libsass.js';
// Prevent the execution of the script if it contains changes.
if (hash_file('sha512', $this->scriptPath) !== self::SCRIPT_HASH || filesize($this->scriptPath) !== self::SCRIPT_SIZE) {
throw new \Exception($this
->t('Compiler initialization failed. The execution script is modified.'));
}
}
/**
* {@inheritdoc}
*/
public static function getVersion() {
// Parse package.json and find libsass version, because of run node-sass -v
// take around 150ms.
$node_modules_path = \Drupal::service('scss_compiler')
->getOption('node_modules_path');
if (file_exists($node_modules_path . '/node-sass/package.json')) {
$package = file_get_contents($node_modules_path . '/node-sass/package.json');
$match = [];
preg_match('/"libsass":\\s"([\\d\\.]+)"/', $package, $match);
if (!empty($match[1])) {
return $match[1];
}
}
return FALSE;
}
/**
* {@inheritdoc}
*/
public static function getStatus() {
if (!function_exists('proc_open')) {
return t('@function disabled.', [
'@function' => 'proc_open',
]);
}
// Checks if node-sass binary exists, run any cli commands will take to many
// time.
if (!file_exists(\Drupal::service('scss_compiler')
->getOption('node_modules_path') . '/node-sass/bin/node-sass')) {
return t('Node-sass library not found');
}
return TRUE;
}
/**
* Compile all queued files.
*/
public function compileQueue() {
if ($this->queueRun || empty($this->queueList)) {
return;
}
try {
$this->queueRun = TRUE;
$command = self::BASE_COMMAND . ' ' . $this->scriptPath;
$cache_folder = $this->scssCompiler
->getCacheFolder();
$this->fileSystem
->prepareDirectory($cache_folder, FileSystemInterface::CREATE_DIRECTORY);
$files = $this->queueList;
foreach ($files as &$file) {
$css_dir = dirname($file['css_path']);
$this->fileSystem
->prepareDirectory($css_dir, FileSystemInterface::CREATE_DIRECTORY);
}
$import_paths = [
DRUPAL_ROOT,
];
if ($this->scssCompiler
->getAdditionalImportPaths()) {
$import_paths = array_merge($import_paths, $this->scssCompiler
->getAdditionalImportPaths());
}
$data = [
'config' => [
'sourcemaps' => $this->scssCompiler
->getOption('sourcemaps'),
'output_format' => $this->scssCompiler
->getOption('output_format'),
'import_paths' => $import_paths,
],
'files' => $files,
];
$data = json_encode($data);
file_put_contents($cache_folder . '/libsass_temp.json', $data);
$process = new Process($command);
$process
->run(NULL, [
'SCSS_COMPILER_NODE_MODULES_PATH' => $this->scssCompiler
->getOption('node_modules_path'),
'SCSS_COMPILER_DRUPAL_ROOT' => DRUPAL_ROOT,
'SCSS_COMPILER_CACHE_FOLDER' => $this->fileSystem
->realpath($cache_folder),
]);
$this->fileSystem
->delete($cache_folder . '/libsass_temp.json');
if (!$process
->isSuccessful()) {
throw new ProcessFailedException($process);
}
} catch (\Exception $e) {
$this
->messenger()
->addError($e
->getMessage());
}
}
/**
* {@inheritdoc}
*/
public function compile(array $scss_file) {
// Node-sass cli has awful performance, so we collect all scss files and run
// them through custom script.
$this->queueList[] = $scss_file;
}
/**
* {@inheritdoc}
*/
public function checkLastModifyTime(array &$source_file) {
$last_modify_time = filemtime($source_file['source_path']);
$source_folder = dirname($source_file['source_path']);
$import = [];
$content = file_get_contents($source_file['source_path']);
preg_match_all('/@import(.*);/', $content, $import);
if (!empty($import[1])) {
foreach ($import[1] as $file) {
// Normalize @import path.
$file_path = trim($file, '\'" ');
$pathinfo = pathinfo($file_path);
$extension = '.' . pathinfo($file_path, PATHINFO_EXTENSION);
$filename = $pathinfo['filename'];
$dirname = $pathinfo['dirname'] === '.' ? '' : $pathinfo['dirname'] . '/';
$file_path = $source_folder . '/' . $dirname . $filename . $extension;
$scss_path = $source_folder . '/' . $dirname . '_' . $filename . $extension;
if (file_exists($file_path) || file_exists($file_path = $scss_path)) {
$file_modify_time = filemtime($file_path);
if ($file_modify_time > $last_modify_time) {
$last_modify_time = $file_modify_time;
}
}
}
}
return $last_modify_time;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
LibsassCompiler:: |
protected | property | Array with queued files. | |
LibsassCompiler:: |
protected | property | Flag indicates if queue already compiled. | |
LibsassCompiler:: |
protected | property | Path to nodejs script. | |
LibsassCompiler:: |
constant | Base cli command. | ||
LibsassCompiler:: |
public | function |
Checks if file was changed. Overrides ScssCompilerPluginInterface:: |
|
LibsassCompiler:: |
public | function |
Compiles single source file. Overrides ScssCompilerPluginInterface:: |
|
LibsassCompiler:: |
public | function | Compile all queued files. | |
LibsassCompiler:: |
public static | function |
Returns status of compiler library. Overrides ScssCompilerPluginInterface:: |
|
LibsassCompiler:: |
public static | function |
Returns compiler version. Overrides ScssCompilerPluginInterface:: |
|
LibsassCompiler:: |
public | function |
Calls a code on plugin initialization. Overrides ScssCompilerPluginBase:: |
|
LibsassCompiler:: |
constant | Valid hash sum of the node script. | ||
LibsassCompiler:: |
constant | Node script file size. | ||
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
ScssCompilerPluginBase:: |
protected | property | The file system service. | |
ScssCompilerPluginBase:: |
protected | property | The module handler. | |
ScssCompilerPluginBase:: |
protected | property | The current request. | |
ScssCompilerPluginBase:: |
protected | property | The scss compiler service. | |
ScssCompilerPluginBase:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
ScssCompilerPluginBase:: |
public | function |
Constructs a SCSS Compiler base plugin. Overrides PluginBase:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |