| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCommunity\App; |
| 4 |
|
| 5 |
use FluentCommunity\App\Services\Helper; |
| 6 |
|
| 7 |
class Vite |
| 8 |
{ |
| 9 |
protected static $moduleScripts = []; |
| 10 |
protected static $resourceURL = 'http://localhost:4444/src/'; |
| 11 |
protected static $assetsURL = FLUENT_COMMUNITY_PLUGIN_URL . 'assets/'; |
| 12 |
|
| 13 |
private static function isDev() |
| 14 |
{ |
| 15 |
$config = App::getInstance()->config; |
| 16 |
return $config->get('app.env') === 'dev'; |
| 17 |
} |
| 18 |
|
| 19 |
public static function enqueueScript($handle, $src, $dependency = [], $version = false, $inFooter = false) |
| 20 |
{ |
| 21 |
static::$moduleScripts [] = $handle; |
| 22 |
$src = static::generateSrc($src); |
| 23 |
wp_enqueue_script($handle, $src, $dependency, $version, $inFooter); |
| 24 |
static::addModuleToScript(); |
| 25 |
} |
| 26 |
|
| 27 |
public static function enqueueStyle($handle, $src, $dependency = [], $version = false, $media = 'all') |
| 28 |
{ |
| 29 |
$src = static::generateSrc($src); |
| 30 |
wp_enqueue_style($handle, $src, $dependency, $version, $media); |
| 31 |
} |
| 32 |
|
| 33 |
public static function enqueueStaticScript($handle, $src, $dependency = [], $version = false, $inFooter = false) |
| 34 |
{ |
| 35 |
$src = static::getStaticSrcUrl($src); |
| 36 |
wp_enqueue_script($handle, $src, $dependency, $version, $inFooter); |
| 37 |
} |
| 38 |
|
| 39 |
public static function enqueueStaticStyle($handle, $src, $dependency = [], $version = false, $media = 'all') |
| 40 |
{ |
| 41 |
$src = static::getStaticSrcUrl($src); |
| 42 |
wp_enqueue_style($handle, $src, $dependency, $version, $media); |
| 43 |
} |
| 44 |
|
| 45 |
private static function parseManifest() |
| 46 |
{ |
| 47 |
static $manifest; |
| 48 |
if ($manifest) { |
| 49 |
return $manifest; |
| 50 |
} |
| 51 |
|
| 52 |
$config = App::getInstance()->config; |
| 53 |
$manifest = $config->get('app.manifest'); |
| 54 |
|
| 55 |
if(!$manifest) { |
| 56 |
throw new \Exception('Manifest config could not be found on the app config'); |
| 57 |
} |
| 58 |
|
| 59 |
return $manifest; |
| 60 |
} |
| 61 |
|
| 62 |
private static function addModuleToScript() |
| 63 |
{ |
| 64 |
add_filter('script_loader_tag', function ($tag, $handle, $src) { |
| 65 |
return $tag; |
| 66 |
}, 10, 3); |
| 67 |
} |
| 68 |
|
| 69 |
private static function generateSrc($src, $isRtl = null) |
| 70 |
{ |
| 71 |
if (!static::isDev()) { |
| 72 |
$manifest = static::parseManifest(); |
| 73 |
$src = 'src/' . $src; |
| 74 |
$mainSrc = isset($manifest[$src]) ? $manifest[$src] : false; |
| 75 |
|
| 76 |
if ($mainSrc) { |
| 77 |
if (isset($mainSrc['css'])) { |
| 78 |
foreach ($mainSrc['css'] as $css) { |
| 79 |
wp_enqueue_style($css . '_css', static::$assetsURL . $css, [], '1.0.0', 'all'); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
if($isRtl) { |
| 84 |
$file = $manifest[$src]['file']; |
| 85 |
$file = str_replace('.css', '.rtl.css', $file); |
| 86 |
return static::$assetsURL . $file; |
| 87 |
} |
| 88 |
|
| 89 |
return static::$assetsURL . $manifest[$src]['file']; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
return static::$resourceURL . $src; |
| 94 |
} |
| 95 |
|
| 96 |
public static function getStaticSrcUrl($src) |
| 97 |
{ |
| 98 |
if (!static::isDev()) { |
| 99 |
return static::$assetsURL . $src; |
| 100 |
} |
| 101 |
|
| 102 |
return static::$resourceURL . $src; |
| 103 |
} |
| 104 |
|
| 105 |
public static function getDynamicSrcUrl($src, $isRtl = null) |
| 106 |
{ |
| 107 |
return static::generateSrc($src, $isRtl); |
| 108 |
} |
| 109 |
|
| 110 |
public static function getDynamicProductionSrcUrl($src, $isRtl = null) |
| 111 |
{ |
| 112 |
if (!static::isDev()) { |
| 113 |
return static::generateSrc($src, $isRtl); |
| 114 |
} |
| 115 |
|
| 116 |
return false; |
| 117 |
} |
| 118 |
|
| 119 |
public static function getAssetsUrl() |
| 120 |
{ |
| 121 |
if (!static::isDev()) { |
| 122 |
return static::$assetsURL; |
| 123 |
} |
| 124 |
return static::$resourceURL; |
| 125 |
} |
| 126 |
} |
| 127 |
|