From 2dc6e17cd936382885078c50ce4ebcbacff6ed80 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Sun, 27 Oct 2024 18:36:43 +0900 Subject: [PATCH] refactor: createTransportWithInvoke --- examples/browser-cli/src/cli.ts | 1 + .../web-worker/src/lib/fetch-module-server.ts | 32 ++++++++----------- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/examples/browser-cli/src/cli.ts b/examples/browser-cli/src/cli.ts index 4eae9232..25f8c472 100644 --- a/examples/browser-cli/src/cli.ts +++ b/examples/browser-cli/src/cli.ts @@ -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, diff --git a/examples/web-worker/src/lib/fetch-module-server.ts b/examples/web-worker/src/lib/fetch-module-server.ts index 84cb09e8..13f026e2 100644 --- a/examples/web-worker/src/lib/fetch-module-server.ts +++ b/examples/web-worker/src/lib/fetch-module-server.ts @@ -1,4 +1,4 @@ -import { DevEnvironment, type DevEnvironmentOptions, type Plugin } from "vite"; +import { type HotChannel, type Plugin } from "vite"; export function vitePluginFetchModuleServer(): Plugin { return { @@ -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; } @@ -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; -}; + }; +}