From 748664d46c355b71adbd93c0d5c170166c7582d3 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Dec 2024 13:58:08 +0100 Subject: [PATCH 01/30] Remove unnecessary comment --- packages/framework/src/Facades/Vite.php | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/framework/src/Facades/Vite.php b/packages/framework/src/Facades/Vite.php index d93f60f742d..af0be16bfe6 100644 --- a/packages/framework/src/Facades/Vite.php +++ b/packages/framework/src/Facades/Vite.php @@ -18,7 +18,6 @@ public static function running(): bool return true; } - // Check for Vite hot file return Filesystem::exists('app/storage/framework/cache/vite.hot'); } From df47004d5f43b4fa667cff6d84f4a38860a0a3de Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Dec 2024 13:58:24 +0100 Subject: [PATCH 02/30] Unwrap function call --- packages/framework/src/Facades/Vite.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Facades/Vite.php b/packages/framework/src/Facades/Vite.php index af0be16bfe6..a1f7e0a3193 100644 --- a/packages/framework/src/Facades/Vite.php +++ b/packages/framework/src/Facades/Vite.php @@ -23,7 +23,7 @@ public static function running(): bool public static function assets(array $paths): HtmlString { - $html = sprintf(''); + $html = ''; foreach ($paths as $path) { if (str_ends_with($path, '.css')) { From f0029d1e8baebdff926b0864edc2a40c861c88ce Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Dec 2024 14:00:57 +0100 Subject: [PATCH 03/30] Extract helper method from code comment --- packages/framework/src/Facades/Vite.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/framework/src/Facades/Vite.php b/packages/framework/src/Facades/Vite.php index a1f7e0a3193..27441fdcd53 100644 --- a/packages/framework/src/Facades/Vite.php +++ b/packages/framework/src/Facades/Vite.php @@ -13,12 +13,7 @@ class Vite { public static function running(): bool { - // Check if Vite was enabled via the serve command - if (env('HYDE_SERVER_VITE') === 'enabled') { - return true; - } - - return Filesystem::exists('app/storage/framework/cache/vite.hot'); + return self::checkIfViteWasEnabledViaTheServeCommand() || Filesystem::exists('app/storage/framework/cache/vite.hot'); } public static function assets(array $paths): HtmlString @@ -37,4 +32,9 @@ public static function assets(array $paths): HtmlString return new HtmlString($html); } + + protected static function checkIfViteWasEnabledViaTheServeCommand(): bool + { + return env('HYDE_SERVER_VITE') === 'enabled'; + } } From dece09711653a55aaf7bc0e6a74652058d770e3c Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Dec 2024 14:02:50 +0100 Subject: [PATCH 04/30] Add strict types declaration --- packages/framework/tests/Unit/Facades/ViteFacadeTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/framework/tests/Unit/Facades/ViteFacadeTest.php b/packages/framework/tests/Unit/Facades/ViteFacadeTest.php index 2219d319ceb..e8c32b3eb65 100644 --- a/packages/framework/tests/Unit/Facades/ViteFacadeTest.php +++ b/packages/framework/tests/Unit/Facades/ViteFacadeTest.php @@ -1,5 +1,7 @@ Date: Sun, 8 Dec 2024 14:06:12 +0100 Subject: [PATCH 05/30] Convert concatenation to a scalar values --- .../framework/tests/Unit/Facades/ViteFacadeTest.php | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/packages/framework/tests/Unit/Facades/ViteFacadeTest.php b/packages/framework/tests/Unit/Facades/ViteFacadeTest.php index e8c32b3eb65..a429e9494ce 100644 --- a/packages/framework/tests/Unit/Facades/ViteFacadeTest.php +++ b/packages/framework/tests/Unit/Facades/ViteFacadeTest.php @@ -47,8 +47,7 @@ public function testAssetsMethodGeneratesCorrectHtmlForJavaScriptFiles() { $html = Vite::assets(['resources/js/app.js']); - $expected = '' - .''; + $expected = ''; $this->assertSame($expected, (string) $html); } @@ -57,8 +56,7 @@ public function testAssetsMethodGeneratesCorrectHtmlForCssFiles() { $html = Vite::assets(['resources/css/app.css']); - $expected = '' - .''; + $expected = ''; $this->assertSame($expected, (string) $html); } @@ -71,10 +69,7 @@ public function testAssetsMethodGeneratesCorrectHtmlForMultipleFiles() 'resources/js/other.js', ]); - $expected = '' - .'' - .'' - .''; + $expected = ''; $this->assertSame($expected, (string) $html); } From 4f5d228460c3161a2d2cdf6d5e62d58398023871 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Dec 2024 14:06:16 +0100 Subject: [PATCH 06/30] Replace qualifier with an import --- packages/framework/tests/Unit/Facades/ViteFacadeTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/framework/tests/Unit/Facades/ViteFacadeTest.php b/packages/framework/tests/Unit/Facades/ViteFacadeTest.php index a429e9494ce..90247deddf1 100644 --- a/packages/framework/tests/Unit/Facades/ViteFacadeTest.php +++ b/packages/framework/tests/Unit/Facades/ViteFacadeTest.php @@ -7,6 +7,7 @@ use Hyde\Testing\UnitTestCase; use Hyde\Facades\Vite; use Hyde\Testing\CreatesTemporaryFiles; +use Illuminate\Support\HtmlString; /** * @covers \Hyde\Facades\Vite @@ -76,6 +77,6 @@ public function testAssetsMethodGeneratesCorrectHtmlForMultipleFiles() public function testAssetsMethodReturnsHtmlString() { - $this->assertInstanceOf(\Illuminate\Support\HtmlString::class, Vite::assets([])); + $this->assertInstanceOf(HtmlString::class, Vite::assets([])); } } From 5e653a93803406971cac5edbef71fde5d58286d8 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Dec 2024 14:07:31 +0100 Subject: [PATCH 07/30] Add inverse test --- .../framework/tests/Unit/Facades/ViteFacadeTest.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/packages/framework/tests/Unit/Facades/ViteFacadeTest.php b/packages/framework/tests/Unit/Facades/ViteFacadeTest.php index 90247deddf1..76cb606ce16 100644 --- a/packages/framework/tests/Unit/Facades/ViteFacadeTest.php +++ b/packages/framework/tests/Unit/Facades/ViteFacadeTest.php @@ -31,6 +31,19 @@ public function testRunningReturnsTrueWhenEnvironmentVariableIsSet() putenv('HYDE_SERVER_VITE'); } + + public function testRunningReturnsFalseWhenEnvironmentVariableIsNotSetOrDisabled() + { + $this->assertFalse(Vite::running()); + + putenv('HYDE_SERVER_VITE=disabled'); + + $this->assertFalse(Vite::running()); + + putenv('HYDE_SERVER_VITE'); + + $this->assertFalse(Vite::running()); + } public function testRunningReturnsTrueWhenViteHotFileExists() { From 5508d358a746c817019e1dcec4974d2da983b761 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Dec 2024 14:08:16 +0100 Subject: [PATCH 08/30] Improve confidence of test --- packages/framework/tests/Unit/Facades/ViteFacadeTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/framework/tests/Unit/Facades/ViteFacadeTest.php b/packages/framework/tests/Unit/Facades/ViteFacadeTest.php index 76cb606ce16..90c5e06481c 100644 --- a/packages/framework/tests/Unit/Facades/ViteFacadeTest.php +++ b/packages/framework/tests/Unit/Facades/ViteFacadeTest.php @@ -54,6 +54,8 @@ public function testRunningReturnsTrueWhenViteHotFileExists() public function testRunningReturnsFalseWhenViteHotFileDoesNotExist() { + $this->assertFileDoesNotExist('app/storage/framework/cache/vite.hot'); + $this->assertFalse(Vite::running()); } From 079913d8a37306cb87a557f134dbcde4ff0c34e2 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Dec 2024 14:11:57 +0100 Subject: [PATCH 09/30] Move up and improve test --- .../tests/Unit/Facades/ViteFacadeTest.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/framework/tests/Unit/Facades/ViteFacadeTest.php b/packages/framework/tests/Unit/Facades/ViteFacadeTest.php index 90c5e06481c..6c0cbfac33f 100644 --- a/packages/framework/tests/Unit/Facades/ViteFacadeTest.php +++ b/packages/framework/tests/Unit/Facades/ViteFacadeTest.php @@ -59,6 +59,15 @@ public function testRunningReturnsFalseWhenViteHotFileDoesNotExist() $this->assertFalse(Vite::running()); } + public function testAssetsMethodReturnsHtmlString() + { + $this->assertInstanceOf(HtmlString::class, Vite::assets([])); + $this->assertInstanceOf(HtmlString::class, Vite::assets(['foo.js'])); + + $this->assertEquals(new HtmlString(''), Vite::assets([])); + $this->assertEquals(new HtmlString(''), Vite::assets(['foo.js'])); + } + public function testAssetsMethodGeneratesCorrectHtmlForJavaScriptFiles() { $html = Vite::assets(['resources/js/app.js']); @@ -89,9 +98,4 @@ public function testAssetsMethodGeneratesCorrectHtmlForMultipleFiles() $this->assertSame($expected, (string) $html); } - - public function testAssetsMethodReturnsHtmlString() - { - $this->assertInstanceOf(HtmlString::class, Vite::assets([])); - } } From ecbf8bdd06459e19d59ab2851d91eaae59e611e5 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Dec 2024 14:12:04 +0100 Subject: [PATCH 10/30] Extract helper methods --- packages/framework/src/Facades/Vite.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Facades/Vite.php b/packages/framework/src/Facades/Vite.php index 27441fdcd53..cadc8ec6dfc 100644 --- a/packages/framework/src/Facades/Vite.php +++ b/packages/framework/src/Facades/Vite.php @@ -22,11 +22,11 @@ public static function assets(array $paths): HtmlString foreach ($paths as $path) { if (str_ends_with($path, '.css')) { - $html .= sprintf('', $path); + $html .= static::formatStylesheetLink($path); } if (str_ends_with($path, '.js')) { - $html .= sprintf('', $path); + $html .= static::formatScriptInclude($path); } } @@ -37,4 +37,14 @@ protected static function checkIfViteWasEnabledViaTheServeCommand(): bool { return env('HYDE_SERVER_VITE') === 'enabled'; } + + protected static function formatStylesheetLink(string $path): string + { + return sprintf('', $path); + } + + protected static function formatScriptInclude(string $path): string + { + return sprintf('', $path); + } } From 029a0c6ba6cfbe29cc9539d6af8f07329ea1f187 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Sun, 8 Dec 2024 13:12:14 +0000 Subject: [PATCH 11/30] Apply fixes from StyleCI --- packages/framework/tests/Unit/Facades/ViteFacadeTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/tests/Unit/Facades/ViteFacadeTest.php b/packages/framework/tests/Unit/Facades/ViteFacadeTest.php index 6c0cbfac33f..27397aeac9c 100644 --- a/packages/framework/tests/Unit/Facades/ViteFacadeTest.php +++ b/packages/framework/tests/Unit/Facades/ViteFacadeTest.php @@ -31,7 +31,7 @@ public function testRunningReturnsTrueWhenEnvironmentVariableIsSet() putenv('HYDE_SERVER_VITE'); } - + public function testRunningReturnsFalseWhenEnvironmentVariableIsNotSetOrDisabled() { $this->assertFalse(Vite::running()); From aab895a5faa01a1883c2e5bd20a9d358fd03dd06 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Dec 2024 14:19:25 +0100 Subject: [PATCH 12/30] Add todo --- packages/framework/src/Facades/Vite.php | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/framework/src/Facades/Vite.php b/packages/framework/src/Facades/Vite.php index cadc8ec6dfc..991e2b4322c 100644 --- a/packages/framework/src/Facades/Vite.php +++ b/packages/framework/src/Facades/Vite.php @@ -35,6 +35,7 @@ public static function assets(array $paths): HtmlString protected static function checkIfViteWasEnabledViaTheServeCommand(): bool { + // TODO: Do we actually need this? Hotfile should be enough. return env('HYDE_SERVER_VITE') === 'enabled'; } From 3501102f51f97f3d30462a30f7eef3e0fde2d19b Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Dec 2024 14:22:07 +0100 Subject: [PATCH 13/30] Annotate the array types --- packages/framework/src/Facades/Vite.php | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/framework/src/Facades/Vite.php b/packages/framework/src/Facades/Vite.php index 991e2b4322c..e0b5336ed07 100644 --- a/packages/framework/src/Facades/Vite.php +++ b/packages/framework/src/Facades/Vite.php @@ -16,6 +16,7 @@ public static function running(): bool return self::checkIfViteWasEnabledViaTheServeCommand() || Filesystem::exists('app/storage/framework/cache/vite.hot'); } + /** @param array $paths */ public static function assets(array $paths): HtmlString { $html = ''; From 0e940568df15e540a58ea7367318576340b9be32 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Dec 2024 14:23:14 +0100 Subject: [PATCH 14/30] Extract helper methods --- packages/framework/src/Facades/Vite.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Facades/Vite.php b/packages/framework/src/Facades/Vite.php index e0b5336ed07..2ff21c2ab67 100644 --- a/packages/framework/src/Facades/Vite.php +++ b/packages/framework/src/Facades/Vite.php @@ -22,11 +22,11 @@ public static function assets(array $paths): HtmlString $html = ''; foreach ($paths as $path) { - if (str_ends_with($path, '.css')) { + if (self::isCssPath($path)) { $html .= static::formatStylesheetLink($path); } - if (str_ends_with($path, '.js')) { + if (self::isJsPath($path)) { $html .= static::formatScriptInclude($path); } } @@ -40,6 +40,16 @@ protected static function checkIfViteWasEnabledViaTheServeCommand(): bool return env('HYDE_SERVER_VITE') === 'enabled'; } + protected static function isCssPath(string $path): bool + { + return str_ends_with($path, '.css'); + } + + protected static function isJsPath(string $path): bool + { + return str_ends_with($path, '.js'); + } + protected static function formatStylesheetLink(string $path): string { return sprintf('', $path); From 7e0016bad1ac260d556b8bb6d510c804f1115d18 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Dec 2024 14:45:44 +0100 Subject: [PATCH 15/30] Better document the Vite system --- docs/creating-content/managing-assets.md | 51 ++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/docs/creating-content/managing-assets.md b/docs/creating-content/managing-assets.md index dc84cc013d5..0b06e747b1c 100644 --- a/docs/creating-content/managing-assets.md +++ b/docs/creating-content/managing-assets.md @@ -18,6 +18,57 @@ Some extra component styles are organized into modular files in the HydeFront pa To get you started quickly, all the styles are already compiled and minified into `_media/app.css`, which will be copied to the `_site/media/app.css` directory when you run `php hyde build`. +## Vite + +Hyde uses [Vite](https://vitejs.dev/) to compile assets. Vite is a build tool that aims to provide a faster and more efficient development experience for modern web projects. + +### Why Vite? + +HydePHP integrates Vite to compile assets such as CSS and JavaScript files. This integration ensures that your assets are processed efficiently, enhancing the development workflow by leveraging Vite's rapid build system. + +#### Asset Management + +**Development and Production Modes** + +- **Development Mode**: Use `npm run dev` to start the Vite development HMR server, which provides fast live reloading and efficient compilation during development. +- **Production Mode**: Use `npm run build` for creating optimized, minified asset bundles ready for production deployment. + +**Asset Compilation**: + +- Assets are compiled from the `resources/assets` directory. The primary CSS file, `app.css`, is processed with TailwindCSS and other specified tools like PostCSS. +- Vite automatically processes all scripts and styles, outputting compiled files to the `_media` directory. These are copied to `_site/media` when the static site is built with `php hyde build`. + +>warn Note that the HydePHP Vite integration only supports CSS and JavaScript files, if you try to load other file types, they will not be processed by Vite. + +**Configuration**: +- You can customize Vite's behavior and output paths by modifying the pre-configured `vite.config.js` file in the project root directory. + +### Blade Integration + +Hyde effortlessly integrates Vite with Blade views, allowing you to include compiled assets in your templates. The Blade components `hyde::layouts.styles` and `hyde::layouts.scripts` are already set up to load the compiled CSS and JavaScript files. + +You can check if the Vite HMR server is running with `Vite::running()`, and you can include CSS and JavaScript resources with `Vite::asset('path')`, or `Vite::assets([])` to supply an array of paths. + +**Example: Using Vite if the HMR server is enabled, or loading the compiled CSS file if not:** + +```blade +@if(Vite::running()) + {{ Vite::assets(['resources/assets/app.css']) }} +@else + +@endif +``` + +### Hot Module Replacement (HMR) + +Vite's HMR feature allows for instant updates to the browser without requiring a full page reload. This **only works** through the realtime compiler when the Vite development server is also running. + +You can start both of these by running `npm run dev` and `php hyde serve` in separate terminals, or using the `--vite` flag with the serve command: + +```bash +php hyde serve --vite +``` + ## Additional Information and Answers to Common Questions ### Is NodeJS/NPM Required for Using Hyde? From 30b09393f83c7b8ad4349394e87b5a4d2834b0c6 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Dec 2024 15:06:40 +0100 Subject: [PATCH 16/30] Swap section order --- docs/creating-content/managing-assets.md | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/docs/creating-content/managing-assets.md b/docs/creating-content/managing-assets.md index 0b06e747b1c..e4db6fee431 100644 --- a/docs/creating-content/managing-assets.md +++ b/docs/creating-content/managing-assets.md @@ -43,6 +43,16 @@ HydePHP integrates Vite to compile assets such as CSS and JavaScript files. This **Configuration**: - You can customize Vite's behavior and output paths by modifying the pre-configured `vite.config.js` file in the project root directory. +### Hot Module Replacement (HMR) + +Vite's HMR feature allows for instant updates to the browser without requiring a full page reload. This **only works** through the realtime compiler when the Vite development server is also running. + +You can start both of these by running `npm run dev` and `php hyde serve` in separate terminals, or using the `--vite` flag with the serve command: + +```bash +php hyde serve --vite +``` + ### Blade Integration Hyde effortlessly integrates Vite with Blade views, allowing you to include compiled assets in your templates. The Blade components `hyde::layouts.styles` and `hyde::layouts.scripts` are already set up to load the compiled CSS and JavaScript files. @@ -59,16 +69,6 @@ You can check if the Vite HMR server is running with `Vite::running()`, and you @endif ``` -### Hot Module Replacement (HMR) - -Vite's HMR feature allows for instant updates to the browser without requiring a full page reload. This **only works** through the realtime compiler when the Vite development server is also running. - -You can start both of these by running `npm run dev` and `php hyde serve` in separate terminals, or using the `--vite` flag with the serve command: - -```bash -php hyde serve --vite -``` - ## Additional Information and Answers to Common Questions ### Is NodeJS/NPM Required for Using Hyde? From a511976954807e492e533b5c39cb3d2170884e61 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Dec 2024 16:27:47 +0100 Subject: [PATCH 17/30] Support all CSS extensions like Laravel Vite uses --- packages/framework/src/Facades/Vite.php | 2 +- .../tests/Unit/Facades/ViteFacadeTest.php | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Facades/Vite.php b/packages/framework/src/Facades/Vite.php index 2ff21c2ab67..d45c21aaf24 100644 --- a/packages/framework/src/Facades/Vite.php +++ b/packages/framework/src/Facades/Vite.php @@ -42,7 +42,7 @@ protected static function checkIfViteWasEnabledViaTheServeCommand(): bool protected static function isCssPath(string $path): bool { - return str_ends_with($path, '.css'); + return preg_match('/\.(css|less|sass|scss|styl|stylus|pcss|postcss)$/', $path) === 1; } protected static function isJsPath(string $path): bool diff --git a/packages/framework/tests/Unit/Facades/ViteFacadeTest.php b/packages/framework/tests/Unit/Facades/ViteFacadeTest.php index 27397aeac9c..fa10f82300e 100644 --- a/packages/framework/tests/Unit/Facades/ViteFacadeTest.php +++ b/packages/framework/tests/Unit/Facades/ViteFacadeTest.php @@ -98,4 +98,21 @@ public function testAssetsMethodGeneratesCorrectHtmlForMultipleFiles() $this->assertSame($expected, (string) $html); } + + /** + * @dataProvider cssFileExtensions + */ + public function testAssetsMethodSupportsAllCssFileExtensions(string $extension) + { + $html = Vite::assets(["resources/css/app.$extension"]); + + $expected = ''; + + $this->assertSame($expected, (string) $html); + } + + public static function cssFileExtensions(): array + { + return array_map(fn (string $ext): array => [$ext], ['css', 'less', 'sass', 'scss', 'styl', 'stylus', 'pcss', 'postcss']); + } } From 7230be56e385485274325a05d548d6fca25a1967 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Dec 2024 16:32:42 +0100 Subject: [PATCH 18/30] Improve accuracy of test --- .../tests/Unit/Facades/ViteFacadeTest.php | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/packages/framework/tests/Unit/Facades/ViteFacadeTest.php b/packages/framework/tests/Unit/Facades/ViteFacadeTest.php index fa10f82300e..60d2e7f630c 100644 --- a/packages/framework/tests/Unit/Facades/ViteFacadeTest.php +++ b/packages/framework/tests/Unit/Facades/ViteFacadeTest.php @@ -100,19 +100,33 @@ public function testAssetsMethodGeneratesCorrectHtmlForMultipleFiles() } /** - * @dataProvider cssFileExtensions + * @dataProvider cssFileExtensionsProvider */ - public function testAssetsMethodSupportsAllCssFileExtensions(string $extension) + public function testAssetsMethodSupportsAllCssFileExtensions(string $extension, bool $shouldBeUsed) { $html = Vite::assets(["resources/css/app.$extension"]); - $expected = ''; + $expected = $shouldBeUsed + ? '' + : ''; $this->assertSame($expected, (string) $html); } - public static function cssFileExtensions(): array + public static function cssFileExtensionsProvider(): array { - return array_map(fn (string $ext): array => [$ext], ['css', 'less', 'sass', 'scss', 'styl', 'stylus', 'pcss', 'postcss']); + return [ + ['css', true], + ['less', true], + ['sass', true], + ['scss', true], + ['styl', true], + ['stylus', true], + ['pcss', true], + ['postcss', true], + ['foo', false], + ['txt', false], + ['html', false], + ]; } } From 59979e3982b452fad4331a9b8ff7b9e6b6cfe008 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Dec 2024 16:54:20 +0100 Subject: [PATCH 19/30] Use else if instead of if --- packages/framework/src/Facades/Vite.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/framework/src/Facades/Vite.php b/packages/framework/src/Facades/Vite.php index d45c21aaf24..c9a80ff3edf 100644 --- a/packages/framework/src/Facades/Vite.php +++ b/packages/framework/src/Facades/Vite.php @@ -24,9 +24,7 @@ public static function assets(array $paths): HtmlString foreach ($paths as $path) { if (self::isCssPath($path)) { $html .= static::formatStylesheetLink($path); - } - - if (self::isJsPath($path)) { + } elseif (self::isJsPath($path)) { $html .= static::formatScriptInclude($path); } } From 06d9641094392a1982111014bd8e3d2a1699d60b Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Dec 2024 16:57:13 +0100 Subject: [PATCH 20/30] Revert "Use else if instead of if" This reverts commit 59979e3982b452fad4331a9b8ff7b9e6b6cfe008. --- packages/framework/src/Facades/Vite.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Facades/Vite.php b/packages/framework/src/Facades/Vite.php index c9a80ff3edf..d45c21aaf24 100644 --- a/packages/framework/src/Facades/Vite.php +++ b/packages/framework/src/Facades/Vite.php @@ -24,7 +24,9 @@ public static function assets(array $paths): HtmlString foreach ($paths as $path) { if (self::isCssPath($path)) { $html .= static::formatStylesheetLink($path); - } elseif (self::isJsPath($path)) { + } + + if (self::isJsPath($path)) { $html .= static::formatScriptInclude($path); } } From da2ba15dbf69582e1b182cadf07ea0deeb3582d1 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Dec 2024 16:59:02 +0100 Subject: [PATCH 21/30] Add more tests --- .../tests/Unit/Facades/ViteFacadeTest.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/packages/framework/tests/Unit/Facades/ViteFacadeTest.php b/packages/framework/tests/Unit/Facades/ViteFacadeTest.php index 60d2e7f630c..0f1c8d3c155 100644 --- a/packages/framework/tests/Unit/Facades/ViteFacadeTest.php +++ b/packages/framework/tests/Unit/Facades/ViteFacadeTest.php @@ -59,6 +59,22 @@ public function testRunningReturnsFalseWhenViteHotFileDoesNotExist() $this->assertFalse(Vite::running()); } + public function testItAlwaysImportsClientModule() + { + $html = Vite::assets([]); + + $this->assertStringContainsString('', (string) $html); + + $html = Vite::assets(['foo.js']); + + $this->assertStringContainsString('', (string) $html); + } + + public function testItDoesNotIncludeUnknownExtensions() + { + $this->assertSame((string) Vite::assets([]), (string) Vite::assets(['foo.txt'])); + } + public function testAssetsMethodReturnsHtmlString() { $this->assertInstanceOf(HtmlString::class, Vite::assets([])); From d5ec63d6bf53ebd665cbaf399036821d0517fb21 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Dec 2024 17:03:36 +0100 Subject: [PATCH 22/30] Extract helper to format the asset path --- packages/framework/src/Facades/Vite.php | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/packages/framework/src/Facades/Vite.php b/packages/framework/src/Facades/Vite.php index d45c21aaf24..18f1c35fe5e 100644 --- a/packages/framework/src/Facades/Vite.php +++ b/packages/framework/src/Facades/Vite.php @@ -22,18 +22,25 @@ public static function assets(array $paths): HtmlString $html = ''; foreach ($paths as $path) { - if (self::isCssPath($path)) { - $html .= static::formatStylesheetLink($path); - } - - if (self::isJsPath($path)) { - $html .= static::formatScriptInclude($path); - } + $html .= static::formatAssetPath($path); } return new HtmlString($html); } + protected static function formatAssetPath(string $path): string + { + if (self::isCssPath($path)) { + return static::formatStylesheetLink($path); + } + + if (self::isJsPath($path)) { + return static::formatScriptInclude($path); + } + + return ''; // TODO: Throw an exception? + } + protected static function checkIfViteWasEnabledViaTheServeCommand(): bool { // TODO: Do we actually need this? Hotfile should be enough. From 4bc0f52e05eb614861c4cc842f744cb21461a76c Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Dec 2024 17:06:05 +0100 Subject: [PATCH 23/30] Use static instead of self --- packages/framework/src/Facades/Vite.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/framework/src/Facades/Vite.php b/packages/framework/src/Facades/Vite.php index 18f1c35fe5e..eda0ec78a44 100644 --- a/packages/framework/src/Facades/Vite.php +++ b/packages/framework/src/Facades/Vite.php @@ -13,7 +13,7 @@ class Vite { public static function running(): bool { - return self::checkIfViteWasEnabledViaTheServeCommand() || Filesystem::exists('app/storage/framework/cache/vite.hot'); + return static::checkIfViteWasEnabledViaTheServeCommand() || Filesystem::exists('app/storage/framework/cache/vite.hot'); } /** @param array $paths */ @@ -30,11 +30,11 @@ public static function assets(array $paths): HtmlString protected static function formatAssetPath(string $path): string { - if (self::isCssPath($path)) { + if (static::isCssPath($path)) { return static::formatStylesheetLink($path); } - if (self::isJsPath($path)) { + if (static::isJsPath($path)) { return static::formatScriptInclude($path); } From 8af55eda8866d4d71706687f4111ff22428bc333 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Dec 2024 17:09:21 +0100 Subject: [PATCH 24/30] Add helper to get single asset --- packages/framework/src/Facades/Vite.php | 5 +++ .../tests/Unit/Facades/ViteFacadeTest.php | 32 +++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/packages/framework/src/Facades/Vite.php b/packages/framework/src/Facades/Vite.php index eda0ec78a44..d98fdf50eb1 100644 --- a/packages/framework/src/Facades/Vite.php +++ b/packages/framework/src/Facades/Vite.php @@ -16,6 +16,11 @@ public static function running(): bool return static::checkIfViteWasEnabledViaTheServeCommand() || Filesystem::exists('app/storage/framework/cache/vite.hot'); } + public static function asset(string $path): HtmlString + { + return static::assets([$path]); + } + /** @param array $paths */ public static function assets(array $paths): HtmlString { diff --git a/packages/framework/tests/Unit/Facades/ViteFacadeTest.php b/packages/framework/tests/Unit/Facades/ViteFacadeTest.php index 0f1c8d3c155..df7314c1565 100644 --- a/packages/framework/tests/Unit/Facades/ViteFacadeTest.php +++ b/packages/framework/tests/Unit/Facades/ViteFacadeTest.php @@ -129,6 +129,38 @@ public function testAssetsMethodSupportsAllCssFileExtensions(string $extension, $this->assertSame($expected, (string) $html); } + public function testAssetMethodReturnsHtmlString() + { + $this->assertInstanceOf(HtmlString::class, Vite::asset('foo.js')); + } + + public function testAssetMethodGeneratesCorrectHtmlForJavaScriptFile() + { + $html = Vite::asset('resources/js/app.js'); + + $expected = ''; + + $this->assertSame($expected, (string) $html); + } + + public function testAssetMethodGeneratesCorrectHtmlForCssFile() + { + $html = Vite::asset('resources/css/app.css'); + + $expected = ''; + + $this->assertSame($expected, (string) $html); + } + + public function testAssetMethodDoesNotIncludeUnknownExtensions() + { + $html = Vite::asset('unknown.ext'); + + $expected = ''; + + $this->assertSame($expected, (string) $html); + } + public static function cssFileExtensionsProvider(): array { return [ From fb3865c112caf41c35d2d28941e90de2d5af314b Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Dec 2024 17:36:46 +0100 Subject: [PATCH 25/30] Simplify test --- .../tests/Unit/Facades/ViteFacadeTest.php | 33 ++++++++++--------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/packages/framework/tests/Unit/Facades/ViteFacadeTest.php b/packages/framework/tests/Unit/Facades/ViteFacadeTest.php index df7314c1565..80f223688d9 100644 --- a/packages/framework/tests/Unit/Facades/ViteFacadeTest.php +++ b/packages/framework/tests/Unit/Facades/ViteFacadeTest.php @@ -118,15 +118,18 @@ public function testAssetsMethodGeneratesCorrectHtmlForMultipleFiles() /** * @dataProvider cssFileExtensionsProvider */ - public function testAssetsMethodSupportsAllCssFileExtensions(string $extension, bool $shouldBeUsed) + public function testAssetsMethodSupportsAllCssFileExtensions(string $extension) { $html = Vite::assets(["resources/css/app.$extension"]); - $expected = $shouldBeUsed - ? '' - : ''; + if ($extension !== 'js') { + $expected = ''; - $this->assertSame($expected, (string) $html); + $this->assertStringContainsString('stylesheet', (string) $html); + $this->assertSame($expected, (string) $html); + } else { + $this->assertStringNotContainsString('stylesheet', (string) $html); + } } public function testAssetMethodReturnsHtmlString() @@ -164,17 +167,15 @@ public function testAssetMethodDoesNotIncludeUnknownExtensions() public static function cssFileExtensionsProvider(): array { return [ - ['css', true], - ['less', true], - ['sass', true], - ['scss', true], - ['styl', true], - ['stylus', true], - ['pcss', true], - ['postcss', true], - ['foo', false], - ['txt', false], - ['html', false], + ['css'], + ['less'], + ['sass'], + ['scss'], + ['styl'], + ['stylus'], + ['pcss'], + ['postcss'], + ['js'], ]; } } From bf88379b728105893106375397c59716230f9718 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Dec 2024 17:38:13 +0100 Subject: [PATCH 26/30] Throw exception if the asset type is not supported --- packages/framework/src/Facades/Vite.php | 5 +++- .../tests/Unit/Facades/ViteFacadeTest.php | 25 +++++++++++-------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/packages/framework/src/Facades/Vite.php b/packages/framework/src/Facades/Vite.php index d98fdf50eb1..a3db7dd3608 100644 --- a/packages/framework/src/Facades/Vite.php +++ b/packages/framework/src/Facades/Vite.php @@ -5,6 +5,7 @@ namespace Hyde\Facades; use Illuminate\Support\HtmlString; +use InvalidArgumentException; /** * Vite facade for handling Vite-related operations. @@ -33,6 +34,7 @@ public static function assets(array $paths): HtmlString return new HtmlString($html); } + /** @throws InvalidArgumentException If the asset type is not supported. */ protected static function formatAssetPath(string $path): string { if (static::isCssPath($path)) { @@ -43,7 +45,8 @@ protected static function formatAssetPath(string $path): string return static::formatScriptInclude($path); } - return ''; // TODO: Throw an exception? + // We don't know how to handle other asset types, so we throw an exception to let the user know. + throw new InvalidArgumentException("Unsupported asset type for path: '$path'"); } protected static function checkIfViteWasEnabledViaTheServeCommand(): bool diff --git a/packages/framework/tests/Unit/Facades/ViteFacadeTest.php b/packages/framework/tests/Unit/Facades/ViteFacadeTest.php index 80f223688d9..faa90a4676a 100644 --- a/packages/framework/tests/Unit/Facades/ViteFacadeTest.php +++ b/packages/framework/tests/Unit/Facades/ViteFacadeTest.php @@ -8,6 +8,7 @@ use Hyde\Facades\Vite; use Hyde\Testing\CreatesTemporaryFiles; use Illuminate\Support\HtmlString; +use InvalidArgumentException; /** * @covers \Hyde\Facades\Vite @@ -70,9 +71,20 @@ public function testItAlwaysImportsClientModule() $this->assertStringContainsString('', (string) $html); } - public function testItDoesNotIncludeUnknownExtensions() + public function testAssetMethodThrowsExceptionForUnknownExtensions() { - $this->assertSame((string) Vite::assets([]), (string) Vite::assets(['foo.txt'])); + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage("Unsupported asset type for path: 'foo.txt'"); + + Vite::asset('foo.txt'); + } + + public function testAssetsMethodThrowsExceptionForUnknownExtensions() + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage("Unsupported asset type for path: 'foo.txt'"); + + Vite::assets(['foo.txt']); } public function testAssetsMethodReturnsHtmlString() @@ -155,15 +167,6 @@ public function testAssetMethodGeneratesCorrectHtmlForCssFile() $this->assertSame($expected, (string) $html); } - public function testAssetMethodDoesNotIncludeUnknownExtensions() - { - $html = Vite::asset('unknown.ext'); - - $expected = ''; - - $this->assertSame($expected, (string) $html); - } - public static function cssFileExtensionsProvider(): array { return [ From b048fae428615c8329eba5dccd2dd3b557fd7865 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Dec 2024 17:59:14 +0100 Subject: [PATCH 27/30] Use only hotfiles instead of internal environment variable See https://github.com/hydephp/develop/pull/2060 --- .../src/Console/Commands/ServeCommand.php | 4 +++- packages/framework/src/Facades/Vite.php | 8 +------ .../Feature/Commands/ServeCommandTest.php | 12 ++++++++-- .../tests/Unit/Facades/ViteFacadeTest.php | 22 ------------------- .../Unit/ServeCommandOptionsUnitTest.php | 12 ---------- .../realtime-compiler/src/ConsoleOutput.php | 3 ++- 6 files changed, 16 insertions(+), 45 deletions(-) diff --git a/packages/framework/src/Console/Commands/ServeCommand.php b/packages/framework/src/Console/Commands/ServeCommand.php index c998aba44ce..b2b85548d05 100644 --- a/packages/framework/src/Console/Commands/ServeCommand.php +++ b/packages/framework/src/Console/Commands/ServeCommand.php @@ -5,6 +5,7 @@ namespace Hyde\Console\Commands; use Closure; +use Hyde\Facades\Filesystem; use Hyde\Hyde; use Hyde\Facades\Config; use Illuminate\Contracts\Process\InvokedProcess; @@ -112,7 +113,6 @@ protected function getEnvironmentVariables(): array 'HYDE_SERVER_DASHBOARD' => $this->parseEnvironmentOption('dashboard'), 'HYDE_PRETTY_URLS' => $this->parseEnvironmentOption('pretty-urls'), 'HYDE_PLAY_CDN' => $this->parseEnvironmentOption('play-cdn'), - 'HYDE_SERVER_VITE' => $this->option('vite') ? 'enabled' : null, ]); } @@ -204,6 +204,8 @@ protected function runViteProcess(): void ); } + Filesystem::touch('app/storage/framework/cache/vite.hot'); + $this->vite = Process::forever()->start('npm run dev'); } diff --git a/packages/framework/src/Facades/Vite.php b/packages/framework/src/Facades/Vite.php index a3db7dd3608..f69c8f0b9de 100644 --- a/packages/framework/src/Facades/Vite.php +++ b/packages/framework/src/Facades/Vite.php @@ -14,7 +14,7 @@ class Vite { public static function running(): bool { - return static::checkIfViteWasEnabledViaTheServeCommand() || Filesystem::exists('app/storage/framework/cache/vite.hot'); + return Filesystem::exists('app/storage/framework/cache/vite.hot'); } public static function asset(string $path): HtmlString @@ -49,12 +49,6 @@ protected static function formatAssetPath(string $path): string throw new InvalidArgumentException("Unsupported asset type for path: '$path'"); } - protected static function checkIfViteWasEnabledViaTheServeCommand(): bool - { - // TODO: Do we actually need this? Hotfile should be enough. - return env('HYDE_SERVER_VITE') === 'enabled'; - } - protected static function isCssPath(string $path): bool { return preg_match('/\.(css|less|sass|scss|styl|stylus|pcss|postcss)$/', $path) === 1; diff --git a/packages/framework/tests/Feature/Commands/ServeCommandTest.php b/packages/framework/tests/Feature/Commands/ServeCommandTest.php index 1f26744c908..3b34e3f4ce9 100644 --- a/packages/framework/tests/Feature/Commands/ServeCommandTest.php +++ b/packages/framework/tests/Feature/Commands/ServeCommandTest.php @@ -182,6 +182,8 @@ public function testWithFancyOutput() public function testHydeServeCommandWithViteOption() { + $this->cleanUpWhenDone('app/storage/framework/cache/vite.hot'); + $mockViteProcess = mock(InvokedProcess::class); $mockViteProcess->shouldReceive('running') ->once() @@ -202,7 +204,7 @@ public function testHydeServeCommandWithViteOption() Process::shouldReceive('env') ->once() - ->with(['HYDE_SERVER_REQUEST_OUTPUT' => false, 'HYDE_SERVER_VITE' => 'enabled']) + ->with(['HYDE_SERVER_REQUEST_OUTPUT' => false]) ->andReturnSelf(); Process::shouldReceive('start') @@ -224,10 +226,14 @@ public function testHydeServeCommandWithViteOption() ->expectsOutput('server output') ->expectsOutput('vite latest output') ->assertExitCode(0); + + $this->assertFileExists('app/storage/framework/cache/vite.hot'); } public function testHydeServeCommandWithViteOptionButViteNotRunning() { + $this->cleanUpWhenDone('app/storage/framework/cache/vite.hot'); + $mockViteProcess = mock(InvokedProcess::class); $mockViteProcess->shouldReceive('running') ->once() @@ -245,7 +251,7 @@ public function testHydeServeCommandWithViteOptionButViteNotRunning() Process::shouldReceive('env') ->once() - ->with(['HYDE_SERVER_REQUEST_OUTPUT' => false, 'HYDE_SERVER_VITE' => 'enabled']) + ->with(['HYDE_SERVER_REQUEST_OUTPUT' => false]) ->andReturnSelf(); Process::shouldReceive('start') @@ -263,6 +269,8 @@ public function testHydeServeCommandWithViteOptionButViteNotRunning() $this->artisan('serve --no-ansi --vite') ->expectsOutput('Starting the HydeRC server... Use Ctrl+C to stop') ->assertExitCode(0); + + $this->assertFileExists('app/storage/framework/cache/vite.hot'); } public function testHydeServeCommandWithViteOptionThrowsWhenPortIsInUse() diff --git a/packages/framework/tests/Unit/Facades/ViteFacadeTest.php b/packages/framework/tests/Unit/Facades/ViteFacadeTest.php index faa90a4676a..4c804e9bf85 100644 --- a/packages/framework/tests/Unit/Facades/ViteFacadeTest.php +++ b/packages/framework/tests/Unit/Facades/ViteFacadeTest.php @@ -24,28 +24,6 @@ protected function tearDown(): void $this->cleanUpFilesystem(); } - public function testRunningReturnsTrueWhenEnvironmentVariableIsSet() - { - putenv('HYDE_SERVER_VITE=enabled'); - - $this->assertTrue(Vite::running()); - - putenv('HYDE_SERVER_VITE'); - } - - public function testRunningReturnsFalseWhenEnvironmentVariableIsNotSetOrDisabled() - { - $this->assertFalse(Vite::running()); - - putenv('HYDE_SERVER_VITE=disabled'); - - $this->assertFalse(Vite::running()); - - putenv('HYDE_SERVER_VITE'); - - $this->assertFalse(Vite::running()); - } - public function testRunningReturnsTrueWhenViteHotFileExists() { $this->file('app/storage/framework/cache/vite.hot'); diff --git a/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php b/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php index 9df671ec114..eeaa62b5a47 100644 --- a/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php +++ b/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php @@ -335,18 +335,6 @@ public function testGetOpenCommandForUnknownOS() $this->assertNull($this->getMock()->getOpenCommand('UnknownOS')); } - public function testViteOptionPropagatesToEnvironmentVariables() - { - $command = $this->getMock(['vite' => true]); - $this->assertSame('enabled', $command->getEnvironmentVariables()['HYDE_SERVER_VITE']); - - $command = $this->getMock(['vite' => false]); - $this->assertFalse(isset($command->getEnvironmentVariables()['HYDE_SERVER_VITE'])); - - $command = $this->getMock(); - $this->assertFalse(isset($command->getEnvironmentVariables()['HYDE_SERVER_VITE'])); - } - public function testWithViteArgument() { HydeKernel::setInstance(new HydeKernel()); diff --git a/packages/realtime-compiler/src/ConsoleOutput.php b/packages/realtime-compiler/src/ConsoleOutput.php index fdb1cf0a801..0d1d6a611bf 100644 --- a/packages/realtime-compiler/src/ConsoleOutput.php +++ b/packages/realtime-compiler/src/ConsoleOutput.php @@ -5,6 +5,7 @@ namespace Hyde\RealtimeCompiler; use Closure; +use Hyde\Facades\Vite; use Hyde\Hyde; use Illuminate\Support\Str; use Illuminate\Support\Arr; @@ -35,7 +36,7 @@ public function printStartMessage(string $host, int $port, array $environment = sprintf('Listening on: %s', $url, $url), (config('hyde.server.dashboard.enabled') || Arr::has($environment, 'HYDE_SERVER_DASHBOARD')) && Arr::get($environment, 'HYDE_SERVER_DASHBOARD') === 'enabled' ? sprintf('Live dashboard: %s/dashboard', $url, $url) : null, - Arr::get($environment, 'HYDE_SERVER_VITE') === 'enabled' ? + Vite::running() ? sprintf('Vite HMR server: http://%s:5173', $host, $host) : null, '', ]); From 9ffbeed4d63d3b9246e75bd3b44711aa39ee486d Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Dec 2024 18:15:51 +0100 Subject: [PATCH 28/30] Extract constant for CSS extensions --- packages/framework/src/Facades/Vite.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Facades/Vite.php b/packages/framework/src/Facades/Vite.php index f69c8f0b9de..84efa37482e 100644 --- a/packages/framework/src/Facades/Vite.php +++ b/packages/framework/src/Facades/Vite.php @@ -12,6 +12,8 @@ */ class Vite { + protected const CSS_EXTENSIONS = ['css', 'less', 'sass', 'scss', 'styl', 'stylus', 'pcss', 'postcss']; + public static function running(): bool { return Filesystem::exists('app/storage/framework/cache/vite.hot'); @@ -51,7 +53,7 @@ protected static function formatAssetPath(string $path): string protected static function isCssPath(string $path): bool { - return preg_match('/\.(css|less|sass|scss|styl|stylus|pcss|postcss)$/', $path) === 1; + return preg_match('/\.('.implode('|', static::CSS_EXTENSIONS).')$/', $path) === 1; } protected static function isJsPath(string $path): bool From f418e2bd9776a5e8f93bdf972a6fc100e2e50be3 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Dec 2024 18:19:07 +0100 Subject: [PATCH 29/30] Support more JavaScript extensions --- packages/framework/src/Facades/Vite.php | 3 +- .../tests/Unit/Facades/ViteFacadeTest.php | 28 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Facades/Vite.php b/packages/framework/src/Facades/Vite.php index 84efa37482e..f2729b26d27 100644 --- a/packages/framework/src/Facades/Vite.php +++ b/packages/framework/src/Facades/Vite.php @@ -13,6 +13,7 @@ class Vite { protected const CSS_EXTENSIONS = ['css', 'less', 'sass', 'scss', 'styl', 'stylus', 'pcss', 'postcss']; + protected const JS_EXTENSIONS = ['js', 'jsx', 'ts', 'tsx']; public static function running(): bool { @@ -58,7 +59,7 @@ protected static function isCssPath(string $path): bool protected static function isJsPath(string $path): bool { - return str_ends_with($path, '.js'); + return preg_match('/\.('.implode('|', static::JS_EXTENSIONS).')$/', $path) === 1; } protected static function formatStylesheetLink(string $path): string diff --git a/packages/framework/tests/Unit/Facades/ViteFacadeTest.php b/packages/framework/tests/Unit/Facades/ViteFacadeTest.php index 4c804e9bf85..45adda783c4 100644 --- a/packages/framework/tests/Unit/Facades/ViteFacadeTest.php +++ b/packages/framework/tests/Unit/Facades/ViteFacadeTest.php @@ -122,6 +122,23 @@ public function testAssetsMethodSupportsAllCssFileExtensions(string $extension) } } + /** + * @dataProvider jsFileExtensionsProvider + */ + public function testAssetsMethodSupportsAllJsFileExtensions(string $extension) + { + $html = Vite::assets(["resources/js/app.$extension"]); + + if ($extension !== 'css') { + $expected = ''; + + $this->assertStringNotContainsString('stylesheet', (string) $html); + $this->assertSame($expected, (string) $html); + } else { + $this->assertStringContainsString('stylesheet', (string) $html); + } + } + public function testAssetMethodReturnsHtmlString() { $this->assertInstanceOf(HtmlString::class, Vite::asset('foo.js')); @@ -159,4 +176,15 @@ public static function cssFileExtensionsProvider(): array ['js'], ]; } + + public static function jsFileExtensionsProvider(): array + { + return [ + ['js'], + ['jsx'], + ['ts'], + ['tsx'], + ['css'], + ]; + } } From 83264ca794b35e794e65b1cd114fd3ea688267d8 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Dec 2024 18:23:39 +0100 Subject: [PATCH 30/30] Extract helper method for repeated code --- packages/framework/src/Facades/Vite.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Facades/Vite.php b/packages/framework/src/Facades/Vite.php index f2729b26d27..814aff2b07c 100644 --- a/packages/framework/src/Facades/Vite.php +++ b/packages/framework/src/Facades/Vite.php @@ -54,12 +54,17 @@ protected static function formatAssetPath(string $path): string protected static function isCssPath(string $path): bool { - return preg_match('/\.('.implode('|', static::CSS_EXTENSIONS).')$/', $path) === 1; + return static::checkFileExtensionForPath($path, static::CSS_EXTENSIONS); } protected static function isJsPath(string $path): bool { - return preg_match('/\.('.implode('|', static::JS_EXTENSIONS).')$/', $path) === 1; + return static::checkFileExtensionForPath($path, static::JS_EXTENSIONS); + } + + protected static function checkFileExtensionForPath(string $path, array $extensions): bool + { + return preg_match('/\.('.implode('|', $extensions).')$/', $path) === 1; } protected static function formatStylesheetLink(string $path): string