Skip to content

Commit affe289

Browse files
committed
feature #21 feat: add --minify option (Kocal)
This PR was squashed before being merged into the main branch. Discussion ---------- feat: add --minify option Hi, this PR implements the `--minify` option of the standalone Tailwind binary. Even if the [AssetMapper documentation](https://symfony.com/doc/current/frontend/asset_mapper.html#optimizing-performance) says that we can auto-minify generated assets by Cloudflare (or other CDNs), I think it's nice to have the possibility to minify Tailwind's output CSS file if we are not using any CDN. Commits ------- 1c35a20 feat: add --minify option
2 parents 14794cb + 1c35a20 commit affe289

File tree

5 files changed

+43
-7
lines changed

5 files changed

+43
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ When you deploy, run the `tailwind:build` command *before* the `asset-map:compil
6868
command so the built file is available:
6969

7070
```bash
71-
php bin/console tailwind:build
71+
php bin/console tailwind:build --minify
7272
php bin/console asset-map:compile
7373
```
7474

src/Command/TailwindBuildCommand.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Symfony\Component\Console\Attribute\AsCommand;
1313
use Symfony\Component\Console\Command\Command;
1414
use Symfony\Component\Console\Input\InputInterface;
15+
use Symfony\Component\Console\Input\InputOption;
1516
use Symfony\Component\Console\Output\OutputInterface;
1617
use Symfony\Component\Console\Style\SymfonyStyle;
1718
use Symfonycasts\TailwindBundle\TailwindBuilder;
@@ -30,7 +31,10 @@ public function __construct(
3031

3132
protected function configure(): void
3233
{
33-
$this->addOption('watch', 'w', null, 'Watch for changes and rebuild automatically');
34+
$this
35+
->addOption('watch', 'w', null, 'Watch for changes and rebuild automatically')
36+
->addOption('minify', 'm', InputOption::VALUE_NONE, 'Minify the output CSS')
37+
;
3438
}
3539

3640
protected function execute(InputInterface $input, OutputInterface $output): int
@@ -39,7 +43,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
3943
$this->tailwindBuilder->setOutput($io);
4044

4145
$process = $this->tailwindBuilder->runBuild(
42-
$input->getOption('watch'),
46+
watch: $input->getOption('watch'),
47+
minify: $input->getOption('minify'),
4348
);
4449
$process->wait(function ($type, $buffer) use ($io) {
4550
$io->write($buffer);

src/TailwindBuilder.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
* Manages the process of executing Tailwind on the input file.
1818
*
1919
* @author Ryan Weaver <[email protected]>
20+
*
21+
* @final
2022
*/
2123
class TailwindBuilder
2224
{
@@ -40,13 +42,18 @@ public function __construct(
4042
}
4143
}
4244

43-
public function runBuild(bool $watch): Process
44-
{
45+
public function runBuild(
46+
bool $watch,
47+
bool $minify,
48+
): Process {
4549
$binary = $this->createBinary();
4650
$arguments = ['-i', $this->inputPath, '-o', $this->getInternalOutputCssPath()];
4751
if ($watch) {
4852
$arguments[] = '--watch';
4953
}
54+
if ($minify) {
55+
$arguments[] = '--minify';
56+
}
5057
$process = $binary->createProcess($arguments);
5158
if ($watch) {
5259
$process->setTimeout(null);

tests/TailwindBuilderTest.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,37 @@ protected function tearDown(): void
3434
}
3535
}
3636

37-
public function testIntegration(): void
37+
public function testIntegrationWithDefaultOptions(): void
3838
{
3939
$builder = new TailwindBuilder(
4040
__DIR__.'/fixtures',
4141
__DIR__.'/fixtures/assets/styles/app.css',
4242
__DIR__.'/fixtures/var/tailwind'
4343
);
44-
$process = $builder->runBuild(false);
44+
$process = $builder->runBuild(watch: false, minify: false);
4545
$process->wait();
4646

4747
$this->assertTrue($process->isSuccessful());
4848
$this->assertFileExists(__DIR__.'/fixtures/var/tailwind/tailwind.built.css');
49+
50+
$outputFileContents = file_get_contents(__DIR__.'/fixtures/var/tailwind/tailwind.built.css');
51+
$this->assertStringContainsString("body {\n background-color: red;\n}", $outputFileContents, 'The output file should contain non-minified CSS.');
52+
}
53+
54+
public function testIntegrationWithMinify(): void
55+
{
56+
$builder = new TailwindBuilder(
57+
__DIR__.'/fixtures',
58+
__DIR__.'/fixtures/assets/styles/app.css',
59+
__DIR__.'/fixtures/var/tailwind'
60+
);
61+
$process = $builder->runBuild(watch: false, minify: true);
62+
$process->wait();
63+
64+
$this->assertTrue($process->isSuccessful());
65+
$this->assertFileExists(__DIR__.'/fixtures/var/tailwind/tailwind.built.css');
66+
67+
$outputFileContents = file_get_contents(__DIR__.'/fixtures/var/tailwind/tailwind.built.css');
68+
$this->assertStringContainsString('body{background-color:red}', $outputFileContents, 'The output file should contain minified CSS.');
4969
}
5070
}

tests/fixtures/assets/styles/app.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
@tailwind base;
22
@tailwind components;
33
@tailwind utilities;
4+
5+
body {
6+
background-color: red;
7+
}

0 commit comments

Comments
 (0)