From 1c3c66d85e63f638743abb96f816749a5a13f2a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Thu, 9 Oct 2025 09:46:34 +0200 Subject: [PATCH 1/8] allow (s)css files as entry points --- src/Manifest.php | 6 +++++- test/fixtures/manifest.json | 16 ++++++++++++++++ test/test.php | 4 +++- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/Manifest.php b/src/Manifest.php index 4640fd4..452b33e 100644 --- a/src/Manifest.php +++ b/src/Manifest.php @@ -235,6 +235,10 @@ private function createStyleTags(array $chunks): string foreach ($chunk->css as $css) { $tags[] = "base_path}{$css}\" />"; } + + if (str_ends_with($chunk->file, '.css') && $chunk->isEntry) { + $tags[] = "base_path}{$chunk->file}\" />"; + } } return implode("\n", $tags); @@ -248,7 +252,7 @@ private function createScriptTags(array $chunks): string $tags = []; foreach ($chunks as $chunk) { - if ($chunk->isEntry) { + if (str_ends_with($chunk->file, '.js') && $chunk->isEntry) { $tags[] = ""; } } diff --git a/test/fixtures/manifest.json b/test/fixtures/manifest.json index f1da3df..4d91561 100644 --- a/test/fixtures/manifest.json +++ b/test/fixtures/manifest.json @@ -39,5 +39,21 @@ "css": [ "assets/shared.a834bfc3.css" ] + }, + "public/scss/themes/admin/admin.scss": { + "file": "assets/admin-B8_LVhy3.css", + "src": "public/scss/themes/admin/admin.scss", + "isEntry": true, + "names": [ + "admin.css" + ] + }, + "public/css/plus.css": { + "file": "assets/plus-DwWFnKP0.css", + "src": "public/css/plus.css", + "isEntry": true, + "names": [ + "plus.css" + ] } } diff --git a/test/test.php b/test/test.php index 18140a8..d4b1028 100644 --- a/test/test.php +++ b/test/test.php @@ -99,7 +99,7 @@ function () { $vite->preloadImages(); - $tags = $vite->createTags("main.js", "consent-banner.js"); + $tags = $vite->createTags("main.js", "consent-banner.js", "public/scss/themes/admin/admin.scss", "public/css/plus.css"); eq( explode("\n", $tags->preload), @@ -123,6 +123,8 @@ function () { '', // CSS imported by the consent-banner entry point script: '', + '', + '', ] ); From 5af344d0655a22a1a96e12783f609f93cf2c0b33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Thu, 9 Oct 2025 10:08:36 +0200 Subject: [PATCH 2/8] allow preloading of css files --- src/Manifest.php | 19 +++++++++++++++++++ test/test.php | 4 ++++ 2 files changed, 23 insertions(+) diff --git a/src/Manifest.php b/src/Manifest.php index 452b33e..7d167b7 100644 --- a/src/Manifest.php +++ b/src/Manifest.php @@ -120,6 +120,17 @@ public function preloadFonts(): void ]; } + /** + * Register MIME types for preloading common web font formats. + */ + public function preloadStyles(): void + { + $this->preload_types = [ + ...$this->preload_types, + 'css' => ['type' => 'text/css', 'as' => 'style'], + ]; + } + /** * Create preload, CSS and JS tags for the specified entry point script(s). * @@ -206,6 +217,14 @@ private function createPreloadTags(array $chunks): string $tags[] = "base_path}{$chunk->file}\" />"; } + if (str_ends_with($chunk->file, '.css') && isset($this->preload_types['css'])) { + $preload = $this->preload_types['css']; + $type = $preload['type']; + $as = $preload['as']; + + $tags[] = "base_path}{$chunk->file}\" />"; + } + // Preload assets: foreach ($chunk->assets as $asset) { diff --git a/test/test.php b/test/test.php index d4b1028..04e454a 100644 --- a/test/test.php +++ b/test/test.php @@ -98,6 +98,8 @@ function () { ); $vite->preloadImages(); + $vite->preloadFonts(); + $vite->preloadStyles(); $tags = $vite->createTags("main.js", "consent-banner.js", "public/scss/themes/admin/admin.scss", "public/css/plus.css"); @@ -111,6 +113,8 @@ function () { '', // Preload `views/foo.js` entry point script: '', + '', + '', ], ); From 5f0c3104bf8627e30b312994bc24f18946baac4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Thu, 9 Oct 2025 10:24:41 +0200 Subject: [PATCH 3/8] allow preloading of image entries --- src/Manifest.php | 14 ++++++-------- test/fixtures/manifest.json | 4 ++++ test/test.php | 3 ++- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/Manifest.php b/src/Manifest.php index 7d167b7..e2ff4b0 100644 --- a/src/Manifest.php +++ b/src/Manifest.php @@ -217,16 +217,18 @@ private function createPreloadTags(array $chunks): string $tags[] = "base_path}{$chunk->file}\" />"; } - if (str_ends_with($chunk->file, '.css') && isset($this->preload_types['css'])) { - $preload = $this->preload_types['css']; + // Preload assets: + + ['extension' => $extension] = pathinfo($chunk->file); + + if (isset($this->preload_types[$extension])) { + $preload = $this->preload_types[$extension]; $type = $preload['type']; $as = $preload['as']; $tags[] = "base_path}{$chunk->file}\" />"; } - // Preload assets: - foreach ($chunk->assets as $asset) { $type = substr($asset, strrpos($asset, '.') + 1); @@ -290,10 +292,6 @@ private function findImportedChunks(array $entries): array throw new RuntimeException("Entry not found in manifest: {$entry}"); } - if (! $chunk->isEntry) { - throw new RuntimeException("Chunk is not an entry point: {$entry}"); - } - $chunks[$entry] = $chunk; // Recursively find all statically imported chunks: diff --git a/test/fixtures/manifest.json b/test/fixtures/manifest.json index 4d91561..0e0134e 100644 --- a/test/fixtures/manifest.json +++ b/test/fixtures/manifest.json @@ -55,5 +55,9 @@ "names": [ "plus.css" ] + }, + "public/img/favicon.ico": { + "file": "assets/favicon-zR_S-YMI.ico", + "src": "public/img/favicon.ico" } } diff --git a/test/test.php b/test/test.php index 04e454a..b746590 100644 --- a/test/test.php +++ b/test/test.php @@ -101,7 +101,7 @@ function () { $vite->preloadFonts(); $vite->preloadStyles(); - $tags = $vite->createTags("main.js", "consent-banner.js", "public/scss/themes/admin/admin.scss", "public/css/plus.css"); + $tags = $vite->createTags("main.js", "consent-banner.js", "public/scss/themes/admin/admin.scss", "public/css/plus.css", "public/img/favicon.ico"); eq( explode("\n", $tags->preload), @@ -115,6 +115,7 @@ function () { '', '', '', + '', ], ); From 4dcf993935d5e3e9c0753e124319a8146646f752 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Sat, 18 Oct 2025 17:46:12 +0200 Subject: [PATCH 4/8] update check for entry points --- src/Manifest.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Manifest.php b/src/Manifest.php index e2ff4b0..7275622 100644 --- a/src/Manifest.php +++ b/src/Manifest.php @@ -292,6 +292,10 @@ private function findImportedChunks(array $entries): array throw new RuntimeException("Entry not found in manifest: {$entry}"); } + if ((str_ends_with($chunk->file, '.js') || str_ends_with($chunk->file, '.css')) && ! $chunk->isEntry) { + throw new RuntimeException("Chunk is not an entry point: {$entry}"); + } + $chunks[$entry] = $chunk; // Recursively find all statically imported chunks: From 0f7f1cbaa43cf99387b09550f89aa1417647925b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Sun, 19 Oct 2025 13:32:42 +0200 Subject: [PATCH 5/8] add comment and fix docblock --- src/Manifest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Manifest.php b/src/Manifest.php index 7275622..3a8e816 100644 --- a/src/Manifest.php +++ b/src/Manifest.php @@ -121,7 +121,7 @@ public function preloadFonts(): void } /** - * Register MIME types for preloading common web font formats. + * Register MIME types for preloading stylesheets. */ public function preloadStyles(): void { @@ -292,6 +292,7 @@ private function findImportedChunks(array $entries): array throw new RuntimeException("Entry not found in manifest: {$entry}"); } + // only check .js and .css files, because images dont get the "isEntry" information in the manifest if ((str_ends_with($chunk->file, '.js') || str_ends_with($chunk->file, '.css')) && ! $chunk->isEntry) { throw new RuntimeException("Chunk is not an entry point: {$entry}"); } From e9b2219b0df6e314d3cdb1dab305345a3ace0a48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Sun, 19 Oct 2025 13:32:58 +0200 Subject: [PATCH 6/8] remove not needed call in test --- test/test.php | 1 - 1 file changed, 1 deletion(-) diff --git a/test/test.php b/test/test.php index b746590..0437681 100644 --- a/test/test.php +++ b/test/test.php @@ -98,7 +98,6 @@ function () { ); $vite->preloadImages(); - $vite->preloadFonts(); $vite->preloadStyles(); $tags = $vite->createTags("main.js", "consent-banner.js", "public/scss/themes/admin/admin.scss", "public/css/plus.css", "public/img/favicon.ico"); From ec4870c59b4222d9eb9b180695b9262c09f8bb60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Sun, 19 Oct 2025 13:33:34 +0200 Subject: [PATCH 7/8] also test on PHP 8.4 --- .github/workflows/ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 74f7839..6e04fef 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -14,6 +14,7 @@ jobs: - '8.1' - '8.2' - '8.3' + - '8.4' steps: - uses: shivammathur/setup-php@2.30.4 with: From 15b53f1f82c662134d4fd64421d4d37d5097b289 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Sun, 19 Oct 2025 13:57:26 +0200 Subject: [PATCH 8/8] add verbose flag to get detailed coverage report --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index c2a5746..a9b5cbe 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,7 @@ "license": "MPL-2.0", "description": "a lightweight PHP-backend integration for the Vite frontend build tool", "scripts": { - "test": "XDEBUG_MODE=coverage php test/test.php" + "test": "XDEBUG_MODE=coverage php test/test.php -v" }, "autoload": { "psr-4": {