Skip to content

Commit

Permalink
fix: update createEnvironment typing
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Apr 21, 2024
1 parent e8b2b99 commit b62e425
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
2 changes: 1 addition & 1 deletion examples/custom/src/optimize-deps/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const server = await createServer({
const environment = server.environments["custom"];
tinyassert(environment);

const runner = createServerModuleRunner(environment);
const runner = createServerModuleRunner(server, environment);
const mod = await runner.import("/entry");
mod.default();

Expand Down
5 changes: 4 additions & 1 deletion examples/react-server/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,10 @@ function vitePluginReactServer(): PluginOption {
async configureServer(server) {
const reactServerEnv = server.environments["react-server"];
tinyassert(reactServerEnv);
const reactServerRunner = createServerModuleRunner(reactServerEnv);
const reactServerRunner = createServerModuleRunner(
server,
reactServerEnv,
);
$__global.server = server;
$__global.reactServerRunner = reactServerRunner;
},
Expand Down
2 changes: 1 addition & 1 deletion examples/react-ssr/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export function vitePluginSsrMiddleware({
},

configureServer(server) {
runner = createServerModuleRunner(server.environments.ssr);
runner = createServerModuleRunner(server, server.environments.ssr);

const handler: Connect.NextHandleFunction = async (req, res, next) => {
try {
Expand Down
15 changes: 7 additions & 8 deletions packages/workerd/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
type CustomPayload,
type HMRChannel,
type Plugin,
type ViteDevServer,
type ResolvedConfig,
} from "vite";
import { createMiddleware } from "@hattip/adapter-node/native-fetch";
import type { SourcelessWorkerOptions } from "wrangler";
Expand All @@ -44,8 +44,8 @@ export function vitePluginWorkerd(pluginOptions: WorkerdPluginOptions): Plugin {
environments: {
workerd: {
dev: {
createEnvironment: (server, name) =>
createWorkerdDevEnvironment(server, name, pluginOptions),
createEnvironment: (name, config) =>
createWorkerdDevEnvironment(name, config, pluginOptions),
},
build: pluginOptions.entry
? {
Expand Down Expand Up @@ -89,8 +89,8 @@ type WorkerdDevApi = {
};

export async function createWorkerdDevEnvironment(
server: ViteDevServer,
name: string,
config: ResolvedConfig,
pluginOptions: WorkerdEnvironmentOptions,
) {
// setup miniflare with a durable object script to run vite module runner
Expand All @@ -108,8 +108,6 @@ export async function createWorkerdDevEnvironment(
unsafeEvalBinding: "__viteUnsafeEval",
serviceBindings: {
__viteFetchModule: async (request) => {
const devEnv = server.environments["workerd"];
tinyassert(devEnv);
const args = await request.json();
try {
const result = await devEnv.fetchModule(...(args as [any, any]));
Expand All @@ -121,7 +119,7 @@ export async function createWorkerdDevEnvironment(
},
},
bindings: {
__viteRoot: server.config.root,
__viteRoot: config.root,
},
};

Expand Down Expand Up @@ -179,6 +177,7 @@ export async function createWorkerdDevEnvironment(
deserialize: (v) => JSON.parse(v.data),
});

// TODO: move initialization code to `init`?
// inheritance to extend dispose
class WorkerdDevEnvironmentImpl extends DevEnvironment {
override async close() {
Expand All @@ -187,7 +186,7 @@ export async function createWorkerdDevEnvironment(
}
}

const devEnv = new WorkerdDevEnvironmentImpl(server, name, { hot });
const devEnv = new WorkerdDevEnvironmentImpl(name, config, { hot });

// custom environment api
const api: WorkerdDevApi = {
Expand Down

0 comments on commit b62e425

Please sign in to comment.