Skip to content

Commit 5c0e366

Browse files
fracsibocharsky-bw
andauthored
Allow using custom PostCSS configuration file (#80)
* PostCss option Add tailwind cli postcss option to the build command * PostCSS config file to config Test added Documentation added * Documentation fix * Reformat services.php * Consistent naming Co-Authored-By: Victor Bocharsky <[email protected]> --------- Co-authored-by: Victor Bocharsky <[email protected]>
1 parent 28db7cf commit 5c0e366

File tree

7 files changed

+89
-7
lines changed

7 files changed

+89
-7
lines changed

config/services.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
<?php
22

33
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
4-
54
use Symfonycasts\TailwindBundle\AssetMapper\TailwindCssAssetCompiler;
65
use Symfonycasts\TailwindBundle\Command\TailwindBuildCommand;
76
use Symfonycasts\TailwindBundle\Command\TailwindInitCommand;
8-
use Symfonycasts\TailwindBundle\TailwindBinary;
97
use Symfonycasts\TailwindBundle\TailwindBuilder;
8+
109
use function Symfony\Component\DependencyInjection\Loader\Configurator\abstract_arg;
1110
use function Symfony\Component\DependencyInjection\Loader\Configurator\param;
1211
use function Symfony\Component\DependencyInjection\Loader\Configurator\service;
@@ -26,11 +25,12 @@
2625
abstract_arg('path to tailwind binary'),
2726
abstract_arg('Tailwind binary version'),
2827
abstract_arg('path to Tailwind CSS config file'),
28+
abstract_arg('path to PostCSS config file'),
2929
])
3030

3131
->set('tailwind.command.build', TailwindBuildCommand::class)
3232
->args([
33-
service('tailwind.builder')
33+
service('tailwind.builder'),
3434
])
3535
->tag('console.command')
3636

@@ -42,11 +42,10 @@
4242

4343
->set('tailwind.css_asset_compiler', TailwindCssAssetCompiler::class)
4444
->args([
45-
service('tailwind.builder')
45+
service('tailwind.builder'),
4646
])
4747
->tag('asset_mapper.compiler', [
4848
// run before core CssAssetUrlCompiler that resolves url() references
4949
'priority' => 10,
50-
])
51-
;
50+
]);
5251
};

doc/index.rst

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ the default directories, for example, in the ``vendor/`` directory like the
189189
.. code-block:: yaml
190190
191191
# config/packages/twig.yaml
192-
twig:
192+
twig:
193193
form_themes:
194194
- 'tailwind_2_layout.html.twig'
195195
@@ -234,6 +234,25 @@ if you want to use a different version, you can specify the version to use,
234234
set ``binary_version`` option:
235235

236236
.. code-block:: yaml
237+
237238
# config/packages/symfonycasts_tailwind.yaml
238239
symfonycasts_tailwind:
239240
binary_version: 'v3.3.0'
241+
242+
Using a PostCSS config file
243+
------------------------
244+
245+
If you want to use additional PostCSS plugins, you can specify the
246+
PostCSS config file to use, set ``postcss_config_file`` option or
247+
pass the ``--postcss`` option to the ``tailwind:build`` command.
248+
249+
.. code-block:: yaml
250+
251+
# config/packages/symfonycasts_tailwind.yaml
252+
symfonycasts_tailwind:
253+
postcss_config_file: 'postcss.config.js'
254+
255+
256+
.. code-block:: terminal
257+
258+
$ php bin/console tailwind:build --postcss='postcss.config.js'

src/Command/TailwindBuildCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ protected function configure(): void
3737
->addOption('watch', 'w', null, 'Watch for changes and rebuild automatically')
3838
->addOption('poll', null, null, 'Use polling instead of filesystem events when watching')
3939
->addOption('minify', 'm', InputOption::VALUE_NONE, 'Minify the output CSS')
40+
->addOption('postcss', null, InputOption::VALUE_REQUIRED, 'Load custom PostCSS configuration')
4041
;
4142
}
4243

