Skip to content

Commit

Permalink
Merge pull request #73 from berarma/multiple-ext
Browse files Browse the repository at this point in the history
Allow multiple extensions for ScssFilter
  • Loading branch information
markstory authored Jun 1, 2023
2 parents 017350e + 267e702 commit 441b28b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
39 changes: 25 additions & 14 deletions src/Filter/CssDependencyTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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));
}
}
Expand Down
12 changes: 10 additions & 2 deletions src/Filter/ScssFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ScssFilter extends AssetFilter
}

protected $_settings = array(
'ext' => '.scss',
'ext' => ['.scss', '.sass'],
'sass' => '/usr/bin/sass',
'path' => '/usr/bin',
'imports' => [],
Expand All @@ -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) {
Expand Down

0 comments on commit 441b28b

Please sign in to comment.