Skip to content

Commit

Permalink
resolve paths after configuration processing
Browse files Browse the repository at this point in the history
  • Loading branch information
wryk committed Mar 11, 2024
1 parent aa47da8 commit 2684cf3
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 18 deletions.
1 change: 0 additions & 1 deletion config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
->args([
abstract_arg('path to scss files'),
abstract_arg('path to css output directory'),
param('kernel.project_dir'),
service('sass.builder'),
])

Expand Down
6 changes: 1 addition & 5 deletions src/AssetMapper/SassCssCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,21 @@
use Symfony\Component\AssetMapper\AssetMapperInterface;
use Symfony\Component\AssetMapper\Compiler\AssetCompilerInterface;
use Symfony\Component\AssetMapper\MappedAsset;
use Symfony\Component\Filesystem\Path;
use Symfonycasts\SassBundle\SassBuilder;

class SassCssCompiler implements AssetCompilerInterface
{
public function __construct(
private array $scssPaths,
private string $cssPathDirectory,
private string $projectDir,
private readonly SassBuilder $sassBuilder
) {
}

public function supports(MappedAsset $asset): bool
{
foreach ($this->scssPaths as $path) {
$absolutePath = Path::isAbsolute($path) ? $path : Path::makeAbsolute($path, $this->projectDir);

if (realpath($asset->sourcePath) === realpath($absolutePath)) {
if (realpath($asset->sourcePath) === realpath($path)) {
return true;
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/DependencyInjection/SymfonycastsSassExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\Filesystem\Path;

class SymfonycastsSassExtension extends Extension implements ConfigurationInterface
{
Expand All @@ -27,6 +28,16 @@ public function load(array $configs, ContainerBuilder $container): void
$configuration = $this->getConfiguration($configs, $container);
$config = $this->processConfiguration($configuration, $configs);

// Ensure paths are absolute
$normalizeRootSassPath = function ($path) use ($container) {
return Path::isAbsolute($container->getParameterBag()->resolveValue($path))
? $path
: '%kernel.project_dir%/'.$path
;
};

$config['root_sass'] = array_map($normalizeRootSassPath, $config['root_sass']);

// BC Layer with SassBundle < 0.4
if (isset($config['embed_sourcemap'])) {
$config['sass_options']['embed_source_map'] = $config['embed_sourcemap'];
Expand Down
14 changes: 2 additions & 12 deletions tests/AssetMapper/SassCssCompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,12 @@ public function testSupports()

$asset = new MappedAsset('assets/app.scss', __DIR__.'/../fixtures/assets/app.scss');

$compilerAbsolutePath = new SassCssCompiler(
$compiler = new SassCssCompiler(
[__DIR__.'/../fixtures/assets/app.scss'],
__DIR__.'/../fixtures/var/sass',
__DIR__.'/../fixtures',
$builder
);

$this->assertTrue($compilerAbsolutePath->supports($asset), 'Supports absolute paths');

$compilerRelativePath = new SassCssCompiler(
['assets/app.scss'],
__DIR__.'/../fixtures/var/sass',
__DIR__.'/../fixtures',
$builder
);

$this->assertTrue($compilerRelativePath->supports($asset), 'Supportes relative paths');
$this->assertTrue($compiler->supports($asset));
}
}

0 comments on commit 2684cf3

Please sign in to comment.