@@ -50,6 +51,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
5051
poll: $input->getOption('poll'),
5152
minify: $input->getOption('minify'),
5253
inputFile: $input->getArgument('input_css'),
54+
postCssConfigFile: $input->getOption('postcss'),
5355
);
5456
$process->wait(function ($type, $buffer) use ($io) {
5557
$io->write($buffer);

src/DependencyInjection/TailwindExtension.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public function load(array $configs, ContainerBuilder $container): void
3232
->replaceArgument(4, $config['binary'])
3333
->replaceArgument(5, $config['binary_version'])
3434
->replaceArgument(6, $config['config_file'])
35+
->replaceArgument(7, $config['postcss_config_file'])
3536
;
3637
}
3738

@@ -71,6 +72,10 @@ public function getConfigTreeBuilder(): TreeBuilder
7172
->info('Tailwind CLI version to download - null means the latest version')
7273
->defaultNull()
7374
->end()
75+
->scalarNode('postcss_config_file')
76+
->info('Path to PostCSS config file which is passed to the Tailwind CLI')
77+
->defaultNull()
78+
->end()
7479
->end();
7580

7681
return $treeBuilder;

src/TailwindBuilder.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public function __construct(
3434
private readonly ?string $binaryPath = null,
3535
private readonly ?string $binaryVersion = null,
3636
private readonly string $configPath = 'tailwind.config.js',
37+
private readonly ?string $postCssConfigPath = null,
3738
) {
3839
$paths = [];
3940
foreach ($inputPaths as $inputPath) {
@@ -48,6 +49,7 @@ public function runBuild(
4849
bool $poll,
4950
bool $minify,
5051
?string $inputFile = null,
52+
?string $postCssConfigFile = null,
5153
): Process {
5254
$binary = $this->createBinary();
5355

@@ -66,6 +68,12 @@ public function runBuild(
6668
if ($minify) {
6769
$arguments[] = '--minify';
6870
}
71+
72+
$postCssConfigPath = $this->validatePostCssConfigFile($postCssConfigFile ?? $this->postCssConfigPath);
73+
if ($postCssConfigPath) {
74+
$arguments[] = '--postcss';
75+
$arguments[] = $postCssConfigPath;
76+
}
6977
$process = $binary->createProcess($arguments);
7078
if ($watch) {
7179
$process->setTimeout(null);
@@ -145,6 +153,23 @@ private function validateInputFile(string $inputPath): string
145153
throw new \InvalidArgumentException(\sprintf('The input CSS file "%s" does not exist.', $inputPath));
146154
}
147155

156+
private function validatePostCssConfigFile(?string $postCssConfigPath): ?string
157+
{
158+
if (null === $postCssConfigPath) {
159+
return null;
160+
}
161+
162+
if (is_file($postCssConfigPath)) {
163+
return realpath($postCssConfigPath);
164+
}
165+
166+
if (is_file($this->projectRootDir.'/'.$postCssConfigPath)) {
167+
return realpath($this->projectRootDir.'/'.$postCssConfigPath);
168+
}
169+
170+
throw new \InvalidArgumentException(\sprintf('The PostCSS config file "%s" does not exist.', $postCssConfigPath));
171+
}
172+
148173
private function createBinary(): TailwindBinary
149174
{
150175
return new TailwindBinary($this->tailwindVarDir, $this->projectRootDir, $this->binaryPath, $this->binaryVersion, $this->cache, $this->output);

tests/TailwindBuilderTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,26 @@ public function testBuildProvidedInputFile(): void
106106
$outputFileContents = file_get_contents(__DIR__.'/fixtures/var/tailwind/second.built.css');
107107
$this->assertStringContainsString('body{background-color:blue}', $outputFileContents, 'The output file should contain minified CSS.');
108108
}
109+
110+
public function testIntegrationWithPostcss(): void
111+
{
112+
$builder = new TailwindBuilder(
113+
__DIR__.'/fixtures',
114+
[__DIR__.'/fixtures/assets/styles/app.css'],
115+
__DIR__.'/fixtures/var/tailwind',
116+
new ArrayAdapter(),
117+
null,
118+
null,
119+
__DIR__.'/fixtures/tailwind.config.js',
120+
__DIR__.'/fixtures/postcss.config.js',
121+
);
122+
$process = $builder->runBuild(watch: false, poll: false, minify: false);
123+
$process->wait();
124+
125+
$this->assertTrue($process->isSuccessful());
126+
$this->assertFileExists(__DIR__.'/fixtures/var/tailwind/app.built.css');
127+
128+
$outputFileContents = file_get_contents(__DIR__.'/fixtures/var/tailwind/app.built.css');
129+
$this->assertStringContainsString('.dummy {}', $outputFileContents, 'The output file should contain the dummy CSS added by the dummy plugin.');
130+
}
109131
}

tests/fixtures/postcss.config.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = {
2+
plugins: [
3+
{
4+
postcssPlugin: 'dummy',
5+
Once (root) {
6+
root.append('.dummy {}')
7+
},
8+
},
9+
],
10+
}

0 commit comments

Comments
 (0)