Skip to content

Commit

Permalink
fix(dev): dynamic, configurable unstable_dev port
Browse files Browse the repository at this point in the history
  • Loading branch information
pcattori committed Feb 9, 2023
1 parent d7e9b60 commit 797792c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/remix-dev/compiler/plugins/hmrPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ declare global {
function remixLiveReloadConnect(config) {
let protocol = location.protocol === "https:" ? "wss:" : "ws:";
let host = location.hostname;
let port = process.env.REMIX_DEV_SERVER_WS_PORT;
let port = window.__remixContext.dev.liveReloadPort;
let socketPath = protocol + "//" + host + ":" + port + "/socket";
let ws = new WebSocket(socketPath);
ws.onmessage = async (event) => {
Expand Down
18 changes: 8 additions & 10 deletions packages/remix-dev/devServer2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ let findPort = async (portPreference?: number) =>
// prettier-ignore
portPreference ? Number(portPreference) :
process.env.PORT ? Number(process.env.PORT) :
makeRange(8002, 8100),
makeRange(3001, 3100),
});

let fetchAssetsManifest = async (
Expand All @@ -54,16 +54,12 @@ let fetchAssetsManifest = async (

let resolveDev = (
dev: RemixConfig["future"]["unstable_dev"],
flags: { port?: number; appServerPort?: number },
fallbackPort = 8002
flags: { port?: number; appServerPort?: number }
) => {
if (dev === false)
throw Error("The new dev server requires 'unstable_dev' to be set");

let port = fallbackPort;
// if (typeof flags.port === "number") {
// port = flags.port;
// }
let port = flags.port ?? (dev === true ? undefined : dev.port);

let appServerPort =
flags.appServerPort ?? (dev === true || dev.appServerPort == undefined)
Expand Down Expand Up @@ -93,7 +89,8 @@ export let serve = async (
clean(config);
await loadEnv(config.rootDirectory);

let dev = resolveDev(config.future.unstable_dev, flags, config.devServerPort);
let dev = resolveDev(config.future.unstable_dev, flags);
console.log({ dev });

let host = getHost();
let appServerOrigin = `http://${host ?? "localhost"}:${dev.appServerPort}`;
Expand All @@ -112,11 +109,12 @@ export let serve = async (
};

// watch and live reload on rebuilds
let socket = LiveReload.serve({ port: dev.port });
let port = await findPort(dev.port);
let socket = LiveReload.serve({ port });
let prevResult: Compiler.CompileResult | undefined = undefined;
let dispose = await Compiler.watch(config, {
mode: "development",
liveReloadPort: dev.port,
liveReloadPort: port,
onInitialBuild: (durationMs, result) => {
info(`Built in ${prettyMs(durationMs)}`);
prevResult = result;
Expand Down

0 comments on commit 797792c

Please sign in to comment.