From 267e70256dabd8a2d01a502cd613412fc7b0b799 Mon Sep 17 00:00:00 2001 From: Bernat Arlandis Date: Thu, 1 Jun 2023 14:26:25 +0200 Subject: [PATCH] Allow multiple extensions for ScssFilter --- src/Filter/CssDependencyTrait.php | 39 ++++++++++++++++++++----------- src/Filter/ScssFilter.php | 12 ++++++++-- 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/src/Filter/CssDependencyTrait.php b/src/Filter/CssDependencyTrait.php index 5f5b8cfe..a367b12c 100644 --- a/src/Filter/CssDependencyTrait.php +++ b/src/Filter/CssDependencyTrait.php @@ -42,31 +42,42 @@ public function getDependencies(AssetTarget $target, array $paths = []) continue; } - $ext = $this->_settings['ext']; - $extLength = strlen($ext); + $extensions = $this->_settings['ext']; + if (is_string($extensions)) { + $extensions = [$extensions]; + } $deps = []; foreach ($imports as $name) { - if ('.css' === substr($name, -4)) { + $importExt = '.' . pathinfo($name, PATHINFO_EXTENSION); + if ($importExt === '.css') { // skip normal css imports continue; } - if ($ext !== substr($name, -$extLength)) { - $name .= $ext; - } - $deps[] = $name; - if ($hasPrefix) { - $prefixedName = $this->_prependPrefixToFilename($name); - $deps[] = $prefixedName; + if (!in_array($importExt, $extensions)) { + $names = []; + foreach ($extensions as $extension) { + $names[] = $name . $extension; + } + } else { + $names = [$name]; } - foreach ($paths as $path) { - $deps[] = $path . DIRECTORY_SEPARATOR . $name; + foreach ($names as $name) { + $deps[] = $name; if ($hasPrefix) { - $deps[] = $path . DIRECTORY_SEPARATOR . $prefixedName; + $prefixedName = $this->_prependPrefixToFilename($name); + $deps[] = $prefixedName; + } + foreach ($paths as $path) { + $deps[] = $path . DIRECTORY_SEPARATOR . $name; + if ($hasPrefix) { + $deps[] = $path . DIRECTORY_SEPARATOR . $prefixedName; + } } } } foreach ($deps as $import) { + $importExt = '.' . pathinfo($import, PATHINFO_EXTENSION); $path = $this->_findFile($import); try { $file = new Local($path); @@ -81,7 +92,7 @@ public function getDependencies(AssetTarget $target, array $paths = []) // Only recurse through non-css imports as css files are not // inlined by less/sass. - if ($newTarget && $ext === substr($import, -$extLength)) { + if ($newTarget && in_array($importExt, $extensions)) { $children = array_merge($children, $this->getDependencies($newTarget)); } } diff --git a/src/Filter/ScssFilter.php b/src/Filter/ScssFilter.php index 494be945..2f7da796 100644 --- a/src/Filter/ScssFilter.php +++ b/src/Filter/ScssFilter.php @@ -30,7 +30,7 @@ class ScssFilter extends AssetFilter } protected $_settings = array( - 'ext' => '.scss', + 'ext' => ['.scss', '.sass'], 'sass' => '/usr/bin/sass', 'path' => '/usr/bin', 'imports' => [], @@ -57,9 +57,17 @@ public function getDependencies($target) */ public function input($filename, $content) { - if (substr($filename, strlen($this->_settings['ext']) * -1) !== $this->_settings['ext']) { + $ext = '.' . pathinfo($filename, PATHINFO_EXTENSION); + + $acceptedExt = $this->_settings['ext']; + if (is_string($acceptedExt)) { + $acceptedExt = [$acceptedExt]; + } + + if (!in_array($ext, $acceptedExt)) { return $content; } + $filename = preg_replace('/ /', '\\ ', $filename); $cmd = $this->_settings['sass']; foreach ($this->_settings['imports'] as $path) {