Last active
April 13, 2021 19:43
-
-
Save kabouzeid/1a663e8a12b2587e3aa0a5b93c82c160 to your computer and use it in GitHub Desktop.
~/.config/valet/Drivers/LaravelViteValetDriver.php
This file contains 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
<?php | |
// Code copied from `https://github.com/laravel/valet/blob/d312a588f352c4b5c62b75c4abd1897403b04b76/cli/drivers/LaravelValetDriver.php` (13. April 2021) | |
class LaravelViteValetDriver extends ValetDriver | |
{ | |
/** | |
* Determine if the driver serves the request. | |
* | |
* @param string $sitePath | |
* @param string $siteName | |
* @param string $uri | |
* @return bool | |
*/ | |
public function serves($sitePath, $siteName, $uri) | |
{ | |
return file_exists($sitePath.'/public/index.php') && | |
file_exists($sitePath.'/artisan') && | |
file_exists($sitePath.'/vite.config.ts'); | |
} | |
/** | |
* Determine if the incoming request is for a static file. | |
* | |
* @param string $sitePath | |
* @param string $siteName | |
* @param string $uri | |
* @return string|false | |
*/ | |
public function isStaticFile($sitePath, $siteName, $uri) | |
{ | |
if (file_exists($staticFilePath = $sitePath.'/public'.$uri) | |
&& is_file($staticFilePath)) { | |
return $staticFilePath; | |
} | |
$storageUri = $uri; | |
if (strpos($uri, '/storage/') === 0) { | |
$storageUri = substr($uri, 8); | |
} | |
if ($this->isActualFile($storagePath = $sitePath.'/storage/app/public'.$storageUri)) { | |
return $storagePath; | |
} | |
// for some resources the base_url doesn't point to vite (http://localhost:3000), thus we handle it here | |
if (strpos($uri, '/node_modules/') === 0 && $this->isActualFile($resourcePath = $sitePath.$uri)) { | |
return $resourcePath; | |
} | |
return false; | |
} | |
/** | |
* Get the fully resolved path to the application's front controller. | |
* | |
* @param string $sitePath | |
* @param string $siteName | |
* @param string $uri | |
* @return string | |
*/ | |
public function frontControllerPath($sitePath, $siteName, $uri) | |
{ | |
// Shortcut for getting the "local" hostname as the HTTP_HOST | |
if (isset($_SERVER['HTTP_X_ORIGINAL_HOST'], $_SERVER['HTTP_X_FORWARDED_HOST'])) { | |
$_SERVER['HTTP_HOST'] = $_SERVER['HTTP_X_FORWARDED_HOST']; | |
} | |
return $sitePath.'/public/index.php'; | |
} | |
} |
Place the file in ~/.config/valet/Drivers/
. Valet will now also serve http://myapp/node_modules/some/file
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What I've added: