Skip to content

Commit 9e1c2df

Browse files
authored
Merge pull request #2021 from hydephp/vite-integration
Massive Windows performance improvement of the Vite integration
2 parents ec1583a + 18ccd6b commit 9e1c2df

File tree

3 files changed

+44
-36
lines changed

3 files changed

+44
-36
lines changed

packages/framework/src/Facades/Vite.php

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,8 @@ public static function running(): bool
1818
return true;
1919
}
2020

21-
// Check if Vite dev server is running by attempting to connect to it
22-
// Todo: Improve performance on Windows (takes less than 1ms on macOS, but around 100ms on Windows)
23-
set_error_handler(fn () => false); // Todo: This warning surpressor does not work on Windows
24-
$server = fsockopen('localhost', 5173, $errno, $errstr, 0.1);
25-
restore_error_handler();
26-
27-
if ($server) {
28-
fclose($server);
29-
30-
return true;
31-
}
32-
33-
return false;
21+
// Check for Vite hot file
22+
return Filesystem::exists('app/storage/framework/cache/vite.hot');
3423
}
3524

3625
public static function assets(array $paths): HtmlString

packages/framework/tests/Unit/Facades/ViteFacadeTest.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,22 @@
44

55
use Hyde\Testing\UnitTestCase;
66
use Hyde\Facades\Vite;
7+
use Hyde\Testing\CreatesTemporaryFiles;
78

89
/**
910
* @covers \Hyde\Facades\Vite
1011
*/
1112
class ViteFacadeTest extends UnitTestCase
1213
{
14+
use CreatesTemporaryFiles;
15+
16+
protected static bool $needsKernel = true;
17+
18+
protected function tearDown(): void
19+
{
20+
$this->cleanUpFilesystem();
21+
}
22+
1323
public function testRunningReturnsTrueWhenEnvironmentVariableIsSet()
1424
{
1525
putenv('HYDE_SERVER_VITE=enabled');
@@ -19,17 +29,14 @@ public function testRunningReturnsTrueWhenEnvironmentVariableIsSet()
1929
putenv('HYDE_SERVER_VITE');
2030
}
2131

22-
public function testRunningReturnsTrueWhenViteServerIsAccessible()
32+
public function testRunningReturnsTrueWhenViteHotFileExists()
2333
{
24-
// Create a mock server to simulate Vite
25-
$server = stream_socket_server('tcp://localhost:5173');
34+
$this->file('app/storage/framework/cache/vite.hot');
2635

2736
$this->assertTrue(Vite::running());
28-
29-
stream_socket_shutdown($server, STREAM_SHUT_RDWR);
3037
}
3138

32-
public function testRunningReturnsFalseWhenViteServerIsNotAccessible()
39+
public function testRunningReturnsFalseWhenViteHotFileDoesNotExist()
3340
{
3441
$this->assertFalse(Vite::running());
3542
}

vite.config.js

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,34 @@ import autoprefixer from 'autoprefixer';
99
import fs from 'fs';
1010
import path from 'path';
1111

12+
const hydeVitePlugin = () => ({
13+
name: 'hyde-vite',
14+
configureServer(server) {
15+
// Create hot file when Vite server starts
16+
fs.writeFileSync(path.resolve(process.cwd(), 'app/storage/framework/cache/vite.hot'), '');
17+
18+
// Remove hot file when Vite server closes
19+
['SIGINT', 'SIGTERM'].forEach(signal => {
20+
process.on(signal, () => {
21+
fs.rmSync(path.resolve(process.cwd(), 'app/storage/framework/cache/vite.hot'));
22+
process.exit();
23+
});
24+
});
25+
26+
// Render the Vite index page when the root URL is requested
27+
server.middlewares.use((req, res, next) => {
28+
if (req.url === '/') {
29+
res.end(fs.readFileSync(
30+
path.resolve(__dirname, 'vendor/hyde/realtime-compiler/resources/vite-index-page.html'),
31+
'utf-8'
32+
));
33+
} else {
34+
next();
35+
}
36+
});
37+
}
38+
});
39+
1240
export default defineConfig({
1341
server: {
1442
port: 5173,
@@ -18,23 +46,7 @@ export default defineConfig({
1846
},
1947
middlewareMode: false,
2048
},
21-
plugins: [
22-
{
23-
name: 'hyde-vite-server',
24-
configureServer(server) {
25-
server.middlewares.use((req, res, next) => {
26-
if (req.url === '/') {
27-
res.end(fs.readFileSync(
28-
path.resolve(__dirname, 'vendor/hyde/realtime-compiler/resources/vite-index-page.html'),
29-
'utf-8'
30-
));
31-
} else {
32-
next();
33-
}
34-
});
35-
},
36-
},
37-
],
49+
plugins: [hydeVitePlugin()],
3850
css: {
3951
postcss: {
4052
plugins: [

0 commit comments

Comments
 (0)