Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
26 changes: 15 additions & 11 deletions src/AssetMapper/SassCssCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,34 @@
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,
/**
* 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) {
$absolutePath = Path::isAbsolute($path) ? $path : Path::makeAbsolute($path, $this->projectDir);

if (realpath($asset->sourcePath) === realpath($absolutePath)) {
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
Expand Down
10 changes: 10 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,15 @@ 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::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
if (isset($config['embed_sourcemap'])) {
$config['sass_options']['embed_source_map'] = $config['embed_sourcemap'];
Expand Down
18 changes: 4 additions & 14 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(
[__DIR__.'/../fixtures/assets/app.scss'],
__DIR__.'/../fixtures/var/sass',
__DIR__.'/../fixtures',
$compiler = new SassCssCompiler(
[realpath(__DIR__.'/../fixtures/assets/app.scss')],
realpath(__DIR__.'/../fixtures/var/sass'),
$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));
}
}