From b72605990f191e6cbaa917ef3ed513e4506bab2a Mon Sep 17 00:00:00 2001 From: Fracsi Date: Wed, 8 Jan 2025 13:50:47 +0100 Subject: [PATCH 1/5] PostCss option Add tailwind cli postcss option to the build command --- src/Command/TailwindBuildCommand.php | 2 ++ src/TailwindBuilder.php | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/src/Command/TailwindBuildCommand.php b/src/Command/TailwindBuildCommand.php index d86ee0b..c248993 100644 --- a/src/Command/TailwindBuildCommand.php +++ b/src/Command/TailwindBuildCommand.php @@ -37,6 +37,7 @@ protected function configure(): void ->addOption('watch', 'w', null, 'Watch for changes and rebuild automatically') ->addOption('poll', null, null, 'Use polling instead of filesystem events when watching') ->addOption('minify', 'm', InputOption::VALUE_NONE, 'Minify the output CSS') + ->addOption('postcss', null, InputOption::VALUE_REQUIRED, 'Load custom PostCSS configuration') ; } @@ -50,6 +51,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int poll: $input->getOption('poll'), minify: $input->getOption('minify'), inputFile: $input->getArgument('input_css'), + postCss: $input->getOption('postcss'), ); $process->wait(function ($type, $buffer) use ($io) { $io->write($buffer); diff --git a/src/TailwindBuilder.php b/src/TailwindBuilder.php index 2a0a186..7c4f516 100644 --- a/src/TailwindBuilder.php +++ b/src/TailwindBuilder.php @@ -48,6 +48,7 @@ public function runBuild( bool $poll, bool $minify, ?string $inputFile = null, + ?string $postCss = null, ): Process { $binary = $this->createBinary(); @@ -66,6 +67,10 @@ public function runBuild( if ($minify) { $arguments[] = '--minify'; } + if ($postCss) { + $arguments[] = '--postcss'; + $arguments[] = $postCss; + } $process = $binary->createProcess($arguments); if ($watch) { $process->setTimeout(null); From fe082205c7a60b2b85ec03d8979163ca56d0c96d Mon Sep 17 00:00:00 2001 From: Fracsi Date: Thu, 9 Jan 2025 08:49:33 +0100 Subject: [PATCH 2/5] PostCSS config file to config Test added Documentation added --- config/services.php | 61 +++++++++---------- doc/index.rst | 18 +++++- src/Command/TailwindBuildCommand.php | 2 +- src/DependencyInjection/TailwindExtension.php | 5 ++ src/TailwindBuilder.php | 26 +++++++- tests/TailwindBuilderTest.php | 22 +++++++ tests/fixtures/postcss.config.js | 10 +++ 7 files changed, 106 insertions(+), 38 deletions(-) create mode 100644 tests/fixtures/postcss.config.js diff --git a/config/services.php b/config/services.php index 9a7f258..cac8f75 100644 --- a/config/services.php +++ b/config/services.php @@ -1,12 +1,11 @@ services() ->set('cache.symfonycasts.tailwind_bundle') - ->parent('cache.system') - ->tag('cache.pool') - + ->parent('cache.system') + ->tag('cache.pool') ->set('tailwind.builder', TailwindBuilder::class) - ->args([ - param('kernel.project_dir'), - abstract_arg('path to source Tailwind CSS file'), - param('kernel.project_dir').'/var/tailwind', - service('cache.symfonycasts.tailwind_bundle'), - abstract_arg('path to tailwind binary'), - abstract_arg('Tailwind binary version'), - abstract_arg('path to Tailwind CSS config file'), - ]) - + ->args([ + param('kernel.project_dir'), + abstract_arg('path to source Tailwind CSS file'), + param('kernel.project_dir').'/var/tailwind', + service('cache.symfonycasts.tailwind_bundle'), + abstract_arg('path to tailwind binary'), + abstract_arg('Tailwind binary version'), + abstract_arg('path to Tailwind CSS config file'), + abstract_arg('path to PostCSS config file'), + ]) ->set('tailwind.command.build', TailwindBuildCommand::class) - ->args([ - service('tailwind.builder') - ]) - ->tag('console.command') - + ->args([ + service('tailwind.builder'), + ]) + ->tag('console.command') ->set('tailwind.command.init', TailwindInitCommand::class) - ->args([ - service('tailwind.builder'), - ]) - ->tag('console.command') - + ->args([ + service('tailwind.builder'), + ]) + ->tag('console.command') ->set('tailwind.css_asset_compiler', TailwindCssAssetCompiler::class) - ->args([ - service('tailwind.builder') - ]) - ->tag('asset_mapper.compiler', [ - // run before core CssAssetUrlCompiler that resolves url() references - 'priority' => 10, - ]) - ; + ->args([ + service('tailwind.builder'), + ]) + ->tag('asset_mapper.compiler', [ + // run before core CssAssetUrlCompiler that resolves url() references + 'priority' => 10, + ]); }; diff --git a/doc/index.rst b/doc/index.rst index 6a40d66..9ce60c6 100644 --- a/doc/index.rst +++ b/doc/index.rst @@ -189,7 +189,7 @@ the default directories, for example, in the ``vendor/`` directory like the .. code-block:: yaml # config/packages/twig.yaml - twig: + twig: form_themes: - 'tailwind_2_layout.html.twig' @@ -237,3 +237,19 @@ set ``binary_version`` option: # config/packages/symfonycasts_tailwind.yaml symfonycasts_tailwind: binary_version: 'v3.3.0' + +Using a PostCSS config file +------------------------ + +If you want to use additional PostCSS plugins, you can specify the +PostCSS config file to use, set ``postcss_config_file`` option or +pass the ``--postcss`` option to the ``tailwind:build`` command. + +.. code-block:: yaml + # config/packages/symfonycasts_tailwind.yaml + symfonycasts_tailwind: + postcss_config_file: 'postcss.config.js' + +.. code-block:: terminal + + $ php bin/console tailwind:build --postcss='postcss.config.js' diff --git a/src/Command/TailwindBuildCommand.php b/src/Command/TailwindBuildCommand.php index c248993..51b96a1 100644 --- a/src/Command/TailwindBuildCommand.php +++ b/src/Command/TailwindBuildCommand.php @@ -51,7 +51,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int poll: $input->getOption('poll'), minify: $input->getOption('minify'), inputFile: $input->getArgument('input_css'), - postCss: $input->getOption('postcss'), + postCssConfigFile: $input->getOption('postcss'), ); $process->wait(function ($type, $buffer) use ($io) { $io->write($buffer); diff --git a/src/DependencyInjection/TailwindExtension.php b/src/DependencyInjection/TailwindExtension.php index 5bb2912..116ef86 100644 --- a/src/DependencyInjection/TailwindExtension.php +++ b/src/DependencyInjection/TailwindExtension.php @@ -32,6 +32,7 @@ public function load(array $configs, ContainerBuilder $container): void ->replaceArgument(4, $config['binary']) ->replaceArgument(5, $config['binary_version']) ->replaceArgument(6, $config['config_file']) + ->replaceArgument(7, $config['postcss_config_file']) ; } @@ -71,6 +72,10 @@ public function getConfigTreeBuilder(): TreeBuilder ->info('Tailwind CLI version to download - null means the latest version') ->defaultNull() ->end() + ->scalarNode('postcss_config_file') + ->info('Path to PostCSS config file which is passed to the Tailwind CLI') + ->defaultNull() + ->end() ->end(); return $treeBuilder; diff --git a/src/TailwindBuilder.php b/src/TailwindBuilder.php index 7c4f516..becea3c 100644 --- a/src/TailwindBuilder.php +++ b/src/TailwindBuilder.php @@ -34,6 +34,7 @@ public function __construct( private readonly ?string $binaryPath = null, private readonly ?string $binaryVersion = null, private readonly string $configPath = 'tailwind.config.js', + private readonly ?string $postCssconfigPath = null, ) { $paths = []; foreach ($inputPaths as $inputPath) { @@ -48,7 +49,7 @@ public function runBuild( bool $poll, bool $minify, ?string $inputFile = null, - ?string $postCss = null, + ?string $postCssConfigFile = null, ): Process { $binary = $this->createBinary(); @@ -67,9 +68,11 @@ public function runBuild( if ($minify) { $arguments[] = '--minify'; } - if ($postCss) { + + $postCssConfigPath = $this->validatePostCssConfigFile($postCssConfigFile ?? $this->postCssconfigPath); + if ($postCssConfigPath) { $arguments[] = '--postcss'; - $arguments[] = $postCss; + $arguments[] = $postCssConfigPath; } $process = $binary->createProcess($arguments); if ($watch) { @@ -150,6 +153,23 @@ private function validateInputFile(string $inputPath): string throw new \InvalidArgumentException(\sprintf('The input CSS file "%s" does not exist.', $inputPath)); } + private function validatePostCssConfigFile(?string $postCssConfigPath): ?string + { + if (null === $postCssConfigPath) { + return null; + } + + if (is_file($postCssConfigPath)) { + return realpath($postCssConfigPath); + } + + if (is_file($this->projectRootDir.'/'.$postCssConfigPath)) { + return realpath($this->projectRootDir.'/'.$postCssConfigPath); + } + + throw new \InvalidArgumentException(\sprintf('The PostCSS config file "%s" does not exist.', $postCssConfigPath)); + } + private function createBinary(): TailwindBinary { return new TailwindBinary($this->tailwindVarDir, $this->projectRootDir, $this->binaryPath, $this->binaryVersion, $this->cache, $this->output); diff --git a/tests/TailwindBuilderTest.php b/tests/TailwindBuilderTest.php index cb7292e..f5b2054 100644 --- a/tests/TailwindBuilderTest.php +++ b/tests/TailwindBuilderTest.php @@ -106,4 +106,26 @@ public function testBuildProvidedInputFile(): void $outputFileContents = file_get_contents(__DIR__.'/fixtures/var/tailwind/second.built.css'); $this->assertStringContainsString('body{background-color:blue}', $outputFileContents, 'The output file should contain minified CSS.'); } + + public function testIntegrationWithPostcss(): void + { + $builder = new TailwindBuilder( + __DIR__.'/fixtures', + [__DIR__.'/fixtures/assets/styles/app.css'], + __DIR__.'/fixtures/var/tailwind', + new ArrayAdapter(), + null, + null, + __DIR__.'/fixtures/tailwind.config.js', + __DIR__.'/fixtures/postcss.config.js', + ); + $process = $builder->runBuild(watch: false, poll: false, minify: false); + $process->wait(); + + $this->assertTrue($process->isSuccessful()); + $this->assertFileExists(__DIR__.'/fixtures/var/tailwind/app.built.css'); + + $outputFileContents = file_get_contents(__DIR__.'/fixtures/var/tailwind/app.built.css'); + $this->assertStringContainsString('.dummy {}', $outputFileContents, 'The output file should contain the dummy CSS added by the dummy plugin.'); + } } diff --git a/tests/fixtures/postcss.config.js b/tests/fixtures/postcss.config.js new file mode 100644 index 0000000..492f2c5 --- /dev/null +++ b/tests/fixtures/postcss.config.js @@ -0,0 +1,10 @@ +module.exports = { + plugins: [ + { + postcssPlugin: 'dummy', + Once (root) { + root.append('.dummy {}') + }, + }, + ], +} From 28550d5c0568271f7a965708e93d9ec2c0e24419 Mon Sep 17 00:00:00 2001 From: Fracsi Date: Thu, 9 Jan 2025 09:04:07 +0100 Subject: [PATCH 3/5] Documentation fix --- doc/index.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/doc/index.rst b/doc/index.rst index 9ce60c6..9d216e7 100644 --- a/doc/index.rst +++ b/doc/index.rst @@ -234,6 +234,7 @@ if you want to use a different version, you can specify the version to use, set ``binary_version`` option: .. code-block:: yaml + # config/packages/symfonycasts_tailwind.yaml symfonycasts_tailwind: binary_version: 'v3.3.0' @@ -246,10 +247,12 @@ PostCSS config file to use, set ``postcss_config_file`` option or pass the ``--postcss`` option to the ``tailwind:build`` command. .. code-block:: yaml + # config/packages/symfonycasts_tailwind.yaml symfonycasts_tailwind: postcss_config_file: 'postcss.config.js' + .. code-block:: terminal $ php bin/console tailwind:build --postcss='postcss.config.js' From 85c123c86f78a3f9ce2898a720c5771955e3f13f Mon Sep 17 00:00:00 2001 From: Fracsi Date: Thu, 9 Jan 2025 09:46:08 +0100 Subject: [PATCH 4/5] Reformat services.php --- config/services.php | 58 ++++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/config/services.php b/config/services.php index cac8f75..ab9c2f0 100644 --- a/config/services.php +++ b/config/services.php @@ -13,35 +13,39 @@ return static function (ContainerConfigurator $container): void { $container->services() ->set('cache.symfonycasts.tailwind_bundle') - ->parent('cache.system') - ->tag('cache.pool') + ->parent('cache.system') + ->tag('cache.pool') + ->set('tailwind.builder', TailwindBuilder::class) - ->args([ - param('kernel.project_dir'), - abstract_arg('path to source Tailwind CSS file'), - param('kernel.project_dir').'/var/tailwind', - service('cache.symfonycasts.tailwind_bundle'), - abstract_arg('path to tailwind binary'), - abstract_arg('Tailwind binary version'), - abstract_arg('path to Tailwind CSS config file'), - abstract_arg('path to PostCSS config file'), - ]) + ->args([ + param('kernel.project_dir'), + abstract_arg('path to source Tailwind CSS file'), + param('kernel.project_dir').'/var/tailwind', + service('cache.symfonycasts.tailwind_bundle'), + abstract_arg('path to tailwind binary'), + abstract_arg('Tailwind binary version'), + abstract_arg('path to Tailwind CSS config file'), + abstract_arg('path to PostCSS config file'), + ]) + ->set('tailwind.command.build', TailwindBuildCommand::class) - ->args([ - service('tailwind.builder'), - ]) - ->tag('console.command') + ->args([ + service('tailwind.builder'), + ]) + ->tag('console.command') + ->set('tailwind.command.init', TailwindInitCommand::class) - ->args([ - service('tailwind.builder'), - ]) - ->tag('console.command') + ->args([ + service('tailwind.builder'), + ]) + ->tag('console.command') + ->set('tailwind.css_asset_compiler', TailwindCssAssetCompiler::class) - ->args([ - service('tailwind.builder'), - ]) - ->tag('asset_mapper.compiler', [ - // run before core CssAssetUrlCompiler that resolves url() references - 'priority' => 10, - ]); + ->args([ + service('tailwind.builder'), + ]) + ->tag('asset_mapper.compiler', [ + // run before core CssAssetUrlCompiler that resolves url() references + 'priority' => 10, + ]); }; From dd7d906e140bf7e39f6c60d7b8f2879e7bda4c87 Mon Sep 17 00:00:00 2001 From: Fracsi Date: Fri, 17 Jan 2025 15:06:08 +0100 Subject: [PATCH 5/5] Consistent naming Co-Authored-By: Victor Bocharsky --- src/TailwindBuilder.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/TailwindBuilder.php b/src/TailwindBuilder.php index becea3c..7d3fdec 100644 --- a/src/TailwindBuilder.php +++ b/src/TailwindBuilder.php @@ -34,7 +34,7 @@ public function __construct( private readonly ?string $binaryPath = null, private readonly ?string $binaryVersion = null, private readonly string $configPath = 'tailwind.config.js', - private readonly ?string $postCssconfigPath = null, + private readonly ?string $postCssConfigPath = null, ) { $paths = []; foreach ($inputPaths as $inputPath) { @@ -69,7 +69,7 @@ public function runBuild( $arguments[] = '--minify'; } - $postCssConfigPath = $this->validatePostCssConfigFile($postCssConfigFile ?? $this->postCssconfigPath); + $postCssConfigPath = $this->validatePostCssConfigFile($postCssConfigFile ?? $this->postCssConfigPath); if ($postCssConfigPath) { $arguments[] = '--postcss'; $arguments[] = $postCssConfigPath;