Skip to content

Commit

Permalink
refactor: createTransportWithInvoke
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Nov 7, 2024
1 parent c04460e commit 2dc6e17
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
1 change: 1 addition & 0 deletions examples/browser-cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import repl from "node:repl";
import { createManualPromise, tinyassert } from "@hiogawa/utils";
import { chromium } from "@playwright/test";
import {
DevEnvironment,
type Plugin,
type PluginOption,
createServer,
Expand Down
32 changes: 14 additions & 18 deletions examples/web-worker/src/lib/fetch-module-server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DevEnvironment, type DevEnvironmentOptions, type Plugin } from "vite";
import { type HotChannel, type Plugin } from "vite";

export function vitePluginFetchModuleServer(): Plugin {
return {
Expand All @@ -9,7 +9,7 @@ export function vitePluginFetchModuleServer(): Plugin {
if (url.pathname === "/@vite/invoke") {
const [name, ...args] = JSON.parse(url.searchParams.get("payload")!);
const devEnv = server.environments[name]!;
const result = await (devEnv as any).__invoke(...args);
const result = devEnv.hot.api.invoke(...args);
res.end(JSON.stringify(result));
return;
}
Expand All @@ -19,21 +19,17 @@ export function vitePluginFetchModuleServer(): Plugin {
};
}

// expose `DevEnvironment.__invoke`
export const createEnvironmentWithInvoke: NonNullable<
DevEnvironmentOptions["createEnvironment"]
> = (name, config) => {
// expose `hot.api.invoke`
export function createTransportWithInvoke(): HotChannel {
let invokeHandler!: Function;
const devEnv = new DevEnvironment(name, config, {
hot: false,
transport: {
setInvokeHandler(invokeHandler_) {
if (invokeHandler_) {
invokeHandler = invokeHandler_;
}
},
return {
setInvokeHandler(invokeHandler_) {
if (invokeHandler_) {
invokeHandler = invokeHandler_;
}
},
api: {
invoke: (...args: any[]) => invokeHandler(...args),
},
});
Object.assign(devEnv, { __invoke: invokeHandler });
return devEnv;
};
};
}

0 comments on commit 2dc6e17

Please sign in to comment.