feat(vite): add vite config to decouple it from CI_ENVIRONMENT

This commit is contained in:
Yassine Doghri 2022-01-19 18:31:57 +00:00
parent 6e4acc64ad
commit 8721719cd7
12 changed files with 92 additions and 55 deletions

View File

@ -53,6 +53,7 @@ class Autoload extends AutoloadConfig
'ViewComponents' => APPPATH . 'Libraries/ViewComponents/',
'ViewThemes' => APPPATH . 'Libraries/ViewThemes/',
'MediaClipper' => APPPATH . 'Libraries/MediaClipper/',
'Vite' => APPPATH . 'Libraries/Vite/',
'Themes' => ROOTPATH . 'themes',
];

View File

@ -8,7 +8,6 @@ use App\Libraries\Breadcrumb;
use App\Libraries\Negotiate;
use App\Libraries\Router;
use App\Libraries\View;
use App\Libraries\Vite;
use CodeIgniter\Config\BaseService;
use CodeIgniter\HTTP\Request;
use CodeIgniter\HTTP\RequestInterface;
@ -92,13 +91,4 @@ class Services extends BaseService
return new Breadcrumb();
}
public static function vite(bool $getShared = true): Vite
{
if ($getShared) {
return self::getSharedInstance('vite');
}
return new Vite();
}
}

View File

