Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 36 additions & 14 deletions src/Illuminate/Foundation/Vite.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ public function __invoke($entrypoints, $buildDirectory = null)
}

$manifest = $this->manifest($buildDirectory);
$chunkKeysByFile = $this->chunkKeysByFile($manifest);

$tags = new Collection;
$preloads = new Collection;
Expand All @@ -419,19 +420,20 @@ public function __invoke($entrypoints, $buildDirectory = null)
]);

foreach ($manifest[$import]['css'] ?? [] as $css) {
$partialManifest = (new Collection($manifest))->where('file', $css);
$cssKey = $chunkKeysByFile[$css] ?? null;
$cssChunk = $cssKey === null ? null : $manifest[$cssKey];

$preloads->push([
$partialManifest->keys()->first(),
$cssKey,
$this->assetPath("{$buildDirectory}/{$css}"),
$partialManifest->first(),
$cssChunk,
$manifest,
]);

$tags->push($this->makeTagForChunk(
$partialManifest->keys()->first(),
$cssKey,
$this->assetPath("{$buildDirectory}/{$css}"),
$partialManifest->first(),
$cssChunk,
$manifest
));
}
Expand All @@ -445,19 +447,20 @@ public function __invoke($entrypoints, $buildDirectory = null)
));

foreach ($chunk['css'] ?? [] as $css) {
$partialManifest = (new Collection($manifest))->where('file', $css);
$cssKey = $chunkKeysByFile[$css] ?? null;
$cssChunk = $cssKey === null ? null : $manifest[$cssKey];

$preloads->push([
$partialManifest->keys()->first(),
$cssKey,
$this->assetPath("{$buildDirectory}/{$css}"),
$partialManifest->first(),
$cssChunk,
$manifest,
]);

$tags->push($this->makeTagForChunk(
$partialManifest->keys()->first(),
$cssKey,
$this->assetPath("{$buildDirectory}/{$css}"),
$partialManifest->first(),
$cssChunk,
$manifest
));
}
Expand All @@ -481,7 +484,7 @@ public function __invoke($entrypoints, $buildDirectory = null)
->flatMap(fn ($entrypoint) => (new Collection($manifest[$entrypoint]['dynamicImports'] ?? []))
->map(fn ($import) => $manifest[$import])
->filter(fn ($chunk) => str_ends_with($chunk['file'], '.js') || str_ends_with($chunk['file'], '.css'))
->flatMap($f = function ($chunk) use (&$f, $manifest, &$discoveredImports) {
->flatMap($f = function ($chunk) use (&$f, $manifest, $chunkKeysByFile, &$discoveredImports) {
return (new Collection([...$chunk['imports'] ?? [], ...$chunk['dynamicImports'] ?? []]))
->reject(function ($import) use (&$discoveredImports) {
if (isset($discoveredImports[$import])) {
Expand All @@ -495,9 +498,9 @@ public function __invoke($entrypoints, $buildDirectory = null)
$f($manifest[$import])
), new Collection([$chunk]))
->merge((new Collection($chunk['css'] ?? []))->map(
fn ($css) => (new Collection($manifest))->first(fn ($chunk) => $chunk['file'] === $css) ?? [
'file' => $css,
],
fn ($css) => isset($chunkKeysByFile[$css])
? $manifest[$chunkKeysByFile[$css]]
: ['file' => $css],
));
})
->map(function ($chunk) use ($buildDirectory, $manifest) {
Expand Down Expand Up @@ -966,6 +969,25 @@ protected function manifest($buildDirectory)
return static::$manifests[$path];
}

/**
* Index the given manifest's chunk keys by their output file.
*
* @param array $manifest
* @return array<string, string>
*/
protected function chunkKeysByFile($manifest)
{
$index = [];

foreach ($manifest as $key => $chunk) {
if (isset($chunk['file']) && ! isset($index[$chunk['file']])) {
$index[$chunk['file']] = $key;
}
}

return $index;
}

/**
* Get the path to the manifest file for the given build directory.
*
Expand Down
95 changes: 95 additions & 0 deletions tests/Foundation/FoundationViteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,101 @@ public function testViteWithNestedCssImport()
$this->cleanViteManifest($buildDir);
}

public function testViteResolvesCssChunksReachedThroughNestedImports()
{
$buildDir = Str::random();
$this->makeViteManifest([
'resources/js/app.js' => [
'src' => 'resources/js/app.js',
'file' => 'assets/app.versioned.js',
'imports' => [
'_layout.js',
],
],
'_layout.js' => [
'file' => 'assets/layout.versioned.js',
'css' => [
'assets/layout.versioned.css',
],
'imports' => [
'_header.js',
],
],
'_header.js' => [
'file' => 'assets/header.versioned.js',
'css' => [
'assets/header.versioned.css',
],
],
// The CSS reached through the nested imports above has its own
// manifest entries, which must be resolved back from the file name.
'resources/css/layout.css' => [
'src' => 'resources/css/layout.css',
'file' => 'assets/layout.versioned.css',
],
'resources/css/header.css' => [
'src' => 'resources/css/header.css',
'file' => 'assets/header.versioned.css',
],
], $buildDir);

$vite = app(Vite::class);
$result = $vite(['resources/js/app.js'], $buildDir);

$this->assertSame(
'<link rel="preload" as="style" href="https://example.com/'.$buildDir.'/assets/layout.versioned.css" />'
.'<link rel="preload" as="style" href="https://example.com/'.$buildDir.'/assets/header.versioned.css" />'
.'<link rel="modulepreload" as="script" href="https://example.com/'.$buildDir.'/assets/app.versioned.js" />'
.'<link rel="modulepreload" as="script" href="https://example.com/'.$buildDir.'/assets/layout.versioned.js" />'
.'<link rel="modulepreload" as="script" href="https://example.com/'.$buildDir.'/assets/header.versioned.js" />'
.'<link rel="stylesheet" href="https://example.com/'.$buildDir.'/assets/layout.versioned.css" />'
.'<link rel="stylesheet" href="https://example.com/'.$buildDir.'/assets/header.versioned.css" />'
.'<script type="module" src="https://example.com/'.$buildDir.'/assets/app.versioned.js"></script>',
$result->toHtml()
);

$this->assertSame([
'https://example.com/'.$buildDir.'/assets/layout.versioned.css',
'https://example.com/'.$buildDir.'/assets/header.versioned.css',
'https://example.com/'.$buildDir.'/assets/app.versioned.js',
'https://example.com/'.$buildDir.'/assets/layout.versioned.js',
'https://example.com/'.$buildDir.'/assets/header.versioned.js',
], array_keys($vite->preloadedAssets()));

$this->cleanViteManifest($buildDir);
}

public function testViteUsesTheFirstManifestEntryWhenTwoChunksShareAnOutputFile()
{
$buildDir = Str::random();
$this->makeViteManifest([
'resources/js/app.js' => [
'src' => 'resources/js/app.js',
'file' => 'assets/app.versioned.js',
'css' => [
'assets/shared.versioned.css',
],
],
'resources/css/first.css' => [
'src' => 'resources/css/first.css',
'file' => 'assets/shared.versioned.css',
'integrity' => 'first-integrity',
],
'resources/css/second.css' => [
'src' => 'resources/css/second.css',
'file' => 'assets/shared.versioned.css',
'integrity' => 'second-integrity',
],
], $buildDir);

$result = app(Vite::class)(['resources/js/app.js'], $buildDir);

$this->assertStringContainsString('integrity="first-integrity"', $result->toHtml());
$this->assertStringNotContainsString('second-integrity', $result->toHtml());

$this->cleanViteManifest($buildDir);
}

public function testViteHotModuleReplacementWithJsOnly()
{
$this->makeViteHotFile();
Expand Down
Loading