Skip to content

Commit

Permalink
feature #21 feat: add --minify option (Kocal)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
weaverryan committed Sep 2, 2023
2 parents 14794cb + 1c35a20 commit affe289
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ When you deploy, run the `tailwind:build` command *before* the `asset-map:compil
command so the built file is available:

```bash
php bin/console tailwind:build
php bin/console tailwind:build --minify
php bin/console asset-map:compile
```

Expand Down
9 changes: 7 additions & 2 deletions src/Command/TailwindBuildCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfonycasts\TailwindBundle\TailwindBuilder;
Expand All @@ -30,7 +31,10 @@ public function __construct(

protected function configure(): void
{
$this->addOption('watch', 'w', null, 'Watch for changes and rebuild automatically');
$this
->addOption('watch', 'w', null, 'Watch for changes and rebuild automatically')
->addOption('minify', 'm', InputOption::VALUE_NONE, 'Minify the output CSS')
;
}

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

$process = $this->tailwindBuilder->runBuild(
$input->getOption('watch'),
watch: $input->getOption('watch'),
minify: $input->getOption('minify'),
);
$process->wait(function ($type, $buffer) use ($io) {
$io->write($buffer);
Expand Down
11 changes: 9 additions & 2 deletions src/TailwindBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* Manages the process of executing Tailwind on the input file.
*
* @author Ryan Weaver <ryan@symfonycasts.com>
*
* @final
*/
class TailwindBuilder
{
Expand All @@ -40,13 +42,18 @@ public function __construct(
}
}

public function runBuild(bool $watch): Process
{
public function runBuild(
bool $watch,
bool $minify,
): Process {
$binary = $this->createBinary();
$arguments = ['-i', $this->inputPath, '-o', $this->getInternalOutputCssPath()];
if ($watch) {
$arguments[] = '--watch';
}
if ($minify) {
$arguments[] = '--minify';
}
$process = $binary->createProcess($arguments);
if ($watch) {
$process->setTimeout(null);
Expand Down
24 changes: 22 additions & 2 deletions tests/TailwindBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,37 @@ protected function tearDown(): void
}
}

public function testIntegration(): void
public function testIntegrationWithDefaultOptions(): void
{
$builder = new TailwindBuilder(
__DIR__.'/fixtures',
__DIR__.'/fixtures/assets/styles/app.css',
__DIR__.'/fixtures/var/tailwind'
);
$process = $builder->runBuild(false);
$process = $builder->runBuild(watch: false, minify: false);
$process->wait();

$this->assertTrue($process->isSuccessful());
$this->assertFileExists(__DIR__.'/fixtures/var/tailwind/tailwind.built.css');

$outputFileContents = file_get_contents(__DIR__.'/fixtures/var/tailwind/tailwind.built.css');
$this->assertStringContainsString("body {\n background-color: red;\n}", $outputFileContents, 'The output file should contain non-minified CSS.');
}

public function testIntegrationWithMinify(): void
{
$builder = new TailwindBuilder(
__DIR__.'/fixtures',
__DIR__.'/fixtures/assets/styles/app.css',
__DIR__.'/fixtures/var/tailwind'
);
$process = $builder->runBuild(watch: false, minify: true);
$process->wait();

$this->assertTrue($process->isSuccessful());
$this->assertFileExists(__DIR__.'/fixtures/var/tailwind/tailwind.built.css');

$outputFileContents = file_get_contents(__DIR__.'/fixtures/var/tailwind/tailwind.built.css');
$this->assertStringContainsString('body{background-color:red}', $outputFileContents, 'The output file should contain minified CSS.');
}
}
4 changes: 4 additions & 0 deletions tests/fixtures/assets/styles/app.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
@tailwind base;
@tailwind components;
@tailwind utilities;

body {
background-color: red;
}

0 comments on commit affe289

Please sign in to comment.