Skip to content

Commit edca0a6

Browse files
authored
fix(dev): define process.env.REMIX_DEV_HTTP_PORT in server build (#6158)
1 parent 5a4c234 commit edca0a6

File tree

7 files changed

+66
-41
lines changed

7 files changed

+66
-41
lines changed

packages/remix-dev/cli/commands.ts

Lines changed: 52 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { CodemodError } from "../codemod/utils/error";
2424
import { TaskError } from "../codemod/utils/task";
2525
import { transpile as convertFileToJS } from "./useJavascript";
2626
import { warnOnce } from "../warnOnce";
27+
import type { Options } from "../compiler/options";
2728

2829
export async function create({
2930
appTemplate,
@@ -169,24 +170,27 @@ export async function build(
169170

170171
let start = Date.now();
171172
let config = await readConfig(remixRoot);
173+
let options: Options = {
174+
mode,
175+
sourcemap,
176+
onWarning: warnOnce,
177+
};
178+
if (config.future.unstable_dev) {
179+
let dev = await resolveDev(config.future.unstable_dev);
180+
options.devHttpPort = dev.httpPort;
181+
options.devWebsocketPort = dev.websocketPort;
182+
}
183+
172184
fse.emptyDirSync(config.assetsBuildDirectory);
173-
await compiler
174-
.build({
175-
config,
176-
options: {
177-
mode,
178-
sourcemap,
179-
onWarning: warnOnce,
180-
},
181-
})
182-
.catch((thrown) => {
183-
compiler.logThrown(thrown);
184-
process.exit(1);
185-
});
185+
await compiler.build({ config, options }).catch((thrown) => {
186+
compiler.logThrown(thrown);
187+
process.exit(1);
188+
});
186189

187190
console.log(`built in ${prettyMs(Date.now() - start)}`);
188191
}
189192

193+
// TODO: replace watch in v2
190194
export async function watch(
191195
remixRootOrConfig: string | RemixConfig,
192196
modeArg?: string
@@ -232,28 +236,7 @@ export async function dev(
232236
}
233237

234238
let { unstable_dev } = config.future;
235-
236-
let command =
237-
flags.command ?? (unstable_dev === true ? undefined : unstable_dev.command);
238-
let httpPort =
239-
flags.httpPort ??
240-
(unstable_dev === true ? undefined : unstable_dev.httpPort) ??
241-
(await findPort());
242-
let websocketPort =
243-
flags.websocketPort ??
244-
(unstable_dev === true ? undefined : unstable_dev.websocketPort) ??
245-
(await findPort());
246-
let restart =
247-
flags.restart ??
248-
(unstable_dev === true ? undefined : unstable_dev.restart) ??
249-
true;
250-
251-
await devServer_unstable.serve(config, {
252-
command,
253-
httpPort,
254-
websocketPort,
255-
restart,
256-
});
239+
await devServer_unstable.serve(config, await resolveDev(unstable_dev, flags));
257240
}
258241

259242
export async function codemod(
@@ -481,3 +464,37 @@ let parseMode = (
481464
};
482465

483466
let findPort = async () => getPort({ port: makeRange(3001, 3100) });
467+
468+
let resolveDev = async (
469+
dev: Exclude<RemixConfig["future"]["unstable_dev"], false>,
470+
flags: {
471+
command?: string;
472+
httpPort?: number;
473+
restart?: boolean;
474+
websocketPort?: number;
475+
} = {}
476+
): Promise<{
477+
command?: string;
478+
httpPort: number;
479+
restart: boolean;
480+
websocketPort: number;
481+
}> => {
482+
let command = flags.command ?? (dev === true ? undefined : dev.command);
483+
let httpPort =
484+
flags.httpPort ??
485+
(dev === true ? undefined : dev.httpPort) ??
486+
(await findPort());
487+
let websocketPort =
488+
flags.websocketPort ??
489+
(dev === true ? undefined : dev.websocketPort) ??
490+
(await findPort());
491+
let restart =
492+
flags.restart ?? (dev === true ? undefined : dev.restart) ?? true;
493+
494+
return {
495+
command,
496+
httpPort,
497+
websocketPort,
498+
restart,
499+
};
500+
};

packages/remix-dev/compiler/options.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ type Mode = "development" | "production" | "test";
22

33
export type Options = {
44
mode: Mode;
5-
liveReloadPort?: number;
65
sourcemap: boolean;
76
onWarning?: (message: string, key: string) => void;
7+
8+
// TODO: required in v2
9+
devHttpPort?: number;
10+
devWebsocketPort?: number;
811
};

packages/remix-dev/compiler/server/compiler.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,13 @@ const createEsbuildConfig = (
9898
publicPath: ctx.config.publicPath,
9999
define: {
100100
"process.env.NODE_ENV": JSON.stringify(ctx.options.mode),
101+
// TODO: remove REMIX_DEV_SERVER_WS_PORT in v2
101102
"process.env.REMIX_DEV_SERVER_WS_PORT": JSON.stringify(
102103
ctx.config.devServerPort
103104
),
105+
"process.env.REMIX_DEV_HTTP_PORT": JSON.stringify(
106+
ctx.options.devHttpPort ?? ""
107+
),
104108
},
105109
jsx: "automatic",
106110
jsxDev: ctx.options.mode !== "production",

packages/remix-dev/compiler/server/plugins/entry.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ ${Object.keys(config.routes)
5151
export const publicPath = ${JSON.stringify(config.publicPath)};
5252
export const entry = { module: entryServer };
5353
${
54-
options.liveReloadPort
54+
options.devWebsocketPort
5555
? `export const dev = ${JSON.stringify({
56-
liveReloadPort: options.liveReloadPort,
56+
websocketPort: options.devWebsocketPort,
5757
})}`
5858
: ""
5959
}

packages/remix-dev/devServer_unstable/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,10 @@ export let serve = async (
5151
config,
5252
options: {
5353
mode: "development",
54-
liveReloadPort: options.websocketPort, // TODO: rename liveReloadPort
5554
sourcemap: true,
5655
onWarning: warnOnce,
56+
devHttpPort: options.httpPort,
57+
devWebsocketPort: options.websocketPort,
5758
},
5859
},
5960
{

packages/remix-server-runtime/build.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export interface ServerBuild {
1414
publicPath: string;
1515
assetsBuildDirectory: string;
1616
future: FutureConfig;
17-
dev?: { liveReloadPort: number };
17+
dev?: { websocketPort: number };
1818
}
1919

2020
export interface HandleDocumentRequestFunction {

packages/remix-server-runtime/serverHandoff.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export function createServerHandoffString<T>(serverHandoff: {
2020
// we'd end up including duplicate info
2121
state: ValidateShape<T, HydrationState>;
2222
future: FutureConfig;
23-
dev?: { liveReloadPort: number };
23+
dev?: { websocketPort: number };
2424
}): string {
2525
// Uses faster alternative of jsesc to escape data returned from the loaders.
2626
// This string is inserted directly into the HTML in the `<Scripts>` element.

0 commit comments

Comments
 (0)