-
Notifications
You must be signed in to change notification settings - Fork 11.4k
[9.x] Vite #42785
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
Merged
Merged
[9.x] Vite #42785
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
4bfcdd7
Add Vite helpers
jessarcher af9058a
Remove vite helper
jessarcher c706959
Add @viteReactRefresh directive
jessarcher 1df8145
Add docblock
jessarcher 8c629c5
Remove unused import
jessarcher f4c9955
Automatically clean up after tests
jessarcher 8e04ae5
Use ASSET_URL when resolving assets
jessarcher 4d3b30f
support default entrypoint
timacdonald 1769f72
update docblock
timacdonald f744692
Linting
jessarcher d5ffd6c
Support CSS entry points
jessarcher 013ec27
Remove default entry points
jessarcher 290d1c6
Fix test namespace
jessarcher a254664
Add missing import
jessarcher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
<?php | ||
|
||
namespace Illuminate\Foundation; | ||
|
||
use Exception; | ||
use Illuminate\Support\HtmlString; | ||
use Illuminate\Support\Str; | ||
|
||
class Vite | ||
{ | ||
/** | ||
* Generate Vite tags for an entrypoint. | ||
* | ||
* @param string|string[] $entrypoints | ||
* @param string $buildDirectory | ||
* @return \Illuminate\Support\HtmlString | ||
* | ||
* @throws \Exception | ||
*/ | ||
public function __invoke($entrypoints, $buildDirectory = 'build') | ||
{ | ||
static $manifests = []; | ||
|
||
$entrypoints = collect($entrypoints); | ||
$buildDirectory = Str::start($buildDirectory, '/'); | ||
|
||
if (is_file(public_path('/hot'))) { | ||
$url = rtrim(file_get_contents(public_path('/hot'))); | ||
|
||
return new HtmlString( | ||
$entrypoints | ||
->map(fn ($entrypoint) => $this->makeTag("{$url}/{$entrypoint}")) | ||
->prepend($this->makeScriptTag("{$url}/@vite/client")) | ||
->join('') | ||
); | ||
} | ||
|
||
$manifestPath = public_path($buildDirectory.'/manifest.json'); | ||
|
||
if (! isset($manifests[$manifestPath])) { | ||
if (! is_file($manifestPath)) { | ||
throw new Exception("Vite manifest not found at: {$manifestPath}"); | ||
} | ||
|
||
$manifests[$manifestPath] = json_decode(file_get_contents($manifestPath), true); | ||
} | ||
|
||
$manifest = $manifests[$manifestPath]; | ||
|
||
$tags = collect(); | ||
|
||
foreach ($entrypoints as $entrypoint) { | ||
if (! isset($manifest[$entrypoint])) { | ||
throw new Exception("Unable to locate file in Vite manifest: {$entrypoint}."); | ||
} | ||
|
||
$tags->push($this->makeTag(asset("{$buildDirectory}/{$manifest[$entrypoint]['file']}"))); | ||
|
||
if (isset($manifest[$entrypoint]['css'])) { | ||
foreach ($manifest[$entrypoint]['css'] as $css) { | ||
$tags->push($this->makeStylesheetTag(asset("{$buildDirectory}/{$css}"))); | ||
} | ||
} | ||
|
||
if (isset($manifest[$entrypoint]['imports'])) { | ||
foreach ($manifest[$entrypoint]['imports'] as $import) { | ||
if (isset($manifest[$import]['css'])) { | ||
foreach ($manifest[$import]['css'] as $css) { | ||
$tags->push($this->makeStylesheetTag(asset("{$buildDirectory}/{$css}"))); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
[$stylesheets, $scripts] = $tags->partition(fn ($tag) => str_starts_with($tag, '<link')); | ||
|
||
return new HtmlString($stylesheets->join('').$scripts->join('')); | ||
} | ||
|
||
/** | ||
* Generate React refresh runtime script. | ||
* | ||
* @return \Illuminate\Support\HtmlString|void | ||
*/ | ||
public function reactRefresh() | ||
{ | ||
if (! is_file(public_path('/hot'))) { | ||
return; | ||
} | ||
|
||
$url = rtrim(file_get_contents(public_path('/hot'))); | ||
|
||
return new HtmlString( | ||
sprintf( | ||
<<<'HTML' | ||
<script type="module"> | ||
import RefreshRuntime from '%s/@react-refresh' | ||
RefreshRuntime.injectIntoGlobalHook(window) | ||
window.$RefreshReg$ = () => {} | ||
window.$RefreshSig$ = () => (type) => type | ||
window.__vite_plugin_react_preamble_installed__ = true | ||
</script> | ||
HTML, | ||
$url | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Generate an appropriate tag for the given URL. | ||
* | ||
* @param string $url | ||
* @return string | ||
*/ | ||
protected function makeTag($url) | ||
{ | ||
if ($this->isCssPath($url)) { | ||
return $this->makeStylesheetTag($url); | ||
} | ||
|
||
return $this->makeScriptTag($url); | ||
} | ||
|
||
/** | ||
* Generate a script tag for the given URL. | ||
* | ||
* @param string $url | ||
* @return string | ||
*/ | ||
protected function makeScriptTag($url) | ||
{ | ||
return sprintf('<script type="module" src="%s"></script>', $url); | ||
} | ||
|
||
/** | ||
* Generate a stylesheet tag for the given URL. | ||
* | ||
* @param string $url | ||
* @return string | ||
*/ | ||
protected function makeStylesheetTag($url) | ||
{ | ||
return sprintf('<link rel="stylesheet" href="%s" />', $url); | ||
} | ||
|
||
/** | ||
* Determine whether the given path is a CSS file. | ||
* | ||
* @param string $path | ||
* @return bool | ||
*/ | ||
protected function isCssPath($path) | ||
{ | ||
return preg_match('/\.(css|less|sass|scss|styl|stylus|pcss|postcss)$/', $path) === 1; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.