Skip to content

Commit

Permalink
fix(dev): define process.env.REMIX_DEV_HTTP_PORT in server build (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
pcattori authored Apr 25, 2023
1 parent 12f224e commit 341dd3a
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 47 deletions.
87 changes: 52 additions & 35 deletions packages/remix-dev/cli/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { CodemodError } from "../codemod/utils/error";
import { TaskError } from "../codemod/utils/task";
import { transpile as convertFileToJS } from "./useJavascript";
import { warnOnce } from "../warnOnce";
import type { Options } from "../compiler/options";

export async function create({
appTemplate,
Expand Down Expand Up @@ -169,24 +170,27 @@ export async function build(

let start = Date.now();
let config = await readConfig(remixRoot);
let options: Options = {
mode,
sourcemap,
onWarning: warnOnce,
};
if (config.future.unstable_dev) {
let dev = await resolveDev(config.future.unstable_dev);
options.devHttpPort = dev.httpPort;
options.devWebsocketPort = dev.websocketPort;
}

fse.emptyDirSync(config.assetsBuildDirectory);
await compiler
.build({
config,
options: {
mode,
sourcemap,
onWarning: warnOnce,
},
})
.catch((thrown) => {
compiler.logThrown(thrown);
process.exit(1);
});
await compiler.build({ config, options }).catch((thrown) => {
compiler.logThrown(thrown);
process.exit(1);
});

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

// TODO: replace watch in v2
export async function watch(
remixRootOrConfig: string | RemixConfig,
modeArg?: string
Expand Down Expand Up @@ -232,28 +236,7 @@ export async function dev(
}

let { unstable_dev } = config.future;

let command =
flags.command ?? (unstable_dev === true ? undefined : unstable_dev.command);
let httpPort =
flags.httpPort ??
(unstable_dev === true ? undefined : unstable_dev.httpPort) ??
(await findPort());
let websocketPort =
flags.websocketPort ??
(unstable_dev === true ? undefined : unstable_dev.websocketPort) ??
(await findPort());
let restart =
flags.restart ??
(unstable_dev === true ? undefined : unstable_dev.restart) ??
true;

await devServer_unstable.serve(config, {
command,
httpPort,
websocketPort,
restart,
});
await devServer_unstable.serve(config, await resolveDev(unstable_dev, flags));
}

export async function codemod(
Expand Down Expand Up @@ -481,3 +464,37 @@ let parseMode = (
};

let findPort = async () => getPort({ port: makeRange(3001, 3100) });

let resolveDev = async (
dev: Exclude<RemixConfig["future"]["unstable_dev"], false>,
flags: {
command?: string;
httpPort?: number;
restart?: boolean;
websocketPort?: number;
} = {}
): Promise<{
command?: string;
httpPort: number;
restart: boolean;
websocketPort: number;
}> => {
let command = flags.command ?? (dev === true ? undefined : dev.command);
let httpPort =
flags.httpPort ??
(dev === true ? undefined : dev.httpPort) ??
(await findPort());
let websocketPort =
flags.websocketPort ??
(dev === true ? undefined : dev.websocketPort) ??
(await findPort());
let restart =
flags.restart ?? (dev === true ? undefined : dev.restart) ?? true;

return {
command,
httpPort,
websocketPort,
restart,
};
};
5 changes: 4 additions & 1 deletion packages/remix-dev/compiler/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ type Mode = "development" | "production" | "test";

export type Options = {
mode: Mode;
liveReloadPort?: number;
sourcemap: boolean;
onWarning?: (message: string, key: string) => void;

// TODO: required in v2
devHttpPort?: number;
devWebsocketPort?: number;
};
4 changes: 4 additions & 0 deletions packages/remix-dev/compiler/server/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,13 @@ const createEsbuildConfig = (
publicPath: ctx.config.publicPath,
define: {
"process.env.NODE_ENV": JSON.stringify(ctx.options.mode),
// TODO: remove REMIX_DEV_SERVER_WS_PORT in v2
"process.env.REMIX_DEV_SERVER_WS_PORT": JSON.stringify(
ctx.config.devServerPort
),
"process.env.REMIX_DEV_HTTP_PORT": JSON.stringify(
ctx.options.devHttpPort ?? ""
),
},
jsx: "automatic",
jsxDev: ctx.options.mode !== "production",
Expand Down
4 changes: 2 additions & 2 deletions packages/remix-dev/compiler/server/plugins/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ ${Object.keys(config.routes)
export const publicPath = ${JSON.stringify(config.publicPath)};
export const entry = { module: entryServer };
${
options.liveReloadPort
options.devWebsocketPort
? `export const dev = ${JSON.stringify({
liveReloadPort: options.liveReloadPort,
websocketPort: options.devWebsocketPort,
})}`
: ""
}
Expand Down
3 changes: 2 additions & 1 deletion packages/remix-dev/devServer_unstable/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ export let serve = async (
config,
options: {
mode: "development",
liveReloadPort: options.websocketPort, // TODO: rename liveReloadPort
sourcemap: true,
onWarning: warnOnce,
devHttpPort: options.httpPort,
devWebsocketPort: options.websocketPort,
},
},
{
Expand Down
6 changes: 3 additions & 3 deletions packages/remix-react/__tests__/components-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,22 @@ describe("<LiveReload />", () => {
LiveReload = require("../components").LiveReload;
let { container } = render(<LiveReload />);
expect(container.querySelector("script")).toHaveTextContent(
"let port = (window.__remixContext && window.__remixContext.dev && window.__remixContext.dev.liveReloadPort) || 8002;"
"let port = (window.__remixContext && window.__remixContext.dev && window.__remixContext.dev.websocketPort) || 8002;"
);
});

it("can set the port explicitly", () => {
let { container } = render(<LiveReload port={4321} />);
expect(container.querySelector("script")).toHaveTextContent(
"let port = (window.__remixContext && window.__remixContext.dev && window.__remixContext.dev.liveReloadPort) || 4321;"
"let port = (window.__remixContext && window.__remixContext.dev && window.__remixContext.dev.websocketPort) || 4321;"
);
});

it("determines the right port based on REMIX_DEV_SERVER_WS_PORT env variable", () => {
process.env.REMIX_DEV_SERVER_WS_PORT = "1234";
let { container } = render(<LiveReload />);
expect(container.querySelector("script")).toHaveTextContent(
"let port = (window.__remixContext && window.__remixContext.dev && window.__remixContext.dev.liveReloadPort) || 1234;"
"let port = (window.__remixContext && window.__remixContext.dev && window.__remixContext.dev.websocketPort) || 1234;"
);
});

Expand Down
2 changes: 1 addition & 1 deletion packages/remix-react/browser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ declare global {
// The number of active deferred keys rendered on the server
a?: number;
dev?: {
liveReloadPort?: number;
websocketPort?: number;
hmrRuntime?: string;
};
};
Expand Down
3 changes: 2 additions & 1 deletion packages/remix-react/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1689,6 +1689,7 @@ export const LiveReload =
process.env.NODE_ENV !== "development"
? () => null
: function LiveReload({
// TODO: remove REMIX_DEV_SERVER_WS_PORT in v2
port = Number(process.env.REMIX_DEV_SERVER_WS_PORT || 8002),
timeoutMs = 1000,
nonce = undefined,
Expand All @@ -1707,7 +1708,7 @@ export const LiveReload =
function remixLiveReloadConnect(config) {
let protocol = location.protocol === "https:" ? "wss:" : "ws:";
let host = location.hostname;
let port = (window.__remixContext && window.__remixContext.dev && window.__remixContext.dev.liveReloadPort) || ${String(
let port = (window.__remixContext && window.__remixContext.dev && window.__remixContext.dev.websocketPort) || ${String(
port
)};
let socketPath = protocol + "//" + host + ":" + port + "/socket";
Expand Down
2 changes: 1 addition & 1 deletion packages/remix-react/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface RemixContextObject {
serverHandoffString?: string;
future: FutureConfig;
abortDelay?: number;
dev?: { liveReloadPort: number };
dev?: { websocketPort: number };
}

// Additional React-Router information needed at runtime, but not hydrated
Expand Down
2 changes: 1 addition & 1 deletion packages/remix-server-runtime/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export interface ServerBuild {
publicPath: string;
assetsBuildDirectory: string;
future: FutureConfig;
dev?: { liveReloadPort: number };
dev?: { websocketPort: number };
}

export interface HandleDocumentRequestFunction {
Expand Down
2 changes: 1 addition & 1 deletion packages/remix-server-runtime/serverHandoff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function createServerHandoffString<T>(serverHandoff: {
// we'd end up including duplicate info
state: ValidateShape<T, HydrationState>;
future: FutureConfig;
dev?: { liveReloadPort: number };
dev?: { websocketPort: number };
}): string {
// Uses faster alternative of jsesc to escape data returned from the loaders.
// This string is inserted directly into the HTML in the `<Scripts>` element.
Expand Down

0 comments on commit 341dd3a

Please sign in to comment.