| @@ -1,33 +1,32 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace FluentCommunity\App; |
| 4 | 4 | |
| 5 | -use FluentCommunity\App\Services\Helper; | |
| 5 | +use FluentCommunity\App\Functions\Utility; | |
| 6 | 6 | |
| 7 | 7 | class Vite |
| 8 | 8 | { |
| 9 | 9 | protected static $moduleScripts = []; |
| 10 | - protected static $resourceURL = 'http://localhost:4444/src/'; | |
| 10 | + protected static $resourceURL = 'https://localhost:4444/src/'; | |
| 11 | 11 | protected static $assetsURL = FLUENT_COMMUNITY_PLUGIN_URL . 'assets/'; |
| 12 | 12 | |
| 13 | 13 | private static function isDev() |
| 14 | 14 | { |
| 15 | - $config = App::getInstance()->config; | |
| 16 | - return $config->get('app.env') === 'dev'; | |
| 15 | + return Utility::isDev(); | |
| 17 | 16 | } |
| 18 | 17 | |
| 19 | 18 | public static function enqueueScript($handle, $src, $dependency = [], $version = false, $inFooter = false) |
| 20 | 19 | { |
| 21 | 20 | static::$moduleScripts [] = $handle; |
| 22 | - $src = static::generateSrc($src); | |
| 21 | + $src = self::generateSrc($src); | |
| 23 | 22 | wp_enqueue_script($handle, $src, $dependency, $version, $inFooter); |
| 24 | - static::addModuleToScript(); | |
| 23 | + self::addModuleToScript(); | |
| 25 | 24 | } |
| 26 | 25 | |
| 27 | 26 | public static function enqueueStyle($handle, $src, $dependency = [], $version = false, $media = 'all') |
| 28 | 27 | { |
| 29 | - $src = static::generateSrc($src); | |
| 28 | + $src = self::generateSrc($src); | |
| 30 | 29 | wp_enqueue_style($handle, $src, $dependency, $version, $media); |
| 31 | 30 | } |
| 32 | 31 | |
| 33 | 32 | public static function enqueueStaticScript($handle, $src, $dependency = [], $version = false, $inFooter = false) |
| @@ -51,9 +50,9 @@ | ||
| 51 | 50 | |
| 52 | 51 | $config = App::getInstance()->config; |
| 53 | 52 | $manifest = $config->get('app.manifest'); |
| 54 | 53 | |
| 55 | - if(!$manifest) { | |
| 54 | + if (!$manifest) { | |
| 56 | 55 | throw new \Exception('Manifest config could not be found on the app config'); |
| 57 | 56 | } |
| 58 | 57 | |
| 59 | 58 | return $manifest; |
| @@ -67,10 +66,10 @@ | ||
| 67 | 66 | } |
| 68 | 67 | |
| 69 | 68 | private static function generateSrc($src, $isRtl = null) |
| 70 | 69 | { |
| 71 | - if (!static::isDev()) { | |
| 72 | - $manifest = static::parseManifest(); | |
| 70 | + if (!self::isDev()) { | |
| 71 | + $manifest = self::parseManifest(); | |
| 73 | 72 | $src = 'src/' . $src; |
| 74 | 73 | $mainSrc = isset($manifest[$src]) ? $manifest[$src] : false; |
| 75 | 74 | |
| 76 | 75 | if ($mainSrc) { |
| @@ -79,9 +78,9 @@ | ||
| 79 | 78 | wp_enqueue_style($css . '_css', static::$assetsURL . $css, [], '1.0.0', 'all'); |
| 80 | 79 | } |
| 81 | 80 | } |
| 82 | 81 | |
| 83 | - if($isRtl) { | |
| 82 | + if ($isRtl) { | |
| 84 | 83 | $file = $manifest[$src]['file']; |
| 85 | 84 | $file = str_replace('.css', '.rtl.css', $file); |
| 86 | 85 | return static::$assetsURL . $file; |
| 87 | 86 | } |
| @@ -92,34 +91,51 @@ | ||
| 92 | 91 | |
| 93 | 92 | return static::$resourceURL . $src; |
| 94 | 93 | } |
| 95 | 94 | |
| 96 | - public static function getStaticSrcUrl($src) | |
| 95 | + public static function getStaticSrcUrl($src, $isRtl = null) | |
| 97 | 96 | { |
| 98 | - if (!static::isDev()) { | |
| 99 | - return static::$assetsURL . $src; | |
| 97 | + if ($isRtl) { | |
| 98 | + $src = str_replace('.css', '.rtl.css', $src); | |
| 100 | 99 | } |
| 101 | 100 | |
| 102 | - return static::$resourceURL . $src; | |
| 101 | + return static::$assetsURL . $src; | |
| 103 | 102 | } |
| 104 | 103 | |
| 105 | 104 | public static function getDynamicSrcUrl($src, $isRtl = null) |
| 106 | 105 | { |
| 107 | - return static::generateSrc($src, $isRtl); | |
| 106 | + return self::generateSrc($src, $isRtl); | |
| 108 | 107 | } |
| 109 | 108 | |
| 110 | 109 | public static function getDynamicProductionSrcUrl($src, $isRtl = null) |
| 111 | 110 | { |
| 112 | - if (!static::isDev()) { | |
| 113 | - return static::generateSrc($src, $isRtl); | |
| 111 | + if (!self::isDev()) { | |
| 112 | + return self::generateSrc($src, $isRtl); | |
| 114 | 113 | } |
| 115 | 114 | |
| 116 | 115 | return false; |
| 117 | 116 | } |
| 118 | 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 | + | |
| 119 | 135 | public static function getAssetsUrl() |
| 120 | 136 | { |
| 121 | - if (!static::isDev()) { | |
| 137 | + if (!self::isDev()) { | |
| 122 | 138 | return static::$assetsURL; |
| 123 | 139 | } |
| 124 | 140 | return static::$resourceURL; |
| 125 | 141 | } |