Skip to content

Commit

Permalink
Fix root_sass relative paths (#8 #43) (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
wryk authored Feb 26, 2024
1 parent 29eef1b commit aa47da8
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
"php": ">=8.1",
"symfony/asset-mapper": "^6.3|^7.0",
"symfony/console": "^5.4|^6.3|^7.0",
"symfony/filesystem": "^5.4|^6.3|^7.0",
"symfony/http-client": "^5.4|^6.3|^7.0",
"symfony/process": "^5.4|^6.3|^7.0"
},
"require-dev": {
"matthiasnoback/symfony-config-test": "^5.0",
"symfony/filesystem": "^6.3|^7.0",
"symfony/framework-bundle": "^6.3|^7.0",
"symfony/phpunit-bridge": "^6.3|^7.0",
"phpstan/phpstan-symfony": "^1.4"
Expand Down
1 change: 1 addition & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
->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: 5 additions & 1 deletion src/AssetMapper/SassCssCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,25 @@
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) {
if (realpath($asset->sourcePath) === realpath($path)) {
$absolutePath = Path::isAbsolute($path) ? $path : Path::makeAbsolute($path, $this->projectDir);

if (realpath($asset->sourcePath) === realpath($absolutePath)) {
return true;
}
}
Expand Down
43 changes: 43 additions & 0 deletions tests/AssetMapper/SassCssCompilerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* This file is part of the SymfonyCasts SassBundle package.
* Copyright (c) SymfonyCasts <https://symfonycasts.com/>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfonycasts\SassBundle\Tests\AssetMapper;

use PHPUnit\Framework\TestCase;
use Symfony\Component\AssetMapper\MappedAsset;
use Symfonycasts\SassBundle\AssetMapper\SassCssCompiler;
use Symfonycasts\SassBundle\SassBuilder;

class SassCssCompilerTest extends TestCase
{
public function testSupports()
{
$builder = $this->createMock(SassBuilder::class);

$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',
$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');
}
}

0 comments on commit aa47da8

Please sign in to comment.