Skip to content

Commit

Permalink
Merge branch 'main' into new-branch-farm
Browse files Browse the repository at this point in the history
  • Loading branch information
aniketkumar7 committed Mar 27, 2024
2 parents 4099578 + 1d1ae1f commit ec4ff0d
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/flat-gorillas-play.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@farmfe/core': patch
---

fix: Multiple server startups result in WebSocket connection interruptions.
3 changes: 2 additions & 1 deletion e2e/vitestSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ export const startProjectAndTest = async (
env: {
BROWSER: 'none',
NO_COLOR: 'true',
FARM_DEFAULT_SERVER_PORT: String(port)
FARM_DEFAULT_SERVER_PORT: String(port),
FARM_DEFAULT_HMR_PORT: String(port),
}
});

Expand Down
1 change: 1 addition & 0 deletions examples/react/farm.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default defineConfig({
},
},
server: {
port: 6532,
hmr: {
path: "/__farm_hmr",
}
Expand Down
2 changes: 1 addition & 1 deletion examples/react/src/components/welcome/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function Welcome() {
<FarmCard>
<div className="main-desc">
<h2 className="main-sub-title">
Get started With
Get started Withasdasd
<span className="main-content"> React + Farm</span>
</h2>
<span className="main-content">
Expand Down
13 changes: 9 additions & 4 deletions packages/core/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,13 +377,18 @@ export async function normalizeUserCompilationConfig(
) {
const publicPath = userConfig.compilation?.output?.publicPath ?? '/';
const hmrPath = userConfig.server.hmr.path;
const serverOptions = userConfig.server;
const defineHmrPath = normalizeBasePath(path.join(publicPath, hmrPath));

config.runtime.plugins.push(hmrClientPluginPath);
config.define.FARM_HMR_PORT = String(userConfig.server.hmr.port);
// TODO optimize get hmr logic
config.define.FARM_HMR_PORT = String(
(serverOptions.hmr.port || undefined) ??
serverOptions.port ??
DEFAULT_DEV_SERVER_OPTIONS.port
);
config.define.FARM_HMR_HOST = userConfig.server.hmr.host;
config.define.FARM_HMR_PROTOCOL = userConfig.server.hmr.protocol;

const defineHmrPath = normalizeBasePath(path.join(publicPath, hmrPath));
config.define.FARM_HMR_PATH = defineHmrPath;
}

Expand Down Expand Up @@ -443,7 +448,7 @@ export async function normalizeUserCompilationConfig(
export const DEFAULT_HMR_OPTIONS: Required<UserHmrConfig> = {
ignores: [],
host: true,
port: Number(process.env.FARM_DEFAULT_SERVER_PORT) || 9000,
port: Number(process.env.FARM_DEFAULT_HMR_PORT) ?? undefined,
path: '/__hmr',
protocol: 'ws',
watchOptions: {}
Expand Down
6 changes: 4 additions & 2 deletions packages/core/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,13 +299,13 @@ export class Server implements ImplDevServer {
logger: Logger
): Promise<void> {
let devPort = normalizedDevConfig.port;
let hmrPort = DEFAULT_HMR_OPTIONS.port;
let hmrPort = normalizedDevConfig.hmr.port;

const { strictPort, host } = normalizedDevConfig;
const httpServer = http.createServer();
const isPortAvailable = (portToCheck: number) => {
return new Promise((resolve, reject) => {
const onError = async (error: { code: string }) => {
logger.error(`Error in httpServer: ${error} `);
if (error.code === 'EADDRINUSE') {
clearScreen();
if (strictPort) {
Expand All @@ -317,6 +317,7 @@ export class Server implements ImplDevServer {
resolve(false);
}
} else {
logger.error(`Error in httpServer: ${error} `);
reject(true);
}
};
Expand All @@ -330,6 +331,7 @@ export class Server implements ImplDevServer {
};

let isPortAvailableResult = await isPortAvailable(devPort);

while (isPortAvailableResult === false) {
if (typeof normalizedDevConfig.hmr === 'object') {
normalizedDevConfig.hmr.port = ++hmrPort;
Expand Down
3 changes: 2 additions & 1 deletion packages/runtime-plugin-hmr/src/hmr-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { logger } from './logger';
import { ErrorOverlay, overlayId } from './overlay';

// Inject during compile time
const hmrPort = Number(FARM_HMR_PORT || 9000);
const hmrPort = Number(FARM_HMR_PORT);

const hmrHost =
typeof FARM_HMR_HOST === 'boolean'
? window.location.hostname || 'localhost'
Expand Down

0 comments on commit ec4ff0d

Please sign in to comment.