@ -34,16 +34,8 @@ class Image extends BaseMedia
helper('media');
foreach ($this->sizes as $name => $size) {
if (array_key_exists('extension', $size)) {
$extension = $size['extension'];
} else {
$extension = $this->file_extension;
}
if (array_key_exists('mimetype', $size)) {
$mimetype = $size['mimetype'];
} else {
$mimetype = $this->file_mimetype;
}
$extension = array_key_exists('extension', $size) ? $size['extension'] : $this->file_extension;
$mimetype = array_key_exists('mimetype', $size) ? $size['mimetype'] : $this->file_mimetype;
$this->{$name . '_path'} = $this->file_directory . '/' . $this->file_name . '_' . $name . '.' . $extension;
$this->{$name . '_url'} = media_base_url($this->{$name . '_path'});
$this->{$name . '_mimetype'} = $mimetype;

View File

@ -15,8 +15,6 @@ return [
'list_of_episodes_season' =>
'Season {seasonNumber} episodes ({episodeCount})',
'no_episode' => 'No episode found!',
'no_episode_hint' =>
'Navigate the podcast episodes with the navigation bar above.',
'follow' => 'Follow',
'followTitle' => 'Follow {actorDisplayName} on the fediverse!',
'followers' => '{numberOfFollowers, plural,

View File

@ -15,8 +15,6 @@ return [
'list_of_episodes_season' =>
'Épisodes de la saison {seasonNumber} ({episodeCount})',
'no_episode' => 'Aucun épisode trouvé!',
'no_episode_hint' =>
'Naviguez au sein des épisodes du podcast episodes grâce à la barre de navigation ci-dessus.',
'follow' => 'Suivre',
'followTitle' => 'Suivez {actorDisplayName} sur le fédiverse!',
'followers' => '{numberOfFollowers, plural,

View File

@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace Vite\Config;
use CodeIgniter\Config\BaseService;
use Vite\Vite;
/**
* Services Configuration file.
*
* Services are simply other classes/libraries that the system uses to do its job. This is used by CodeIgniter to allow
* the core of the framework to be swapped out easily without affecting the usage within the rest of your application.
*
* This file holds any application-specific services, or service overrides that you might need. An example has been
* included with the general method format you should use for your service methods. For more examples, see the core
* Services file at system/Config/Services.php.
*/
class Services extends BaseService
{
public static function vite(bool $getShared = true): Vite
{
if ($getShared) {
return self::getSharedInstance('vite');
}
return new Vite();
}
}

View File

@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
namespace Vite\Config;
use CodeIgniter\Config\BaseConfig;
class Vite extends BaseConfig
{
public string $environment = 'production';
public string $baseUrl = 'http://localhost:3000/';
public string $assetsRoot = 'assets';
public string $manifestFile = 'manifest.json';
public string $manifestCSSFile = 'manifest-css.json';
}

View File

@ -2,14 +2,12 @@
declare(strict_types=1);
namespace App\Libraries;
namespace Vite;
use ErrorException;
class Vite
{
protected string $manifestPath = 'assets/manifest.json';
protected string $manifestCSSPath = 'assets/manifest-css.json';
/**
* @var array<string, mixed>
*/
@ -22,17 +20,16 @@ class Vite
public function asset(string $path, string $type): string
{
if (ENVIRONMENT !== 'production') {
if (config('Vite')->environment !== 'production') {
return $this->loadDev($path, $type);
}
// @phpstan-ignore-next-line
return $this->loadProd($path, $type);
}
private function loadDev(string $path, string $type): string
{
return $this->getHtmlTag("http://localhost:3000/assets/{$path}", $type);
return $this->getHtmlTag(config('Vite')->baseUrl . config('Vite')->assetsRoot . "/{$path}", $type);
}
private function loadProd(string $path, string $type): string
@ -41,35 +38,46 @@ class Vite
if ($this->manifestCSSData === null) {
$cacheName = 'vite-manifest-css';
if (! ($cachedManifestCSS = cache($cacheName))) {
if (($manifestCSSContent = file_get_contents($this->manifestCSSPath)) !== false) {
$cachedManifestCSS = json_decode($manifestCSSContent, true);
cache()
->save($cacheName, $cachedManifestCSS, DECADE);
} else {
$manifestCSSPath = config('Vite')
->assetsRoot . '/' . config('Vite')
->manifestCSSFile;
try {
if (($manifestCSSContent = file_get_contents($manifestCSSPath)) !== false) {
$cachedManifestCSS = json_decode($manifestCSSContent, true);
cache()
->save($cacheName, $cachedManifestCSS, DECADE);
}
} catch (ErrorException) {
// ERROR when getting the manifest-css file
$manifestCSSPath = $this->manifestCSSPath;
die("Could not load Vite's <pre>{$manifestCSSPath}</pre> file.");
die("Could not load css manifest: <strong>{$manifestCSSPath}</strong> file not found!");
}
}
$this->manifestCSSData = $cachedManifestCSS;
}
if (array_key_exists($path, $this->manifestCSSData)) {
return $this->getHtmlTag('/assets/' . $this->manifestCSSData[$path]['file'], 'css');
return $this->getHtmlTag(
'/' . config('Vite')->assetsRoot . '/' . $this->manifestCSSData[$path]['file'],
'css'
);
}
}
if ($this->manifestData === null) {
$cacheName = 'vite-manifest';
if (! ($cachedManifest = cache($cacheName))) {
if (($manifestContents = file_get_contents($this->manifestPath)) !== false) {
$cachedManifest = json_decode($manifestContents, true);
cache()
->save($cacheName, $cachedManifest, DECADE);
} else {
$manifestPath = config('Vite')
->assetsRoot . '/' . config('Vite')
->manifestFile;
try {
if (($manifestContents = file_get_contents($manifestPath)) !== false) {
$cachedManifest = json_decode($manifestContents, true);
cache()
->save($cacheName, $cachedManifest, DECADE);
}
} catch (ErrorException) {
// ERROR when retrieving the manifest file
$manifestPath = $this->manifestPath;
die("Could not load Vite's <pre>{$manifestPath}</pre> file.");
die("Could not load manifest: <strong>{$manifestPath}</strong> file not found!");
}
}
$this->manifestData = $cachedManifest;
@ -82,7 +90,7 @@ class Vite
// import css dependencies if any
if (array_key_exists('css', $manifestElement)) {
foreach ($manifestElement['css'] as $cssFile) {
$html .= $this->getHtmlTag('/assets/' . $cssFile, 'css');
$html .= $this->getHtmlTag('/' . config('Vite')->assetsRoot . '/' . $cssFile, 'css');
}
}
@ -90,12 +98,15 @@ class Vite
if (array_key_exists('imports', $manifestElement)) {
foreach ($manifestElement['imports'] as $importPath) {
if (array_key_exists($importPath, $this->manifestData)) {
$html .= $this->getHtmlTag('/assets/' . $this->manifestData[$importPath]['file'], 'js');
$html .= $this->getHtmlTag(
'/' . config('Vite')->assetsRoot . '/' . $this->manifestData[$importPath]['file'],
'js'
);
}
}
}
$html .= $this->getHtmlTag('/assets/' . $manifestElement['file'], $type);
$html .= $this->getHtmlTag('/' . config('Vite')->assetsRoot . '/' . $manifestElement['file'], $type);
}
return $html;

View File

@ -42,6 +42,8 @@ to help you kickstart your contribution.
```ini
CI_ENVIRONMENT="development"
# If set to development, you must run `npm run dev` to start the static assets server
vite.environment="development"
# By default, this is set to true in the app config.
# For development, this must be set to false as it is

View File

@ -223,8 +223,6 @@ return [
'list_of_episodes_season' =>
'Season {seasonNumber} episodes ({episodeCount})',
'no_episode' => 'No episode found!',
'no_episode_hint' =>
'Navigate the podcast episodes with the navigation bar above.',
'follow' => 'Follow',
'followers' => '{numberOfFollowers, plural,
one {<span class="font-semibold">#</span> follower}

View File

@ -225,8 +225,6 @@ return [
'list_of_episodes_season' =>
'Épisodes de la saison {seasonNumber} ({episodeCount})',
'no_episode' => 'Aucun épisode trouvé!',
'no_episode_hint' =>
'Naviguez au sein des épisodes du podcast episodes grâce à la barre de navigation ci-dessus.',
'follow' => 'Suivre',
'followers' => '{numberOfFollowers, plural,
one {<span class="font-semibold">#</span> abonné·e}

View File

@ -50,7 +50,6 @@
<h1 class="px-4 mb-2 text-xl text-center"><?= lang(
'Podcast.no_episode',
) ?></h1>
<p class="italic text-center"><?= lang('Podcast.no_episode_hint') ?></p>
<?php endif; ?>
<?= $this->endSection()