diff --git a/packages/vite-node-miniflare/src/client/vite-node.ts b/packages/vite-node-miniflare/src/client/vite-node.ts index bb3bb5402..3b9868d35 100644 --- a/packages/vite-node-miniflare/src/client/vite-node.ts +++ b/packages/vite-node-miniflare/src/client/vite-node.ts @@ -7,6 +7,7 @@ import { tinyassert } from "@hiogawa/utils"; import type { ViteNodeRunnerOptions } from "vite-node"; import { ViteNodeRunner } from "vite-node/client"; import { installSourcemapsSupport } from "vite-node/source-map"; +import { ESModulesRunner, ViteRuntime } from "vite/runtime"; import type { ViteNodeRpc } from ".."; import { __setDebug } from "./polyfills/debug"; import { __setUnsafeEval } from "./polyfills/node-vm"; @@ -14,6 +15,7 @@ import { __setUnsafeEval } from "./polyfills/node-vm"; export interface ViteNodeMiniflareClient { rpc: TinyRpcProxy; runner: ViteNodeRunner; + runtime: ViteRuntime; } export function createViteNodeClient(options: { @@ -29,6 +31,38 @@ export function createViteNodeClient(options: { adapter: httpClientAdapter({ url: options.serverRpcUrl }), }); + const runtime = new ViteRuntime( + { + root: options.runnerOptions.root, + fetchModule(id, importer) { + console.log({ id, importer }); + return rpc.ssrFetchModule(id, importer); + }, + // TODO + // hmr: {}, + }, + { + ...new ESModulesRunner(), // TODO: processImport? + + async runViteModule(context, transformed) { + // do same as vite-node/client + // https://github.com/vitest-dev/vitest/blob/c6e04125fb4a0af2db8bd58ea193b965d50d415f/packages/vite-node/src/client.ts#L415 + const codeDefinition = `'use strict';async (${Object.keys(context).join( + "," + )})=>{{`; + const code = `${codeDefinition}${transformed}\n}}`; + const fn = options.unsafeEval.eval(code, "todo-filename"); + await fn(...Object.values(context)); + Object.freeze(context.__vite_ssr_exports__); + }, + + runExternalModule(filepath, metadata) { + console.error("[runExternalModule]", filepath, metadata); + throw new Error(`[runExternalModule] ${filepath}`); + }, + } + ); + const runner = new ViteNodeRunner({ ...options.runnerOptions, fetchModule(id) { @@ -53,7 +87,7 @@ export function createViteNodeClient(options: { }, }); - return { rpc, runner }; + return { rpc, runner, runtime }; } function setupBufferPolyfill() { diff --git a/packages/vite-node-miniflare/src/client/worker-entry.ts b/packages/vite-node-miniflare/src/client/worker-entry.ts index 149c23a83..44bfa8bf4 100644 --- a/packages/vite-node-miniflare/src/client/worker-entry.ts +++ b/packages/vite-node-miniflare/src/client/worker-entry.ts @@ -24,6 +24,22 @@ export default { debug: env.__VITE_NODE_DEBUG, }); + if (1) { + // TODO: hmr, full-reload + + // TODO: for now, invalidate module tree like before? + client.runtime.moduleCache; + + const workerEntry = await client.runtime.executeEntrypoint( + env.__WORKER_ENTRY + ); + const workerEnv = { + ...env, + __VITE_NODE_MINIFLARE_CLIENT: client, + }; + return await workerEntry.default.fetch(request, workerEnv, ctx); + } + // invalidate modules similar to nuxt // https://github.com/nuxt/nuxt/blob/1de44a5a5ca5757d53a8b52c9809cbc027d2d246/packages/vite/src/runtime/vite-node.mjs#L21-L23 const invalidatedModules = await client.rpc.getInvalidatedModules(); diff --git a/packages/vite-node-miniflare/src/server/vite-node.ts b/packages/vite-node-miniflare/src/server/vite-node.ts index 35a09707a..843c49098 100644 --- a/packages/vite-node-miniflare/src/server/vite-node.ts +++ b/packages/vite-node-miniflare/src/server/vite-node.ts @@ -10,7 +10,7 @@ import { WORKER_ENTRY_SCRIPT } from "../client/worker-entry-script"; // prettier-ignore export type ViteNodeRpc = Pick & - Pick & + Pick & { getInvalidatedModules: () => string[]; }; @@ -29,6 +29,7 @@ export function setupViteNodeServerRpc( fetchModule: viteNodeServer.fetchModule.bind(viteNodeServer), resolveId: viteNodeServer.resolveId.bind(viteNodeServer), transformIndexHtml: viteNodeServer.server.transformIndexHtml, + ssrFetchModule: viteNodeServer.server.ssrFetchModule, getInvalidatedModules: () => { // there must be at most one client to make use of this RPC const result = [...invalidatedModules];