-
-
Notifications
You must be signed in to change notification settings - Fork 6
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
[2.x] Add Vite Realtime Compiler integration for HMR #2016
Conversation
The socket check takes a few milliseconds on Mac which is totally okay, but about 500ms on Windows which is not acceptable for each page visit.
e656c6e
to
761d92a
Compare
Resources for live reload later on https://github.com/andrefelipe/vite-php-setup/blob/master/vite/vite.config.js |
We have this latter part to automatically connect to a Vite server directly run through // Check if Vite dev server is running by attempting to connect to it
// Todo: Improve performance on Windows (takes less than 1ms on macOS, but around 100ms on Windows)
set_error_handler(fn () => false); // Todo: This warning surpressor does not work on Windows
$server = fsockopen('localhost', 5173, $errno, $errstr, 0.1);
restore_error_handler();
if ($server) {
fclose($server);
return true;
}
return false; However, this introduces an unacceptable real-time compiler performance penalty for Windows users, where each page load now takes 100ms longer as this check is performed every time a page is served. Here are my ideas initial ideas to adress this.
|
Claude AnalysisLet me analyze the options and propose a solution: Analysis of Options
Recommended Solution: Hot File ApproachThis is the best option because:
Development Plan
namespace Hyde\Framework\Services;
class ViteManager
{
protected static function getHotFile(): string
{
return Hyde::path('storage/framework/cache/vite.hot');
}
public static function createHotFile(): void
{
file_put_contents(static::getHotFile(), time());
}
public static function removeHotFile(): void
{
@unlink(static::getHotFile());
}
public static function isRunning(): bool
{
return file_exists(static::getHotFile());
}
}
public static function running(): bool
{
// Check if Vite was enabled via the serve command
if (env('HYDE_SERVER_VITE') === 'enabled') {
return true;
}
return ViteManager::isRunning();
}
protected ConsoleOutput $console;
protected InvokedProcess $server;
protected InvokedProcess $vite;
public function safeHandle(): int
{
$this->configureOutput();
$this->printStartMessage();
if ($this->option('open') !== 'false') {
$this->openInBrowser((string) $this->option('open'));
}
$command = sprintf('php -S %s:%d %s',
$this->getHostSelection(),
$this->getPortSelection(),
$this->getExecutablePath()
);
if ($this->option('vite')) {
$this->runViteProcess();
}
$this->runServerProcess($command);
while ($this->server->running()) {
if (isset($this->vite) && $this->vite->running()) {
$output = $this->vite->latestOutput();
if ($output) {
$this->output->write($output);
}
}
Sleep::usleep(100000); // 100ms
}
return Command::SUCCESS;
} Add hot file management: protected function runViteProcess(): void
{
ViteManager::createHotFile();
// Existing Vite process code...
register_shutdown_function(function () {
ViteManager::removeHotFile();
});
}
while ($this->server->running()) {
if (isset($this->vite) && $this->vite->running()) {
ViteManager::createHotFile();
} else {
ViteManager::removeHotFile();
}
// Existing output handling...
} Implementation Benefits
|
07034e4
to
3687cea
Compare
plugins: [ | ||
{ | ||
name: 'hyde-vite-server', | ||
configureServer(server) { | ||
server.middlewares.use((req, res, next) => { | ||
if (req.url === '/') { | ||
res.end(fs.readFileSync( | ||
path.resolve(__dirname, 'vendor/hyde/realtime-compiler/resources/vite-index-page.html'), | ||
'utf-8' | ||
)); | ||
} else { | ||
next(); | ||
} | ||
}); | ||
}, | ||
}, | ||
], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Eventually we may want to extract a NPM plugin for this
@else | ||
@if(config('hyde.load_app_styles_from_cdn', false)) | ||
<link rel="stylesheet" href="{{ HydeFront::cdnLink('app.css') }}"> | ||
@elseif(Asset::exists('app.css')) | ||
<link rel="stylesheet" href="{{ Asset::get('app.css') }}"> | ||
@endif | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make more sense if we just automatically enabled Vite when using Asset::get
and Vite is running?
@@ -1,5 +1,7 @@ | |||
{{-- The compiled Vite scripts --}} | |||
@if(Asset::exists('app.js')) | |||
@if(Vite::running()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
May make more sense to have this alongside the CSS include since the script is deferred anyways, but not sure how to handle that best with the partials since it would fit better in the head
component, and don't want to make such a big change here.
Add Vite Realtime Compiler integration for HMR
Description
This PR integrates the Vite with the Realtime Compiler for Hot Module Replacement for a highly efficient development workflow by enabling real-time updates without needing a full page reload.
Key Changes
serve
command now has a--vite
option to enable Vite for Hot Module Replacement (HMR). When enabled, the command starts a Vite development server along with the HydeRC server.serve
command now runs the server and Vite processes asynchronously, allowing for parallel execution and more responsive output.serve
command also checks if the Vite server port is available before starting the Vite development server, preventing conflicts and providing useful error messages.This significantly improves the developer experience by allowing for fast, iterative development with instant feedback via HMR.
Future Tasks