Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Output only unique asset / preload tags #45404

Merged
merged 3 commits into from
Dec 23, 2022
Merged
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
5 changes: 3 additions & 2 deletions src/Illuminate/Foundation/Vite.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,10 @@ public function __invoke($entrypoints, $buildDirectory = null)
}
}

[$stylesheets, $scripts] = $tags->partition(fn ($tag) => str_starts_with($tag, '<link'));
[$stylesheets, $scripts] = $tags->unique()->partition(fn ($tag) => str_starts_with($tag, '<link'));

$preloads = $preloads->sortByDesc(fn ($args) => $this->isCssPath($args[1]))
$preloads = $preloads->unique()
->sortByDesc(fn ($args) => $this->isCssPath($args[1]))
->map(fn ($args) => $this->makePreloadTagForChunk(...$args));

return new HtmlString($preloads->join('').$stylesheets->join('').$scripts->join(''));
Expand Down
51 changes: 51 additions & 0 deletions tests/Foundation/FoundationViteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,57 @@ public function testItCanConfigureTheManifestFilename()
rmdir(public_path($buildDir));
}

public function testItOnlyOutputsUniquePreloadTags()
{
$buildDir = Str::random();
$this->makeViteManifest([
'resources/js/app.css' => [
'file' => 'assets/app-versioned.css',
'src' => 'resources/js/app.css',
],
'resources/js/Pages/Welcome.vue' => [
'file' => 'assets/Welcome-versioned.js',
'src' => 'resources/js/Pages/Welcome.vue',
'imports' => [
'resources/js/app.js',
],
],
'resources/js/app.js' => [
'file' => 'assets/app-versioned.js',
'src' => 'resources/js/app.js',
'css' => [
'assets/app-versioned.css',
],
],
], $buildDir);

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

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

$this->assertSame([
"https://example.com/$buildDir/assets/app-versioned.css" => [
'rel="preload"',
'as="style"',
],
"https://example.com/$buildDir/assets/app-versioned.js" => [
'rel="modulepreload"',
],
"https://example.com/$buildDir/assets/Welcome-versioned.js" => [
'rel="modulepreload"',
],
], ViteFacade::preloadedAssets());

$this->cleanViteManifest($buildDir);
}

protected function makeViteManifest($contents = null, $path = 'build')
{
app()->singleton('path.public', fn () => __DIR__);
Expand Down