From 2684cf37e05c0aecbc43670ce4b733d0982c6fc8 Mon Sep 17 00:00:00 2001 From: wryk Date: Mon, 11 Mar 2024 21:54:26 +0100 Subject: [PATCH 1/2] resolve paths after configuration processing --- config/services.php | 1 - src/AssetMapper/SassCssCompiler.php | 6 +----- .../SymfonycastsSassExtension.php | 11 +++++++++++ tests/AssetMapper/SassCssCompilerTest.php | 14 ++------------ 4 files changed, 14 insertions(+), 18 deletions(-) diff --git a/config/services.php b/config/services.php index 2e147ea..755843a 100644 --- a/config/services.php +++ b/config/services.php @@ -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'), ]) diff --git a/src/AssetMapper/SassCssCompiler.php b/src/AssetMapper/SassCssCompiler.php index aebdd5d..8270e82 100644 --- a/src/AssetMapper/SassCssCompiler.php +++ b/src/AssetMapper/SassCssCompiler.php @@ -12,7 +12,6 @@ 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 @@ -20,7 +19,6 @@ class SassCssCompiler implements AssetCompilerInterface public function __construct( private array $scssPaths, private string $cssPathDirectory, - private string $projectDir, private readonly SassBuilder $sassBuilder ) { } @@ -28,9 +26,7 @@ public function __construct( 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; } } diff --git a/src/DependencyInjection/SymfonycastsSassExtension.php b/src/DependencyInjection/SymfonycastsSassExtension.php index 9eb1a42..b391bb2 100644 --- a/src/DependencyInjection/SymfonycastsSassExtension.php +++ b/src/DependencyInjection/SymfonycastsSassExtension.php @@ -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 { @@ -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']; diff --git a/tests/AssetMapper/SassCssCompilerTest.php b/tests/AssetMapper/SassCssCompilerTest.php index cd6884c..2f05540 100644 --- a/tests/AssetMapper/SassCssCompilerTest.php +++ b/tests/AssetMapper/SassCssCompilerTest.php @@ -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)); } } From 649bd70cf75bdd82475072033a286f34b8b4528f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Andr=C3=A9?= Date: Sat, 16 Mar 2024 03:12:48 +0100 Subject: [PATCH 2/2] Fix other options are forbidden when --no-source-map --- src/AssetMapper/SassCssCompiler.php | 22 +++++++++++++------ .../SymfonycastsSassExtension.php | 9 ++++---- tests/AssetMapper/SassCssCompilerTest.php | 4 ++-- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/AssetMapper/SassCssCompiler.php b/src/AssetMapper/SassCssCompiler.php index 8270e82..17439b2 100644 --- a/src/AssetMapper/SassCssCompiler.php +++ b/src/AssetMapper/SassCssCompiler.php @@ -17,21 +17,29 @@ class SassCssCompiler implements AssetCompilerInterface { public function __construct( - private array $scssPaths, - private string $cssPathDirectory, + /** + * Absolute paths to the .scss files. + * + * @var string[] $scssPaths + */ + private readonly array $scssPaths, + + /** + * Absolute path to the directory where the .css files are stored. + */ + private readonly string $cssPathDirectory, + private readonly SassBuilder $sassBuilder ) { } public function supports(MappedAsset $asset): bool { - foreach ($this->scssPaths as $path) { - if (realpath($asset->sourcePath) === realpath($path)) { - return true; - } + if (!str_ends_with($asset->sourcePath, '.scss')) { + return false; } - return false; + return \in_array(realpath($asset->sourcePath), $this->scssPaths, true); } public function compile(string $content, MappedAsset $asset, AssetMapperInterface $assetMapper): string diff --git a/src/DependencyInjection/SymfonycastsSassExtension.php b/src/DependencyInjection/SymfonycastsSassExtension.php index b391bb2..971f401 100644 --- a/src/DependencyInjection/SymfonycastsSassExtension.php +++ b/src/DependencyInjection/SymfonycastsSassExtension.php @@ -30,12 +30,11 @@ public function load(array $configs, ContainerBuilder $container): void // Ensure paths are absolute $normalizeRootSassPath = function ($path) use ($container) { - return Path::isAbsolute($container->getParameterBag()->resolveValue($path)) - ? $path - : '%kernel.project_dir%/'.$path - ; + return Path::makeAbsolute( + $container->getParameterBag()->resolveValue($path), + $container->getParameter('kernel.project_dir') + ); }; - $config['root_sass'] = array_map($normalizeRootSassPath, $config['root_sass']); // BC Layer with SassBundle < 0.4 diff --git a/tests/AssetMapper/SassCssCompilerTest.php b/tests/AssetMapper/SassCssCompilerTest.php index 2f05540..a02f7e4 100644 --- a/tests/AssetMapper/SassCssCompilerTest.php +++ b/tests/AssetMapper/SassCssCompilerTest.php @@ -23,8 +23,8 @@ public function testSupports() $asset = new MappedAsset('assets/app.scss', __DIR__.'/../fixtures/assets/app.scss'); $compiler = new SassCssCompiler( - [__DIR__.'/../fixtures/assets/app.scss'], - __DIR__.'/../fixtures/var/sass', + [realpath(__DIR__.'/../fixtures/assets/app.scss')], + realpath(__DIR__.'/../fixtures/var/sass'), $builder );