Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowing a custom config_file relative to the project #30

Merged
merged 20 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
param('kernel.project_dir').'/var/tailwind',
abstract_arg('path to tailwind binary'),
abstract_arg('Tailwind binary version'),
abstract_arg('path to Tailwind CSS config file'),
])

->set('tailwind.command.build', TailwindBuildCommand::class)
Expand All @@ -31,7 +32,6 @@
->set('tailwind.command.init', TailwindInitCommand::class)
->args([
service('tailwind.builder'),
param('kernel.project_dir'),
])
->tag('console.command')

Expand Down
9 changes: 9 additions & 0 deletions doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,15 @@ directives):
symfonycasts_tailwind:
input_css: 'assets/styles/other.css'

Another option is the ``config_file`` option, which defaults to ``tailwind.config.js``.
This represents the Tailwind configuration file:

.. code-block:: yaml

# config/packages/symfonycasts_tailwind.yaml
symfonycasts_tailwind:
config_file: 'tailwind.config.js'

Using a Different Binary
------------------------

Expand Down
8 changes: 4 additions & 4 deletions src/Command/TailwindInitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class TailwindInitCommand extends Command
{
public function __construct(
private TailwindBuilder $tailwindBuilder,
private string $projectDir,
bocharsky-bw marked this conversation as resolved.
Show resolved Hide resolved
) {
parent::__construct();
}
Expand All @@ -49,8 +48,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int

private function createTailwindConfig(SymfonyStyle $io): bool
{
if (file_exists($this->projectDir.'/tailwind.config.js')) {
$io->note('tailwind.config.js found: Tailwind CSS is already initialized.');
$configFile = $this->tailwindBuilder->getConfigFilePath();
if (file_exists($configFile)) {
$io->note(sprintf('Tailwind config file already exists in "%s"', $configFile));

return true;
}
Expand Down Expand Up @@ -85,7 +85,7 @@ private function createTailwindConfig(SymfonyStyle $io): bool

EOF;

file_put_contents($this->projectDir.'/tailwind.config.js', $tailwindConfig);
file_put_contents($configFile, $tailwindConfig);

return true;
}
Expand Down
5 changes: 5 additions & 0 deletions src/DependencyInjection/TailwindExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function load(array $configs, ContainerBuilder $container): void
->replaceArgument(1, $config['input_css'])
->replaceArgument(3, $config['binary'])
->replaceArgument(4, $config['binary_version'])
->replaceArgument(5, $config['config_file'])
;
}

Expand All @@ -56,6 +57,10 @@ public function getConfigTreeBuilder(): TreeBuilder
->info('Path to CSS file to process through Tailwind')
->defaultValue('%kernel.project_dir%/assets/styles/app.css')
->end()
->scalarNode('config_file')
->info('Path to the tailwind.config.js file')
->defaultValue('%kernel.project_dir%/tailwind.config.js')
->end()
->scalarNode('binary')
->info('The tailwind binary to use instead of downloading a new one')
->defaultNull()
Expand Down
8 changes: 7 additions & 1 deletion src/TailwindBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function __construct(
private readonly string $tailwindVarDir,
private readonly ?string $binaryPath = null,
private readonly ?string $binaryVersion = null,
private readonly string $configPath = 'tailwind.config.js',
) {
if (is_file($inputPath)) {
$this->inputPath = $inputPath;
Expand All @@ -48,7 +49,7 @@ public function runBuild(
bool $minify,
): Process {
$binary = $this->createBinary();
$arguments = ['-i', $this->inputPath, '-o', $this->getInternalOutputCssPath()];
$arguments = ['-c', $this->configPath, '-i', $this->inputPath, '-o', $this->getInternalOutputCssPath()];
if ($watch) {
$arguments[] = '--watch';
}
Expand Down Expand Up @@ -105,6 +106,11 @@ public function getInputCssPath(): string
return $this->inputPath;
}

public function getConfigFilePath(): string
{
return $this->configPath;
}

public function getOutputCssContent(): string
{
if (!is_file($this->getInternalOutputCssPath())) {
Expand Down
10 changes: 8 additions & 2 deletions tests/TailwindBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ public function testIntegrationWithDefaultOptions(): void
$builder = new TailwindBuilder(
__DIR__.'/fixtures',
__DIR__.'/fixtures/assets/styles/app.css',
__DIR__.'/fixtures/var/tailwind'
__DIR__.'/fixtures/var/tailwind',
null,
null,
__DIR__.'/fixtures/tailwind.config.js'
);
$process = $builder->runBuild(watch: false, minify: false);
$process->wait();
Expand All @@ -56,7 +59,10 @@ public function testIntegrationWithMinify(): void
$builder = new TailwindBuilder(
__DIR__.'/fixtures',
__DIR__.'/fixtures/assets/styles/app.css',
__DIR__.'/fixtures/var/tailwind'
__DIR__.'/fixtures/var/tailwind',
null,
null,
__DIR__.'/fixtures/tailwind.config.js'
);
$process = $builder->runBuild(watch: false, minify: true);
$process->wait();
Expand Down