Skip to content

Commit

Permalink
refactor: createEnvironmentWithInvoke
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Nov 7, 2024
1 parent 14ba845 commit dbf24ef
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 43 deletions.
36 changes: 14 additions & 22 deletions examples/browser-cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import {
createServer,
parseAstAsync,
} from "vite";
import type { FetchFunction, ModuleRunner } from "vite/module-runner";
import type { ModuleRunner } from "vite/module-runner";
import {
createEnvironmentWithInvoke,
vitePluginFetchModuleServer,
} from "../../web-worker/src/lib/fetch-module-server";

const headless = !process.env["CLI_HEADED"];
const extension = process.env["CLI_EXTENSION"] ?? "tsx";
Expand All @@ -29,6 +33,15 @@ async function main() {
resolve: {
noExternal: true,
},
<<<<<<< HEAD
=======
dev: {
createEnvironment: createEnvironmentWithInvoke,
optimizeDeps: {
exclude: ["vite/module-runner"],
},
},
>>>>>>> 45db83b (refactor: createEnvironmentWithInvoke)
},
},
server: {
Expand Down Expand Up @@ -185,25 +198,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;
};
}
21 changes: 20 additions & 1 deletion examples/web-worker/src/lib/fetch-module-server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Plugin } from "vite";
import { DevEnvironment, type DevEnvironmentOptions, type Plugin } from "vite";

export function vitePluginFetchModuleServer(): Plugin {
return {
Expand All @@ -18,3 +18,22 @@ export function vitePluginFetchModuleServer(): Plugin {
},
};
}

// expose `DevEnvironment.__invoke`
export const createEnvironmentWithInvoke: NonNullable<
DevEnvironmentOptions["createEnvironment"]
> = (name, config) => {
let invokeHandler!: Function;
const devEnv = new DevEnvironment(name, config, {
hot: false,
transport: {
setInvokeHandler(invokeHandler_) {
if (invokeHandler_) {
invokeHandler = invokeHandler_;
}
},
},
});
Object.assign(devEnv, { __invoke: invokeHandler });
return devEnv;
};
7 changes: 5 additions & 2 deletions examples/web-worker/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { rmSync } from "node:fs";
import react from "@vitejs/plugin-react";
import MagicString from "magic-string";
import { DevEnvironment, type Plugin, defineConfig } from "vite";
import { vitePluginFetchModuleServer } from "./src/lib/fetch-module-server";
import { type Plugin, defineConfig } from "vite";
import {
createEnvironmentWithInvoke,
vitePluginFetchModuleServer,
} from "./src/lib/fetch-module-server";

export default defineConfig((_env) => ({
clearScreen: false,
Expand Down

0 comments on commit dbf24ef

Please sign in to comment.