I managed to hack my way trough.
I'm running Valet and the website is configured to run under the domain templates.test.
Step 1:
Add origin to the vite-config.js:
{
...
server: {
...
hmr: {host},
origin: `https://${host}`, // in my case it's https://templates.test
}
}
After adding origin, all workers urls will be pointing to the laravel server, instead of the Vite one.
Instead of https://domain:5174/node_modules/... you'll have https://domain/node_modules/...
Step 2:
In your web.php file, add this route:
if (env('ENVIRONMENT') != "production") {
// Match all routes that start with node_modules (from Vite)
Route::get('/node_modules/{any}', function ($any) {
// Disable ssl checks for php
$streamContext = stream_context_create([
"ssl" => [
"verify_peer" => false,
"verify_peer_name" => false,
],
]);
// Fetch the Vite compiled worker module and return it
return response(file_get_contents(config('app.url').":5174/node_modules/$any", false, $streamContext), 200)
->header("Content-Type", "text/javascript");
})->where('any', '(.*)');
}