Skip to content

Commit

Permalink
refactor: obtain HMRHandler from onUpdate callback
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Feb 1, 2024
1 parent 8b32d19 commit 322c69e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
11 changes: 8 additions & 3 deletions packages/vite-node-miniflare/src/client/vite-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
proxyTinyRpc,
} from "@hiogawa/tiny-rpc";
import { tinyassert } from "@hiogawa/utils";
import type { HMRPayload } from "vite";
import type { ViteNodeRunnerOptions } from "vite-node";
import { ViteNodeRunner } from "vite-node/client";
import { installSourcemapsSupport } from "vite-node/source-map";
Expand All @@ -16,6 +17,7 @@ export interface ViteNodeMiniflareClient {
rpc: TinyRpcProxy<ViteNodeRpc>;
runner: ViteNodeRunner;
runtime: ViteRuntime;
runtimeHMRHandler: (payload: HMRPayload) => Promise<void>;
}

export function createViteNodeClient(options: {
Expand All @@ -31,6 +33,8 @@ export function createViteNodeClient(options: {
adapter: httpClientAdapter({ url: options.serverRpcUrl }),
});

let runtimeHMRHandler!: (payload: HMRPayload) => Promise<void>;

const runtime = new ViteRuntime(
{
root: options.runnerOptions.root,
Expand All @@ -47,9 +51,10 @@ export function createViteNodeClient(options: {
send(messages) {
console.log("[runtime.hmr.connection.send]", messages);
},
// TODO: for now, we fetch HMRPayload via rpc, so nothing to register
// TODO: for now, we fetch HMRPayload via separate rpc, so we just grab the callback and use it later.
onUpdate(callback) {
console.log("[runtime.hmr.connection.onUpdate]", callback);
// this is called during ViteRuntime constructor
runtimeHMRHandler = callback as any;
},
},
logger: console,
Expand Down Expand Up @@ -100,7 +105,7 @@ export function createViteNodeClient(options: {
},
});

return { rpc, runner, runtime };
return { rpc, runner, runtime, runtimeHMRHandler };
}

function setupBufferPolyfill() {
Expand Down
3 changes: 1 addition & 2 deletions packages/vite-node-miniflare/src/client/worker-entry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { handleHMRUpdate } from "vite/runtime";
import {
type ViteNodeMiniflareClient,
createViteNodeClient,
Expand Down Expand Up @@ -49,7 +48,7 @@ export default {
}
continue;
}
await handleHMRUpdate(client.runtime, payload);
await client.runtimeHMRHandler(payload);
}

const workerEntry = await client.runtime.executeEntrypoint(
Expand Down

0 comments on commit 322c69e

Please sign in to comment.