Skip to content

Commit

Permalink
wip: simple integration without hmr/invalidation
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Jan 21, 2024
1 parent 599be33 commit 385dc74
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
36 changes: 35 additions & 1 deletion packages/vite-node-miniflare/src/client/vite-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ 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";

export interface ViteNodeMiniflareClient {
rpc: TinyRpcProxy<ViteNodeRpc>;
runner: ViteNodeRunner;
runtime: ViteRuntime;
}

export function createViteNodeClient(options: {
Expand All @@ -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) {
Expand All @@ -53,7 +87,7 @@ export function createViteNodeClient(options: {
},
});

return { rpc, runner };
return { rpc, runner, runtime };
}

function setupBufferPolyfill() {
Expand Down
16 changes: 16 additions & 0 deletions packages/vite-node-miniflare/src/client/worker-entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion packages/vite-node-miniflare/src/server/vite-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { WORKER_ENTRY_SCRIPT } from "../client/worker-entry-script";
// prettier-ignore
export type ViteNodeRpc =
Pick<ViteNodeServer, "fetchModule" | "resolveId"> &
Pick<ViteDevServer, "transformIndexHtml"> &
Pick<ViteDevServer, "transformIndexHtml" | "ssrFetchModule"> &
{
getInvalidatedModules: () => string[];
};
Expand All @@ -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];
Expand Down

0 comments on commit 385dc74

Please sign in to comment.