How can I Configure Vite to Automatically Stop the Server When Browser Tab is Closed?

I have configured a three.js project with an index.html file in directory C:Usersharryworkspacetest where the last commands in PowerShell are:

npm install --save three
npm install --save-dev vite

I have configured vite.config.mjs in the same directory with the following javascript content so that when I run npx vite it will automatically open my index.html file in a browser. This works perfectly fine.

import { defineConfig } from 'vite';

export default defineConfig({
  server: {
    open: true  // Automatically open the browser
  }
});

I’m currently running the following Python script within Blender 4.2 using its integrated Text Editor. However, it should also work independently by using a standard Python interpreter.

import subprocess
import os

directory = r'C:Usersharryworkspacetest'
command = 'npx vite'

subprocess.run(command, shell=True, cwd=directory, check=True)

This one works perfectly fine and displays the following output in the console and also successfully opens a browser tab with my index.html content:

enter image description here

Is it possible to configure vite to automatically stop the server when the browser tab is closed? I’m looking for a way to have the server detect when no clients are connected and shut down automatically. Currently, I have to manually stop the server by pressing qEnter in the console, because this server process blocks the Blender UI. Ideally, I’d like to run the script to test some output in the browser, then continue editing in Blender, and easily test again. Is this possible? Maybe by some additional config inside of vite.config.mjs?