Skip to content

Commit cff4dbf

Browse files
committed
throw exceptions for non-entry scripts; add test coverage for expected exceptions; add docs
1 parent 9a03ecc commit cff4dbf

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
lines changed

src/Manifest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ public function preloadFonts(): void
121121
}
122122

123123
/**
124-
* Create preload, CSS and JS tags for the specified entries.
124+
* Create preload, CSS and JS tags for the specified entry point script(s).
125+
*
126+
* Entry points are defined in Vite's `build.rollupOptions` using RollUp's `input` setting.
125127
*
126128
* The expected typical usage in an HTML template is as follows:
127129
*
@@ -139,6 +141,9 @@ public function preloadFonts(): void
139141
* </body>
140142
* </html>
141143
* ```
144+
*
145+
* @link https://vitejs.dev/config/build-options#build-rollupoptions
146+
* @link https://rollupjs.org/configuration-options/#input
142147
*/
143148
public function createTags(string ...$entries): Tags
144149
{
@@ -262,7 +267,7 @@ private function findImportedChunks(array $entries): array
262267
throw new RuntimeException("Entry not found in manifest: {$entry}");
263268
}
264269

265-
if (! $chunk->isEntry && ! $chunk->isDynamicEntry) {
270+
if (! $chunk->isEntry) {
266271
throw new RuntimeException("Chunk is not an entry point: {$entry}");
267272
}
268273

test/fixtures/manifest.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@
1717
"_shared.83069a53.js"
1818
]
1919
},
20+
"consent-banner.js": {
21+
"file": "assets/consent-banner.0e3b3b7b.js",
22+
"src": "consent-banner.js",
23+
"name": "consent-banner",
24+
"isEntry": true,
25+
"css": [
26+
"assets/consent-banner.8ba40300.css"
27+
]
28+
},
2029
"views/foo.js": {
2130
"file": "assets/foo.869aea0d.js",
2231
"src": "views/foo.js",

test/test.php

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use mindplay\vite\Manifest;
44

5-
use function mindplay\testies\{ configure, eq, run, test };
5+
use function mindplay\testies\{ configure, eq, expect, run, test };
66

77
require __DIR__ . '/../vendor/autoload.php';
88

@@ -99,7 +99,7 @@ function () {
9999

100100
$vite->preloadImages();
101101

102-
$tags = $vite->createTags("main.js", "views/foo.js");
102+
$tags = $vite->createTags("main.js", "consent-banner.js");
103103

104104
eq(
105105
explode("\n", $tags->preload),
@@ -110,7 +110,7 @@ function () {
110110
// Preload module shared by both entry points, no duplicates:
111111
'<link rel="modulepreload" href="/dist/assets/shared.83069a53.js" />',
112112
// Preload `views/foo.js` entry point script:
113-
'<link rel="modulepreload" href="/dist/assets/foo.869aea0d.js" />',
113+
'<link rel="modulepreload" href="/dist/assets/consent-banner.0e3b3b7b.js" />',
114114
],
115115
);
116116

@@ -121,6 +121,8 @@ function () {
121121
'<link rel="stylesheet" href="/dist/assets/main.b82dbe22.css" />',
122122
// CSS shared by both entry points, no duplicates:
123123
'<link rel="stylesheet" href="/dist/assets/shared.a834bfc3.css" />',
124+
// CSS imported by the consent-banner entry point script:
125+
'<link rel="stylesheet" href="/dist/assets/consent-banner.8ba40300.css" />',
124126
]
125127
);
126128

@@ -129,11 +131,44 @@ function () {
129131
[
130132
// Main entry point script:
131133
'<script type="module" src="/dist/assets/main.4889e940.js"></script>',
134+
// Consent-banner entry point script:
135+
'<script type="module" src="/dist/assets/consent-banner.0e3b3b7b.js"></script>',
132136
]
133137
);
134138
}
135139
);
136140

141+
test(
142+
"should throw an exception when an entry is not found, or has the wrong type",
143+
function () {
144+
$vite = new Manifest(
145+
dev: false,
146+
manifest_path: __DIR__.'/fixtures/manifest.json',
147+
base_path: '/dist/'
148+
);
149+
150+
$vite->preloadImages();
151+
152+
expect(
153+
RuntimeException::class,
154+
"`does-not-exist.js` does... not exist :-)",
155+
function () use ($vite) {
156+
$tags = $vite->createTags("main.js", "does-not-exist.js");
157+
},
158+
"/Entry not found in manifest\\: does-not-exist\\.js/"
159+
);
160+
161+
expect(
162+
RuntimeException::class,
163+
"`does-not-exist.js` does... not exist :-)",
164+
function () use ($vite) {
165+
$tags = $vite->createTags("views/foo.js");
166+
},
167+
"/Chunk is not an entry point\\: views\\/foo\\.js/"
168+
);
169+
}
170+
);
171+
137172
test(
138173
"can create asset URLs",
139174
function () {

0 commit comments

Comments
 (0)