Skip to content

Commit

Permalink
fix: 🐛 server port first
Browse files Browse the repository at this point in the history
  • Loading branch information
guoshuangyang committed Jan 14, 2025
1 parent ce44858 commit 93b69e6
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions packages/bundler-webpack/src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,31 +240,42 @@ export async function createServer(opts: IOpts): Promise<any> {

const parseSocketServerAddress = async () => {
if (!process.env.SOCKET_SERVER) return basePort;
const {port} = new URL(process.env.SOCKET_SERVER);
const { port } = new URL(process.env.SOCKET_SERVER);
const startPort = Number(port) || basePort;
const hmrPort = await portfinder.getPortPromise({ port: startPort })
if (port && startPort !== hmrPort) {
console.log(`[SOCKET_SERVER] hmr port changed from ${port} to ${basePort}`)
try {
const hmrPort = await portfinder.getPortPromise({
port: startPort,
stopPort: startPort + 1,
});
if (port && startPort !== hmrPort) {
console.log(
`[SOCKET_SERVER] hmr port changed from ${port} to ${basePort}`,
);
return undefined;
}
return startPort === basePort ? undefined : startPort;
} catch (e) {
console.log(`[SOCKET_SERVER] hmr port not found, use ${basePort}`);
return undefined;
}
return startPort === basePort ? undefined : startPort;
};

ws = createWebSocketServer(server, await parseSocketServerAddress());

ws.wss.on('connection', (socket) => {
if (stats) {
sendStats(getStats(stats), false, socket);
}
});
const hmrPort = await parseSocketServerAddress();

server.listen(basePort, () => {
const banner = getDevBanner(protocol, opts.host, basePort);

console.log(banner.before);
logger.ready(banner.main);
console.log(banner.after);
});

ws = createWebSocketServer(server, hmrPort);

ws.wss.on('connection', (socket) => {
if (stats) {
sendStats(getStats(stats), false, socket);
}
});

return server;
}

0 comments on commit 93b69e6

Please sign in to comment.