Skip to content

Commit

Permalink
Use production config to run Vite build automatically in production
Browse files Browse the repository at this point in the history
  • Loading branch information
lcharette committed Jul 5, 2024
1 parent 05c87b9 commit 1326765
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 18 deletions.
6 changes: 3 additions & 3 deletions app/config/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@
'assets' => [
'bundler' => env('ASSETS_BUNDLER'), // Either 'vite' or 'webpack'
'vite' => [
'manifest' => 'public://.vite/manifest.json',
'dev' => env('VITE_DEV_ENABLED'),
'base' => '',
'manifest' => 'assets://.vite/manifest.json',
'dev' => env('VITE_DEV_ENABLED', true),
'base' => 'assets/',
'server' => 'http://[::1]:3000/',
],
// Defines path to Webpack Encore `entrypoints.json` and `manifest.json` files.
Expand Down
12 changes: 12 additions & 0 deletions app/config/production.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@
* Default production config file for UserFrosting. You may override/extend this in your site's configuration file to customize deploy settings.
*/
return [
/*
* ----------------------------------------------------------------------
* Asset bundler Config
* ----------------------------------------------------------------------
* Under production, don't use Vite dev server by default.
*/
'assets' => [
'vite' => [
'dev' => env('VITE_DEV_ENABLED', false),
],
],

/*
* `confirm_sensitive_command` in production mode
*/
Expand Down
12 changes: 7 additions & 5 deletions app/src/Bakery/AssetsViteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use UserFrosting\Bakery\WithSymfonyStyle;
use UserFrosting\Config\Config;
use UserFrosting\Sprinkle\Core\Bakery\Helper\ShellCommandHelper;
use UserFrosting\Sprinkle\Core\Exceptions\VersionCompareException;
use UserFrosting\Sprinkle\Core\Validators\NodeVersionValidator;
Expand All @@ -37,8 +38,8 @@ final class AssetsViteCommand extends Command
#[Inject]
protected NpmVersionValidator $npmVersionValidator;

#[Inject('UF_MODE')]
protected string $envMode;
#[Inject]
protected Config $config;

/**
* {@inheritdoc}
Expand Down Expand Up @@ -66,7 +67,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
$this->io->title('Running Vite');

// Get options
$production = (bool) $input->getOption('production');
$forceProduction = (bool) $input->getOption('production');
$devEnabled = $this->config->getBool('assets.vite.dev', true);

// Validate dependencies
try {
Expand Down Expand Up @@ -95,8 +97,8 @@ protected function execute(InputInterface $input, OutputInterface $output)

// Select command based on command arguments
$command = match (true) {
($production || $this->envMode === 'production') => 'npm run vite:build',
default => 'npm run vite:dev',
$forceProduction, !$devEnabled => 'npm run vite:build',
default => 'npm run vite:dev',
};

$this->io->info("Running command: $command");
Expand Down
2 changes: 1 addition & 1 deletion app/src/ServicesProvider/ViteService.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function register(): array
{
return [
ViteManifestInterface::class => function (ResourceLocatorInterface $locator, Config $config) {
$manifestFile = $config->getString('assets.vite.manifest', 'public://.vite/manifest.json');
$manifestFile = $config->getString('assets.vite.manifest', 'assets://.vite/manifest.json');
$manifestFile = (string) $locator->getResource($manifestFile);

return new ViteManifest(
Expand Down
57 changes: 49 additions & 8 deletions app/tests/Unit/Bakery/AssetsViteCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use phpmock\mockery\PHPMockery;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use UserFrosting\Config\Config;
use UserFrosting\Sprinkle\Core\Bakery\AssetsViteCommand;
use UserFrosting\Sprinkle\Core\Bakery\Helper\ShellCommandHelper;
use UserFrosting\Sprinkle\Core\Exceptions\VersionCompareException;
Expand Down Expand Up @@ -56,11 +57,16 @@ public function testCommand(): void
->shouldReceive('validate')->andReturn(true)
->getMock();

// Set config mock
$config = Mockery::mock(Config::class)
->shouldReceive('getBool')->with('assets.vite.dev', true)->andReturn(true)
->getMock();

// Set mock in CI and run command
$ci = ContainerStub::create();
$ci->set(NodeVersionValidator::class, $node);
$ci->set(NpmVersionValidator::class, $npm);
$ci->set('UF_MODE', '');
$ci->set(Config::class, $config);

/** @var AssetsViteCommand */
$command = $ci->get(AssetsViteCommand::class);
Expand Down Expand Up @@ -93,11 +99,16 @@ public function testCommandProductionEnv(): void
->shouldReceive('validate')->andReturn(true)
->getMock();

// Set config mock
$config = Mockery::mock(Config::class)
->shouldReceive('getBool')->with('assets.vite.dev', true)->andReturn(false)
->getMock();

// Set mock in CI and run command
$ci = ContainerStub::create();
$ci->set(NodeVersionValidator::class, $node);
$ci->set(NpmVersionValidator::class, $npm);
$ci->set('UF_MODE', 'production'); // Set production mode
$ci->set(Config::class, $config);

/** @var AssetsViteCommand */
$command = $ci->get(AssetsViteCommand::class);
Expand Down Expand Up @@ -130,11 +141,16 @@ public function testCommandProduction(): void
->shouldReceive('validate')->andReturn(true)
->getMock();

// Set config mock
$config = Mockery::mock(Config::class)
->shouldReceive('getBool')->with('assets.vite.dev', true)->andReturn(true)
->getMock();

// Set mock in CI and run command
$ci = ContainerStub::create();
$ci->set(NodeVersionValidator::class, $node);
$ci->set(NpmVersionValidator::class, $npm);
$ci->set('UF_MODE', '');
$ci->set(Config::class, $config);

/** @var AssetsViteCommand */
$command = $ci->get(AssetsViteCommand::class);
Expand Down Expand Up @@ -162,11 +178,16 @@ public function testCommandWithMissingFiles(): void
->shouldReceive('validate')->andReturn(true)
->getMock();

// Set config mock
$config = Mockery::mock(Config::class)
->shouldReceive('getBool')->with('assets.vite.dev', true)->andReturn(true)
->getMock();

// Set mock in CI and run command
$ci = ContainerStub::create();
$ci->set(NodeVersionValidator::class, $node);
$ci->set(NpmVersionValidator::class, $npm);
$ci->set('UF_MODE', '');
$ci->set(Config::class, $config);

/** @var AssetsViteCommand */
$command = $ci->get(AssetsViteCommand::class);
Expand All @@ -192,11 +213,16 @@ public function testCommandWithErrorInGetcwd(): void
->shouldReceive('validate')->andReturn(true)
->getMock();

// Set config mock
$config = Mockery::mock(Config::class)
->shouldReceive('getBool')->with('assets.vite.dev', true)->andReturn(true)
->getMock();

// Set mock in CI and run command
$ci = ContainerStub::create();
$ci->set(NodeVersionValidator::class, $node);
$ci->set(NpmVersionValidator::class, $npm);
$ci->set('UF_MODE', '');
$ci->set(Config::class, $config);

/** @var AssetsViteCommand */
$command = $ci->get(AssetsViteCommand::class);
Expand All @@ -215,11 +241,16 @@ public function testCommandWithNodeError(): void
->getMock();
$npm = Mockery::mock(NpmVersionValidator::class);

// Set config mock
$config = Mockery::mock(Config::class)
->shouldReceive('getBool')->with('assets.vite.dev', true)->andReturn(true)
->getMock();

// Set mock in CI and run command
$ci = ContainerStub::create();
$ci->set(NodeVersionValidator::class, $node);
$ci->set(NpmVersionValidator::class, $npm);
$ci->set('UF_MODE', '');
$ci->set(Config::class, $config);

/** @var AssetsViteCommand */
$command = $ci->get(AssetsViteCommand::class);
Expand All @@ -239,11 +270,16 @@ public function testCommandWithNpmError(): void
->shouldReceive('validate')->andThrow(new VersionCompareException())
->getMock();

// Set config mock
$config = Mockery::mock(Config::class)
->shouldReceive('getBool')->with('assets.vite.dev', true)->andReturn(true)
->getMock();

// Set mock in CI and run command
$ci = ContainerStub::create();
$ci->set(NodeVersionValidator::class, $node);
$ci->set(NpmVersionValidator::class, $npm);
$ci->set('UF_MODE', '');
$ci->set(Config::class, $config);

/** @var AssetsViteCommand */
$command = $ci->get(AssetsViteCommand::class);
Expand Down Expand Up @@ -285,11 +321,16 @@ function (string $command, int &$exitCode) {
->shouldReceive('validate')->andReturn(true)
->getMock();

// Set config mock
$config = Mockery::mock(Config::class)
->shouldReceive('getBool')->with('assets.vite.dev', true)->andReturn(true)
->getMock();

// Set mock in CI and run command
$ci = ContainerStub::create();
$ci->set(NodeVersionValidator::class, $node);
$ci->set(NpmVersionValidator::class, $npm);
$ci->set('UF_MODE', '');
$ci->set(Config::class, $config);

/** @var AssetsViteCommand */
$command = $ci->get(AssetsViteCommand::class);
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"slim/twig-view": "^3.0",
"vlucas/phpdotenv": "^5.3",
"userfrosting/framework": "~6.0.0@dev",
"userfrosting/vite-php-twig": "^1.0"
"userfrosting/vite-php-twig": "^1.0.2"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.0",
Expand Down

0 comments on commit 1326765

Please sign in to comment.