From 5d6748f4d0faf378985f083c017b9ef0233cc689 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Thu, 7 Nov 2024 09:50:18 +0900 Subject: [PATCH] chore: finish up replacing `fetchModule` with `invoke` (#145) --- examples/browser-cli/src/cli.ts | 24 ++----------------- examples/browser-cli/src/runner.ts | 21 +++------------- .../src/lib/fetch-module-client.ts | 12 ---------- .../src/lib/fetch-module-server.ts | 21 ---------------- examples/web-worker-rsc/src/lib/runner.ts | 4 ++-- examples/web-worker-rsc/vite.config.ts | 2 +- .../web-worker/src/lib/fetch-module-client.ts | 12 ++++++---- .../web-worker/src/lib/fetch-module-server.ts | 11 ++++----- examples/web-worker/src/lib/runner.ts | 2 +- 9 files changed, 21 insertions(+), 88 deletions(-) delete mode 100644 examples/web-worker-rsc/src/lib/fetch-module-client.ts delete mode 100644 examples/web-worker-rsc/src/lib/fetch-module-server.ts diff --git a/examples/browser-cli/src/cli.ts b/examples/browser-cli/src/cli.ts index 4d27f979..4eae9232 100644 --- a/examples/browser-cli/src/cli.ts +++ b/examples/browser-cli/src/cli.ts @@ -7,7 +7,8 @@ import { createServer, parseAstAsync, } from "vite"; -import type { FetchFunction, ModuleRunner } from "vite/module-runner"; +import type { ModuleRunner } from "vite/module-runner"; +import { vitePluginFetchModuleServer } from "../../web-worker/src/lib/fetch-module-server"; const headless = !process.env["CLI_HEADED"]; const extension = process.env["CLI_EXTENSION"] ?? "tsx"; @@ -185,25 +186,4 @@ function vitePluginBrowserRunner(): Plugin { }; } -// https://github.com/vitejs/vite/discussions/18191 -function vitePluginFetchModuleServer(): Plugin { - return { - name: vitePluginFetchModuleServer.name, - configureServer(server) { - server.middlewares.use(async (req, res, next) => { - const url = new URL(req.url ?? "/", "https://any.local"); - if (url.pathname === "/@vite/fetchModule") { - const [name, ...args] = JSON.parse(url.searchParams.get("payload")!); - const result = await server.environments[name]!.fetchModule( - ...(args as Parameters), - ); - res.end(JSON.stringify(result)); - return; - } - next(); - }); - }, - }; -} - main(); diff --git a/examples/browser-cli/src/runner.ts b/examples/browser-cli/src/runner.ts index 18501e76..332a3ed3 100644 --- a/examples/browser-cli/src/runner.ts +++ b/examples/browser-cli/src/runner.ts @@ -1,8 +1,5 @@ -import { - ESModulesEvaluator, - type FetchFunction, - ModuleRunner, -} from "vite/module-runner"; +import { ESModulesEvaluator, ModuleRunner } from "vite/module-runner"; +import { fetchClientFetchModule } from "../../web-worker/src/lib/fetch-module-client"; export async function start(options: { root: string }) { const runner = new ModuleRunner( @@ -10,7 +7,7 @@ export async function start(options: { root: string }) { root: options.root, sourcemapInterceptor: false, transport: { - fetchModule: fetchModuleFetchClient("custom"), + invoke: fetchClientFetchModule("custom"), }, hmr: false, }, @@ -19,15 +16,3 @@ export async function start(options: { root: string }) { return runner; } - -// https://github.com/vitejs/vite/discussions/18191 -function fetchModuleFetchClient(environmentName: string): FetchFunction { - return async (...args) => { - const payload = JSON.stringify([environmentName, ...args]); - const response = await fetch( - "/@vite/fetchModule?" + new URLSearchParams({ payload }), - ); - const result = response.json(); - return result as any; - }; -} diff --git a/examples/web-worker-rsc/src/lib/fetch-module-client.ts b/examples/web-worker-rsc/src/lib/fetch-module-client.ts deleted file mode 100644 index 157c65d3..00000000 --- a/examples/web-worker-rsc/src/lib/fetch-module-client.ts +++ /dev/null @@ -1,12 +0,0 @@ -import type { FetchFunction } from "vite"; - -export function fetchClientFetchModule(environmentName: string): FetchFunction { - return async (...args) => { - const payload = JSON.stringify([environmentName, ...args]); - const response = await fetch( - "/@vite/fetchModule?" + new URLSearchParams({ payload }), - ); - const result = response.json(); - return result as any; - }; -} diff --git a/examples/web-worker-rsc/src/lib/fetch-module-server.ts b/examples/web-worker-rsc/src/lib/fetch-module-server.ts deleted file mode 100644 index c9f83f74..00000000 --- a/examples/web-worker-rsc/src/lib/fetch-module-server.ts +++ /dev/null @@ -1,21 +0,0 @@ -import type { FetchFunction, Plugin } from "vite"; - -export function vitePluginFetchModuleServer(): Plugin { - return { - name: vitePluginFetchModuleServer.name, - configureServer(server) { - server.middlewares.use(async (req, res, next) => { - const url = new URL(req.url ?? "/", "https://any.local"); - if (url.pathname === "/@vite/fetchModule") { - const [name, ...args] = JSON.parse(url.searchParams.get("payload")!); - const result = await server.environments[name]!.fetchModule( - ...(args as Parameters), - ); - res.end(JSON.stringify(result)); - return; - } - next(); - }); - }, - }; -} diff --git a/examples/web-worker-rsc/src/lib/runner.ts b/examples/web-worker-rsc/src/lib/runner.ts index 93209025..5edf5a9c 100644 --- a/examples/web-worker-rsc/src/lib/runner.ts +++ b/examples/web-worker-rsc/src/lib/runner.ts @@ -1,5 +1,5 @@ import { ESModulesEvaluator, ModuleRunner } from "vite/module-runner"; -import { fetchClientFetchModule } from "./fetch-module-client"; +import { fetchClientFetchModule } from "../../../web-worker/src/lib/fetch-module-client"; export function createFetchRunner(options: { root: string; @@ -10,7 +10,7 @@ export function createFetchRunner(options: { root: options.root, sourcemapInterceptor: false, transport: { - fetchModule: fetchClientFetchModule(options.environmentName), + invoke: fetchClientFetchModule(options.environmentName), }, hmr: false, }, diff --git a/examples/web-worker-rsc/vite.config.ts b/examples/web-worker-rsc/vite.config.ts index 843f51c4..de09a4f5 100644 --- a/examples/web-worker-rsc/vite.config.ts +++ b/examples/web-worker-rsc/vite.config.ts @@ -1,7 +1,7 @@ import react from "@vitejs/plugin-react"; import { defineConfig } from "vite"; +import { vitePluginFetchModuleServer } from "../web-worker/src/lib/fetch-module-server"; import { vitePluginWorkerEnvironment } from "../web-worker/vite.config"; -import { vitePluginFetchModuleServer } from "./src/lib/fetch-module-server"; export default defineConfig((_env) => ({ clearScreen: false, diff --git a/examples/web-worker/src/lib/fetch-module-client.ts b/examples/web-worker/src/lib/fetch-module-client.ts index 157c65d3..d9c5b378 100644 --- a/examples/web-worker/src/lib/fetch-module-client.ts +++ b/examples/web-worker/src/lib/fetch-module-client.ts @@ -1,10 +1,12 @@ -import type { FetchFunction } from "vite"; +import type { ModuleRunnerTransport } from "vite/module-runner"; -export function fetchClientFetchModule(environmentName: string): FetchFunction { - return async (...args) => { - const payload = JSON.stringify([environmentName, ...args]); +export function fetchClientFetchModule( + environmentName: string, +): ModuleRunnerTransport["invoke"] { + return async (payload) => { + const data = JSON.stringify([environmentName, payload]); const response = await fetch( - "/@vite/fetchModule?" + new URLSearchParams({ payload }), + "/@vite/invoke?" + new URLSearchParams({ data }), ); const result = response.json(); return result as any; diff --git a/examples/web-worker/src/lib/fetch-module-server.ts b/examples/web-worker/src/lib/fetch-module-server.ts index c9f83f74..c2e97aa7 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 type { FetchFunction, Plugin } from "vite"; +import { type Plugin } from "vite"; export function vitePluginFetchModuleServer(): Plugin { return { @@ -6,11 +6,10 @@ export function vitePluginFetchModuleServer(): Plugin { configureServer(server) { server.middlewares.use(async (req, res, next) => { const url = new URL(req.url ?? "/", "https://any.local"); - if (url.pathname === "/@vite/fetchModule") { - const [name, ...args] = JSON.parse(url.searchParams.get("payload")!); - const result = await server.environments[name]!.fetchModule( - ...(args as Parameters), - ); + if (url.pathname === "/@vite/invoke") { + const [name, payload] = JSON.parse(url.searchParams.get("data")!); + const devEnv = server.environments[name]!; + const result = await devEnv.hot.handleInvoke(payload); res.end(JSON.stringify(result)); return; } diff --git a/examples/web-worker/src/lib/runner.ts b/examples/web-worker/src/lib/runner.ts index 93209025..0181f1f2 100644 --- a/examples/web-worker/src/lib/runner.ts +++ b/examples/web-worker/src/lib/runner.ts @@ -10,7 +10,7 @@ export function createFetchRunner(options: { root: options.root, sourcemapInterceptor: false, transport: { - fetchModule: fetchClientFetchModule(options.environmentName), + invoke: fetchClientFetchModule(options.environmentName), }, hmr: false, },