Skip to content

Commit

Permalink
chore: finish up replacing fetchModule with invoke (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa authored Nov 7, 2024
1 parent 39314cb commit 5d6748f
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 88 deletions.
24 changes: 2 additions & 22 deletions examples/browser-cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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<FetchFunction>),
);
res.end(JSON.stringify(result));
return;
}
next();
});
},
};
}

main();
21 changes: 3 additions & 18 deletions examples/browser-cli/src/runner.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
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(
{
root: options.root,
sourcemapInterceptor: false,
transport: {
fetchModule: fetchModuleFetchClient("custom"),
invoke: fetchClientFetchModule("custom"),
},
hmr: false,
},
Expand All @@ -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;
};
}
12 changes: 0 additions & 12 deletions examples/web-worker-rsc/src/lib/fetch-module-client.ts

This file was deleted.

21 changes: 0 additions & 21 deletions examples/web-worker-rsc/src/lib/fetch-module-server.ts

This file was deleted.

4 changes: 2 additions & 2 deletions examples/web-worker-rsc/src/lib/runner.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -10,7 +10,7 @@ export function createFetchRunner(options: {
root: options.root,
sourcemapInterceptor: false,
transport: {
fetchModule: fetchClientFetchModule(options.environmentName),
invoke: fetchClientFetchModule(options.environmentName),
},
hmr: false,
},
Expand Down
2 changes: 1 addition & 1 deletion examples/web-worker-rsc/vite.config.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
12 changes: 7 additions & 5 deletions examples/web-worker/src/lib/fetch-module-client.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
11 changes: 5 additions & 6 deletions examples/web-worker/src/lib/fetch-module-server.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import type { FetchFunction, Plugin } from "vite";
import { type 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<FetchFunction>),
);
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;
}
Expand Down
2 changes: 1 addition & 1 deletion examples/web-worker/src/lib/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function createFetchRunner(options: {
root: options.root,
sourcemapInterceptor: false,
transport: {
fetchModule: fetchClientFetchModule(options.environmentName),
invoke: fetchClientFetchModule(options.environmentName),
},
hmr: false,
},
Expand Down

0 comments on commit 5d6748f

Please sign in to comment.