-
-
Notifications
You must be signed in to change notification settings - Fork 26
Add support for named converted output files #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 12 commits
fe4376d
f1574a7
3b7ae99
a6808a8
3d13b79
9c0afd4
3c8497e
04f0769
c5e3f3a
c095075
b942232
32a8ca3
145df5b
669fa0a
15a9f6e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -71,12 +71,12 @@ public function runBuild(bool $watch): Process | |
| public function getScssCssTargets(): array | ||
| { | ||
| $targets = []; | ||
| foreach ($this->sassPaths as $sassPath) { | ||
| foreach ($this->sassPaths as $fileName => $sassPath) { | ||
| if (!is_file($sassPath)) { | ||
| throw new \Exception(sprintf('Could not find Sass file: "%s"', $sassPath)); | ||
| } | ||
|
|
||
| $targets[] = $sassPath.':'.$this->guessCssNameFromSassFile($sassPath, $this->cssPath); | ||
| $targets[] = $sassPath.':'.self::guessCssNameFromSassFile($sassPath, $this->cssPath, $fileName); | ||
| } | ||
|
|
||
| return $targets; | ||
|
|
@@ -90,13 +90,33 @@ public function setOutput(SymfonyStyle $output): void | |
| /** | ||
| * @internal | ||
| */ | ||
| public static function guessCssNameFromSassFile(string $sassFile, string $outputDirectory): string | ||
| public static function guessCssNameFromSassFile(string $sassFile, string $outputDirectory, string|int $fileName = null): string | ||
| { | ||
| $fileName = basename($sassFile, '.scss'); | ||
| if (null === $fileName || \is_int($fileName)) { | ||
| $fileName = basename($sassFile, '.scss'); | ||
| } | ||
|
|
||
| return $outputDirectory.'/'.$fileName.'.output.css'; | ||
| } | ||
|
|
||
| public function getIdentifierByLogicalPath(string $path): ?string | ||
| { | ||
| if (array_is_list($this->sassPaths)) { | ||
| return null; | ||
| } | ||
|
|
||
| foreach ($this->sassPaths as $identifier => $configuredSassPath) { | ||
| // as the configured paths include the project dir, we need to subtract it to be able to compare the paths | ||
| $logicalPath = str_replace($this->projectRootDir.'/assets/', '', $configuredSassPath); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. be careful about windows paths when doing And also be careful that you don't remove only a prefix. If the project root dir is
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good one, I refactored it to make use of |
||
|
|
||
| if ($path === $logicalPath) { | ||
| return $identifier; | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| private function createBinary(): SassBinary | ||
| { | ||
| return new SassBinary($this->projectRootDir.'/var', $this->binaryPath, $this->output); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /* | ||
| * 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\AssetMapperInterface; | ||
| use Symfony\Component\AssetMapper\MappedAsset; | ||
| use Symfonycasts\SassBundle\AssetMapper\SassCssCompiler; | ||
| use Symfonycasts\SassBundle\SassBuilder; | ||
|
|
||
| final class SassCssCompilerTest extends TestCase | ||
| { | ||
| private const CSS_DIR = __DIR__.'/../fixtures/var/sass'; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| if (!is_dir(self::CSS_DIR)) { | ||
| mkdir(self::CSS_DIR, 0777, true); | ||
| } | ||
| } | ||
|
|
||
| public function testCompileSingleSassPath(): void | ||
| { | ||
| $scssFile = __DIR__.'/../fixtures/assets/app.scss'; | ||
| $scssPaths = [ | ||
| $scssFile, | ||
| ]; | ||
| $cssFile = self::CSS_DIR.'/app.output.css'; | ||
|
|
||
| $compiler = new SassCssCompiler( | ||
| $scssPaths, | ||
| self::CSS_DIR, | ||
| $this->createSassBuilder($scssPaths, self::CSS_DIR) | ||
| ); | ||
|
|
||
| $mappedAsset = new MappedAsset( | ||
| 'app.scss', | ||
| $scssFile, | ||
| 'app.css' | ||
| ); | ||
|
|
||
| file_put_contents($cssFile, <<<EOF | ||
| p { | ||
| color: red; | ||
| } | ||
| EOF | ||
| ); | ||
|
|
||
| $compiledContent = $compiler->compile( | ||
| file_get_contents($scssFile), | ||
| $mappedAsset, | ||
| $this->createMock(AssetMapperInterface::class) | ||
| ); | ||
|
|
||
| $this->assertStringEqualsFile( | ||
| $cssFile, | ||
| $compiledContent | ||
| ); | ||
| } | ||
|
|
||
| public function testCompileNamedSassPath() | ||
| { | ||
| $scssFile = __DIR__.'/../fixtures/assets/admin/app.scss'; | ||
| $scssPaths = [ | ||
| 'admin' => $scssFile, | ||
| ]; | ||
| $cssFile = self::CSS_DIR.'/admin.output.css'; | ||
|
|
||
| $compiler = new SassCssCompiler( | ||
| $scssPaths, | ||
| self::CSS_DIR, | ||
| $this->createSassBuilder($scssPaths, self::CSS_DIR) | ||
| ); | ||
|
|
||
| $mappedAsset = new MappedAsset( | ||
| 'admin/app.scss', | ||
| $scssFile, | ||
| 'admin.css' | ||
| ); | ||
|
|
||
| file_put_contents($cssFile, <<<EOF | ||
| p { | ||
| color: blue; | ||
| } | ||
| EOF | ||
| ); | ||
|
|
||
| $compiledContent = $compiler->compile( | ||
| file_get_contents($scssFile), | ||
| $mappedAsset, | ||
| $this->createMock(AssetMapperInterface::class) | ||
| ); | ||
|
|
||
| $this->assertStringEqualsFile( | ||
| $cssFile, | ||
| $compiledContent | ||
| ); | ||
| } | ||
|
|
||
| private function createSassBuilder(array $sassPaths, string $cssPath): SassBuilder | ||
| { | ||
| return new SassBuilder( | ||
| $sassPaths, | ||
| $cssPath, | ||
| __DIR__.'/../fixtures', | ||
| null, | ||
| false | ||
| ); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| $color: blue; | ||
|
|
||
| p { | ||
| color: $color; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you want to allow using maps and not just lists, you need to use
->useAttributeAsKey. Otherwise, merging multiple config files (note that for this matter, awhen@prodsection counts as a separate config for the merging process) together won't preserve keys (and devs using a XML config file would not be able to specify a key)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure how I would define that?
With or without
->useAttributeAsKey(), and manually testing it with an extrawhen@devconfig for example, does not change the array passed to thevalidate-callback function? And what would be the$namevariable to pass to->useAttributeAsKey()?Can you point me to an example definition?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@evertharmeling you would have to define a
when@devsection (or a separate file) that also configures thisroot_sassnode, so that some merging actually happens (if the value is defined only in 1 place, the merging is bypassed entirely, which will preserve keys even if the node is not configured for that).For the name being passed, this would be the name of the attribute used in an XML config (where it cannot be an array key). In this case,
outputlooks like a good option.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Testing with:
config/packages/symfony_casts.yaml:config/packages/test.yaml:Running
symfony console debug:config SymfonycastsSassBundle:Even replacing an entry with same key
websiteseems to work?I'm fine with adding
->useAttributeAsKey('output'), but can't reproduce it with these examples or am I still overlooking something?