Skip to content

Commit 5e27d7d

Browse files
Update Vite.php
1 parent 2aec5dd commit 5e27d7d

File tree

1 file changed

+78
-61
lines changed

1 file changed

+78
-61
lines changed

Vite.php

Lines changed: 78 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public function __invoke(array|string $entries, string $buildDirectory = null):
101101
}
102102

103103
if ($optional || $query !== $value) {
104-
if (file_exists($this->app->root('base') . '/' . $query)) {
104+
if ($this->exists($query)) {
105105
$entries[$key] = $query;
106106
$exists[] = $query;
107107
} else {
@@ -149,67 +149,9 @@ public function __invoke(array|string $entries, string $buildDirectory = null):
149149
$assets[$file] = $this->makeTag(...$args);
150150
}
151151

152-
foreach ($chunk['imports'] ?? [] as $key) {
153-
$chunk = $this->chunk($manifest, $key);
154-
$file = $chunk['file'];
155-
156-
if (! isset($preloads[$file])) {
157-
$preloads[$file] = $this->makePreloadTag(
158-
$key,
159-
url($buildDirectory . '/'. $file),
160-
$chunk,
161-
$manifest,
162-
);
163-
}
164-
165-
foreach ($chunk['css'] ?? [] as $key) {
166-
$chunks = array_filter($manifest, fn ($value) =>
167-
$value['file'] === $key
168-
);
169-
170-
$chunk = $chunks[$key = array_key_first($chunks)];
171-
$file = $chunk['file'];
172-
173-
$args = [
174-
$key,
175-
url($buildDirectory . '/'. $file),
176-
$chunk,
177-
$manifest,
178-
];
179-
180-
if (! isset($preloads[$file])) {
181-
$preloads[$file] = $this->makePreloadTag(...$args);
182-
}
183-
184-
if (! isset($assets[$file])) {
185-
$assets[$file] = $this->makeTag(...$args);
186-
}
187-
}
188-
}
152+
$this->resolveImports($chunk, $buildDirectory, $manifest, $assets, $preloads);
189153

190-
foreach ($chunk['css'] ?? [] as $key) {
191-
$chunks = array_filter($manifest, fn ($value) =>
192-
$value['file'] === $key
193-
);
194-
195-
$chunk = $chunks[$key = array_key_first($chunks)];
196-
$file = $chunk['file'];
197-
198-
$args = [
199-
$key,
200-
url($buildDirectory . '/'. $file),
201-
$chunk,
202-
$manifest,
203-
];
204-
205-
if (! isset($preloads[$file])) {
206-
$preloads[$file] = $this->makePreloadTag(...$args);
207-
}
208-
209-
if (! isset($assets[$file])) {
210-
$assets[$file] = $this->makeTag(...$args);
211-
}
212-
}
154+
$this->resolveCss($chunk, $buildDirectory, $manifest, $assets, $preloads);
213155
}
214156

215157
uksort($preloads, fn ($a, $b) =>
@@ -253,6 +195,28 @@ protected function chunk(array $manifest, string $file): array
253195
return $manifest[$file];
254196
}
255197

198+
/**
199+
* Check if a source file exists.
200+
*/
201+
protected function exists(string $file): bool
202+
{
203+
$base = $this->app->root('vite:base') ?? $this->app->root('base');
204+
$index = $this->app->root('index');
205+
206+
$paths = [
207+
$base ?? $index,
208+
dirname($index),
209+
];
210+
211+
foreach ($paths as $path) {
212+
if (file_exists(rtrim($path, '/') . '/' . $file)) {
213+
return true;
214+
}
215+
}
216+
217+
return false;
218+
}
219+
256220
/**
257221
* Get the the manifest file for the given build directory.
258222
*
@@ -486,6 +450,59 @@ public function nonce(): ?string
486450
return $this->nonce;
487451
}
488452

453+
/**
454+
* Resolve related css files.
455+
*/
456+
public function resolveCss(array $chunk, string $buildDirectory, array $manifest, array &$assets, array &$preloads): void
457+
{
458+
foreach ($chunk['css'] ?? [] as $key) {
459+
$chunks = array_filter($manifest, fn ($value) =>
460+
$value['file'] === $key
461+
);
462+
463+
$key = array_key_first($chunks);
464+
$chunk = current($chunks);
465+
$file = $chunk['file'];
466+
467+
$args = [
468+
$key,
469+
url($buildDirectory . '/'. $file),
470+
$chunk,
471+
$manifest,
472+
];
473+
474+
if (! isset($assets[$file])) {
475+
$assets[$file] = $this->makeTag(...$args);
476+
}
477+
478+
if (! isset($preloads[$file])) {
479+
$preloads[$file] = $this->makePreloadTag(...$args);
480+
}
481+
}
482+
}
483+
484+
/**
485+
* Resolve related imports.
486+
*/
487+
public function resolveImports(array $chunk, string $buildDirectory, array $manifest, array &$assets, array &$preloads): void
488+
{
489+
foreach ($chunk['imports'] ?? [] as $key) {
490+
$chunk = $this->chunk($manifest, $key);
491+
$file = $chunk['file'];
492+
493+
if (! isset($preloads[$file])) {
494+
$preloads[$file] = $this->makePreloadTag(
495+
$key,
496+
url($buildDirectory . '/'. $file),
497+
$chunk,
498+
$manifest,
499+
);
500+
}
501+
502+
$this->resolveCss($chunk, $buildDirectory, $manifest, $assets, $preloads);
503+
}
504+
}
505+
489506
/**
490507
* Generate or set a Content Security Policy nonce to apply to all generated tags.
491508
*/

0 commit comments

Comments
 (0)