-
Notifications
You must be signed in to change notification settings - Fork 153
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
Use HMR port when specified #63
Conversation
Use hmr.port when specified
Have you tried configuring the |
By the way, the TypeScript error with your PR is that |
I did not try the VITE_PORT var, as I am not using Sail. I simply used the same wording as pull request #42 (referencing Sail), which is very similar. My setup is actually using custom Docker containers. Since I have multiple containers running on my machine, I need a different port to access each Vite server. I have Vite running on the default port (3000) inside the containers, but each container has a different port mapped to 3000. For example, on a particular container, I mapped port 3001 (external) to 3000 (internal). Then on another one, 3002 to 3000. Etc. Sorry for the poor pull request, it’s my first pull request ever, in a language I’m not familiar with, and I’m not equipped to test different setups than mine. |
That is weird, I followed the same syntax as lines 394 and 399. |
The difference with those is they are later checked for undefined/null and another value is used instead that is guaranteed not to be undefined. You might be better off with something like this: const configHmrPort = typeof config.server.hmr === 'object' ? config.server.hmr.port : null;
const port = configHmrPort ?? address.port |
No worries! I would suggest adding Then you can configure the |
When I look at index.ts line 114, it seems that |
Oh, good point! You could try setting that variable as well, although I'm not 100% sure what else it's used for. As for this PR, I'll need to have more of a think about the impacts of this with the |
Could I also suggest investigating |
I have a team of developers that are all set up to use Docker stacks and Traefik as a reverse-proxy/ingress container on each of their machines as we have a number of projects in the pipe at any one time. Now that Vite is the js-builder of choice in new Laravel projects, and it seems set on making the HMR server start as part of dev, I need to configure new projects to work with HMR by default (without having to co-ordinate which projects use which ports for which listener all the time). In my test case, I clearly have the vite-hmr server accessible through ingress, as I can hit e.g. https://test5-vite.nb.test/@vite/client in a broswer and I get a HTML response "This is the Vite development server that provides Hot Module Replacement for your Laravel application. To access your Laravel application, you will need to run a local development server." and npm run dev outputs:
I have the following in my vite.config.js:
FTR I have chosen port 3220 myself at random (I was finding 3000 at the time was conflicting with other services in the same container) but I am not exposing this outside the container (to avoid conflicts) and instead creating some docker labels to advise Traefik as such:
I have also previously had success configuring apache or nginx to ProxyPass a subfolder to a websocket port, but that is outside the bounds of this discussion. My problems start when I hit the laravel project as it fails while trying to websocket-connect to https://test5-vite.nb.test:3220/@vite/client. Setting Is there any reason why the laravel-vite-plugin should not respect the configuration I have provided? |
Thank you so much @jessarcher ! |
Yay, my first pull request ever! |
Congrats @guilheb! Thanks so much for helping to improve the ecosystem for everyone ❤️ |
This PR updates the hot file generation to use Vite's
server.hmr.port
config option when specified by the user.This configuration option is intended to tell the Vite client where to find the HMR server port for cases where it is different from
server.port
. I believe it makes sense for us to use it as well.This probably helps a range of use cases, but specifically, it allows users to run Vite inside a Sail container that is running inside WSL, where the Vite dev server needs to be configured on the container's public address, but where the host machine cannot connect to this same address.
Users running Vite inside Sail inside WSL will need to add the following to the
defineConfig
section of theirvite.config.js
file: