-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Tailwind CSS Standalone CLI utility filter (#42)
- Loading branch information
1 parent
2dfc881
commit fde0642
Showing
9 changed files
with
827 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
namespace Assetic\Filter; | ||
|
||
use Assetic\Contracts\Asset\AssetInterface; | ||
|
||
/** | ||
* Tailwind CSS filter. | ||
* | ||
* Compiles Tailwind CSS into standard CSS, using the standalone Tailwind CSS CLI tool. | ||
* | ||
* @author Ben Thomson <[email protected]> | ||
*/ | ||
class TailwindCssFilter extends BaseProcessFilter | ||
{ | ||
/** | ||
* @var string Path to the binary for this process based filter | ||
*/ | ||
protected $binaryPath = '/usr/bin/tailwindcss'; | ||
|
||
/** | ||
* @var string|null Path to the Tailwind configuration file. | ||
*/ | ||
protected $configPath = null; | ||
|
||
/** | ||
* @var bool Is minification enabled? | ||
*/ | ||
protected $minify = false; | ||
|
||
/** | ||
* @var bool Is autoprefixing enabled? | ||
*/ | ||
protected $autoprefix = true; | ||
|
||
/** | ||
* Sets the path for the configuration file. | ||
*/ | ||
public function setConfigPath(string $configPath): void | ||
{ | ||
$this->configPath = $configPath; | ||
} | ||
|
||
/** | ||
* Enable minification. | ||
*/ | ||
public function minify(): void | ||
{ | ||
$this->minify = true; | ||
} | ||
|
||
/** | ||
* Disable autoprefixer. | ||
*/ | ||
public function withoutAutoprefixing(): void | ||
{ | ||
$this->autoprefix = false; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function filterLoad(AssetInterface $asset) | ||
{ | ||
$args = [ | ||
'--input', | ||
'{INPUT}', | ||
'--output', | ||
'{OUTPUT}' | ||
]; | ||
|
||
if (!is_null($this->configPath)) { | ||
$args[] = '--config'; | ||
$args[] = $this->configPath; | ||
} | ||
|
||
if ($this->minify) { | ||
$args[] = '--minify'; | ||
} | ||
|
||
if (!$this->autoprefix) { | ||
$args[] = '--no-autoprefixer'; | ||
} | ||
|
||
$result = $this->runProcess($asset->getContent(), $args); | ||
$asset->setContent($result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
namespace Assetic\Test\Filter; | ||
|
||
use Assetic\Asset\FileAsset; | ||
use Assetic\Filter\TailwindCssFilter; | ||
|
||
/** | ||
* @group integration | ||
*/ | ||
class TailwindCssFilterTest extends FilterTestCase | ||
{ | ||
/** @var TailwindCssFilter|null */ | ||
private $filter; | ||
|
||
protected function setUp(): void | ||
{ | ||
$tailwindCssBin = $this->findExecutable('tailwindcss', 'TAILWINDCSS_BIN'); | ||
|
||
if (!$tailwindCssBin) { | ||
$this->markTestSkipped('Unable to find `tailwindcss` executable.'); | ||
} | ||
|
||
$this->filter = new TailwindCssFilter($tailwindCssBin); | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
$this->filter = null; | ||
} | ||
|
||
public function testFilterLoad() | ||
{ | ||
$fileAsset = new FileAsset(__DIR__ . '/fixtures/tailwindcss/css/style.css'); | ||
$fileAsset->load(); | ||
|
||
$this->filter->setConfigPath(__DIR__ . '/fixtures/tailwindcss/tailwind.config.js'); | ||
$this->filter->setWorkingDirectory(__DIR__ . '/fixtures/tailwindcss'); | ||
$this->filter->filterLoad($fileAsset); | ||
$contents = $fileAsset->getContent(); | ||
|
||
// Detect boilerplate TailwindCSS styling | ||
$expected = <<<'STYLE' | ||
::backdrop { | ||
--tw-border-spacing-x: 0; | ||
--tw-border-spacing-y: 0; | ||
--tw-translate-x: 0; | ||
--tw-translate-y: 0; | ||
STYLE; | ||
|
||
$this->assertStringContainsString($expected, $contents); | ||
|
||
// Detect class defined in HTML | ||
$expected = <<<'STYLE' | ||
.text-gray-800 { | ||
--tw-text-opacity: 1; | ||
color: rgb(31 41 55 / var(--tw-text-opacity)); | ||
} | ||
STYLE; | ||
|
||
$this->assertStringContainsString($expected, $contents); | ||
} | ||
|
||
public function testFilterLoadWithMinification() | ||
{ | ||
$fileAsset = new FileAsset(__DIR__ . '/fixtures/tailwindcss/css/style.css'); | ||
$fileAsset->load(); | ||
|
||
$this->filter->setConfigPath(__DIR__ . '/fixtures/tailwindcss/tailwind.config.js'); | ||
$this->filter->setWorkingDirectory(__DIR__ . '/fixtures/tailwindcss'); | ||
$this->filter->minify(); | ||
$this->filter->filterLoad($fileAsset); | ||
$contents = $fileAsset->getContent(); | ||
|
||
// Detect boilerplate TailwindCSS styling | ||
$expected = <<<'STYLE' | ||
*,::backdrop,:after,:before{--tw-border-spacing-x:0;--tw-border-spacing-y:0;--tw-translate-x:0;--tw-translate-y:0; | ||
STYLE; | ||
|
||
$this->assertStringContainsString($expected, $contents); | ||
|
||
// Detect class defined in HTML | ||
$expected = <<<'STYLE' | ||
.text-gray-800{--tw-text-opacity:1;color:rgb(31 41 55/var(--tw-text-opacity))} | ||
STYLE; | ||
|
||
$this->assertStringContainsString($expected, $contents); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
@tailwind base; | ||
@tailwind components; | ||
@tailwind utilities; |
15 changes: 15 additions & 0 deletions
15
tests/Assetic/Test/Filter/fixtures/tailwindcss/html/index.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Tailwind CSS test</title> | ||
</head> | ||
<body class="antialiased bg-white text-gray-800"> | ||
<div class="flex flex-row gap-10"> | ||
<div class="bg-red text-white p-4">Red</div> | ||
<div class="bg-green text-white p-4">Green</div> | ||
<div class="bg-blue text-white p-4">Blue</div> | ||
</div> | ||
</body> | ||
</html> |
8 changes: 8 additions & 0 deletions
8
tests/Assetic/Test/Filter/fixtures/tailwindcss/tailwind.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** @type {import('tailwindcss').Config} */ | ||
module.exports = { | ||
content: ["./html/**/*.html"], | ||
theme: { | ||
extend: {}, | ||
}, | ||
plugins: [], | ||
} |
Oops, something went wrong.