Skip to content

Instantly share code, notes, and snippets.

@kabouzeid
Last active April 13, 2021 19:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kabouzeid/1a663e8a12b2587e3aa0a5b93c82c160 to your computer and use it in GitHub Desktop.
Save kabouzeid/1a663e8a12b2587e3aa0a5b93c82c160 to your computer and use it in GitHub Desktop.
~/.config/valet/Drivers/LaravelViteValetDriver.php
<?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';
}
}
@kabouzeid
Copy link
Author

What I've added:

file_exists($sitePath.'/vite.config.ts')
// 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;
}

@kabouzeid
Copy link
Author

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