| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCommunity\App; |
| 4 |
|
| 5 |
use FluentCommunity\App\Functions\Utility; |
| 6 |
|
| 7 |
class Vite |
| 8 |
{ |
| 9 |
protected static $moduleScripts = []; |
| 10 |
protected static $resourceURL = 'https://localhost:4444/src/'; |
| 11 |
protected static $assetsURL = FLUENT_COMMUNITY_PLUGIN_URL . 'assets/'; |
| 12 |
|
| 13 |
private static function isDev() |
| 14 |
{ |
| 15 |
return Utility::isDev(); |
| 16 |
} |
| 17 |
|
| 18 |
public static function enqueueScript($handle, $src, $dependency = [], $version = false, $inFooter = false) |
| 19 |
{ |
| 20 |
static::$moduleScripts [] = $handle; |
| 21 |
$src = self::generateSrc($src); |
| 22 |
wp_enqueue_script($handle, $src, $dependency, $version, $inFooter); |
| 23 |
self::addModuleToScript(); |
| 24 |
} |
| 25 |
|
| 26 |
public static function enqueueStyle($handle, $src, $dependency = [], $version = false, $media = 'all') |
| 27 |
{ |
| 28 |
$src = self::generateSrc($src); |
| 29 |
wp_enqueue_style($handle, $src, $dependency, $version, $media); |
| 30 |
} |
| 31 |
|
| 32 |
public static function enqueueStaticScript($handle, $src, $dependency = [], $version = false, $inFooter = false) |
| 33 |
{ |
| 34 |
$src = static::getStaticSrcUrl($src); |
| 35 |
wp_enqueue_script($handle, $src, $dependency, $version, $inFooter); |
| 36 |
} |
| 37 |
|
| 38 |
public static function enqueueStaticStyle($handle, $src, $dependency = [], $version = false, $media = 'all') |
| 39 |
{ |
| 40 |
$src = static::getStaticSrcUrl($src); |
| 41 |
wp_enqueue_style($handle, $src, $dependency, $version, $media); |
| 42 |
} |
| 43 |
|
| 44 |
private static function parseManifest() |
| 45 |
{ |
| 46 |
static $manifest; |
| 47 |
if ($manifest) { |
| 48 |
return $manifest; |
| 49 |
} |
| 50 |
|
| 51 |
$config = App::getInstance()->config; |
| 52 |
$manifest = $config->get('app.manifest'); |
| 53 |
|
| 54 |
if (!$manifest) { |
| 55 |
throw new \Exception('Manifest config could not be found on the app config'); |
| 56 |
} |
| 57 |
|
| 58 |
return $manifest; |
| 59 |
} |
| 60 |
|
| 61 |
private static function addModuleToScript() |
| 62 |
{ |
| 63 |
add_filter('script_loader_tag', function ($tag, $handle, $src) { |
| 64 |
return $tag; |
| 65 |
}, 10, 3); |
| 66 |
} |
| 67 |
|
| 68 |
private static function generateSrc($src, $isRtl = null) |
| 69 |
{ |
| 70 |
if (!self::isDev()) { |
| 71 |
$manifest = self::parseManifest(); |
| 72 |
$src = 'src/' . $src; |
| 73 |
$mainSrc = isset($manifest[$src]) ? $manifest[$src] : false; |
| 74 |
|
| 75 |
if ($mainSrc) { |
| 76 |
if (isset($mainSrc['css'])) { |
| 77 |
foreach ($mainSrc['css'] as $css) { |
| 78 |
wp_enqueue_style($css . '_css', static::$assetsURL . $css, [], '1.0.0', 'all'); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
if ($isRtl) { |
| 83 |
$file = $manifest[$src]['file']; |
| 84 |
$file = str_replace('.css', '.rtl.css', $file); |
| 85 |
return static::$assetsURL . $file; |
| 86 |
} |
| 87 |
|
| 88 |
return static::$assetsURL . $manifest[$src]['file']; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
return static::$resourceURL . $src; |
| 93 |
} |
| 94 |
|
| 95 |
public static function getStaticSrcUrl($src, $isRtl = null) |
| 96 |
{ |
| 97 |
if ($isRtl) { |
| 98 |
$src = str_replace('.css', '.rtl.css', $src); |
| 99 |
} |
| 100 |
|
| 101 |
return static::$assetsURL . $src; |
| 102 |
} |
| 103 |
|
| 104 |
public static function getDynamicSrcUrl($src, $isRtl = null) |
| 105 |
{ |
| 106 |
return self::generateSrc($src, $isRtl); |
| 107 |
} |
| 108 |
|
| 109 |
public static function getDynamicProductionSrcUrl($src, $isRtl = null) |
| 110 |
{ |
| 111 |
if (!self::isDev()) { |
| 112 |
return self::generateSrc($src, $isRtl); |
| 113 |
} |
| 114 |
|
| 115 |
return false; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Build the URL an entry script is loaded with from HTML. |
| 120 |
* |
| 121 |
* The build rewrites every inter-chunk import to `<chunk>.js?ver=<version>` |
| 122 |
* (see src/dev/version-plugin.js), and wp_enqueue_script() emits the same |
| 123 |
* `?ver=` key. HTML script tags must match it exactly: to the browser |
| 124 |
* `app.js?version=x` and `app.js?ver=x` are two different modules, so a |
| 125 |
* mismatch evaluates the whole app a second time and remounts it. |
| 126 |
* |
| 127 |
* @param string $url |
| 128 |
* @return string |
| 129 |
*/ |
| 130 |
public static function versionedUrl($url) |
| 131 |
{ |
| 132 |
return $url . '?ver=' . FLUENT_COMMUNITY_PLUGIN_VERSION; |
| 133 |
} |
| 134 |
|
| 135 |
public static function getAssetsUrl() |
| 136 |
{ |
| 137 |
if (!self::isDev()) { |
| 138 |
return static::$assetsURL; |
| 139 |
} |
| 140 |
return static::$resourceURL; |
| 141 |
} |
| 142 |
} |
| 143 |
|