Skip to content

Commit 4c41d9d

Browse files
authored
allow (s)css and image files as entry points (#6)
* allow (s)css files as entry points * allow preloading of css & image entrypoints * test on PHP 8.4
1 parent fb138ba commit 4c41d9d

File tree

5 files changed

+57
-4
lines changed

5 files changed

+57
-4
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ jobs:
1414
- '8.1'
1515
- '8.2'
1616
- '8.3'
17+
- '8.4'
1718
steps:
1819
- uses: shivammathur/setup-php@2.30.4
1920
with:

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"license": "MPL-2.0",
55
"description": "a lightweight PHP-backend integration for the Vite frontend build tool",
66
"scripts": {
7-
"test": "XDEBUG_MODE=coverage php test/test.php"
7+
"test": "XDEBUG_MODE=coverage php test/test.php -v"
88
},
99
"autoload": {
1010
"psr-4": {

src/Manifest.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,17 @@ public function preloadFonts(): void
120120
];
121121
}
122122

123+
/**
124+
* Register MIME types for preloading stylesheets.
125+
*/
126+
public function preloadStyles(): void
127+
{
128+
$this->preload_types = [
129+
...$this->preload_types,
130+
'css' => ['type' => 'text/css', 'as' => 'style'],
131+
];
132+
}
133+
123134
/**
124135
* Create preload, CSS and JS tags for the specified entry point script(s).
125136
*
@@ -208,6 +219,16 @@ private function createPreloadTags(array $chunks): string
208219

209220
// Preload assets:
210221

222+
['extension' => $extension] = pathinfo($chunk->file);
223+
224+
if (isset($this->preload_types[$extension])) {
225+
$preload = $this->preload_types[$extension];
226+
$type = $preload['type'];
227+
$as = $preload['as'];
228+
229+
$tags[] = "<link rel=\"preload\" as=\"{$as}\" type=\"{$type}\" href=\"{$this->base_path}{$chunk->file}\" />";
230+
}
231+
211232
foreach ($chunk->assets as $asset) {
212233
$type = substr($asset, strrpos($asset, '.') + 1);
213234

@@ -235,6 +256,10 @@ private function createStyleTags(array $chunks): string
235256
foreach ($chunk->css as $css) {
236257
$tags[] = "<link rel=\"stylesheet\" href=\"{$this->base_path}{$css}\" />";
237258
}
259+
260+
if (str_ends_with($chunk->file, '.css') && $chunk->isEntry) {
261+
$tags[] = "<link rel=\"stylesheet\" href=\"{$this->base_path}{$chunk->file}\" />";
262+
}
238263
}
239264

240265
return implode("\n", $tags);
@@ -248,7 +273,7 @@ private function createScriptTags(array $chunks): string
248273
$tags = [];
249274

250275
foreach ($chunks as $chunk) {
251-
if ($chunk->isEntry) {
276+
if (str_ends_with($chunk->file, '.js') && $chunk->isEntry) {
252277
$tags[] = "<script type=\"module\" src=\"{$this->base_path}{$chunk->file}\"></script>";
253278
}
254279
}
@@ -267,7 +292,8 @@ private function findImportedChunks(array $entries): array
267292
throw new RuntimeException("Entry not found in manifest: {$entry}");
268293
}
269294

270-
if (! $chunk->isEntry) {
295+
// only check .js and .css files, because images dont get the "isEntry" information in the manifest
296+
if ((str_ends_with($chunk->file, '.js') || str_ends_with($chunk->file, '.css')) && ! $chunk->isEntry) {
271297
throw new RuntimeException("Chunk is not an entry point: {$entry}");
272298
}
273299

test/fixtures/manifest.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,25 @@
3939
"css": [
4040
"assets/shared.a834bfc3.css"
4141
]
42+
},
43+
"public/scss/themes/admin/admin.scss": {
44+
"file": "assets/admin-B8_LVhy3.css",
45+
"src": "public/scss/themes/admin/admin.scss",
46+
"isEntry": true,
47+
"names": [
48+
"admin.css"
49+
]
50+
},
51+
"public/css/plus.css": {
52+
"file": "assets/plus-DwWFnKP0.css",
53+
"src": "public/css/plus.css",
54+
"isEntry": true,
55+
"names": [
56+
"plus.css"
57+
]
58+
},
59+
"public/img/favicon.ico": {
60+
"file": "assets/favicon-zR_S-YMI.ico",
61+
"src": "public/img/favicon.ico"
4262
}
4363
}

test/test.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,9 @@ function () {
9898
);
9999

100100
$vite->preloadImages();
101+
$vite->preloadStyles();
101102

102-
$tags = $vite->createTags("main.js", "consent-banner.js");
103+
$tags = $vite->createTags("main.js", "consent-banner.js", "public/scss/themes/admin/admin.scss", "public/css/plus.css", "public/img/favicon.ico");
103104

104105
eq(
105106
explode("\n", $tags->preload),
@@ -111,6 +112,9 @@ function () {
111112
'<link rel="modulepreload" href="/dist/assets/shared.83069a53.js" />',
112113
// Preload `views/foo.js` entry point script:
113114
'<link rel="modulepreload" href="/dist/assets/consent-banner.0e3b3b7b.js" />',
115+
'<link rel="preload" as="style" type="text/css" href="/dist/assets/admin-B8_LVhy3.css" />',
116+
'<link rel="preload" as="style" type="text/css" href="/dist/assets/plus-DwWFnKP0.css" />',
117+
'<link rel="preload" as="image" type="image/x-icon" href="/dist/assets/favicon-zR_S-YMI.ico" />',
114118
],
115119
);
116120

@@ -123,6 +127,8 @@ function () {
123127
'<link rel="stylesheet" href="/dist/assets/shared.a834bfc3.css" />',
124128
// CSS imported by the consent-banner entry point script:
125129
'<link rel="stylesheet" href="/dist/assets/consent-banner.8ba40300.css" />',
130+
'<link rel="stylesheet" href="/dist/assets/admin-B8_LVhy3.css" />',
131+
'<link rel="stylesheet" href="/dist/assets/plus-DwWFnKP0.css" />',
126132
]
127133
);
128134

0 commit comments

Comments
 (0)